react-contexify icon indicating copy to clipboard operation
react-contexify copied to clipboard

Open menu without clicking

Open adamlibusa opened this issue 7 years ago • 1 comments

I want to programmatically open the menu on component load. I can call contextMenu.show(), but it requires an event. Right, I am trying something like this, for a menu to open at the right bottom corner of my element:

    const {bottom, right} = element.getBoundingClientRect()

    const event = new MouseEvent('click', {
      clientX: bottom,
      clientY: right,
    })

    contextMenu.show({
       ...
      event,
      ...
    })

but the menu opens at unexpected places on the screen. Is there a way to omit passing the event altogether and instead pass coordinates? Do you have any tips for this use case? Thank you!

adamlibusa avatar Apr 25 '19 12:04 adamlibusa

In the end, only a small change was necessary:

    const {x, y, width, height} = element.getBoundingClientRect()

    const event = new MouseEvent('click', {
      clientX: x + width,
      clientY: y + height,
    })

    contextMenu.show({
      ...
      event,
      ...
    })

adamlibusa avatar Apr 25 '19 13:04 adamlibusa