Access ContextMenu's DOM node from a parent component
Closed this issue · 1 comments
the-realtom commented
For things like controlling focus, selection, getting size/position etc. we need a reference to the DOM node of ContextMenu.
Version
1.9.2
Test Case
- The trigger element is right-clicked which causes the context menu to open. The trigger element has now gained focus.
- When an item in the context menu is clicked (specifically the mousedown event), the trigger element will lose focus.
- To prevent this from happening, the default action for the mousedown event needs to be prevented (using
preventDefault
). We need to be able to attach an event handler to the ContextMenu from its parent. Currently the only way to do this is usingfindDOMNode
which is discouraged.
Possible Solutions
- To allow the parent to access the
<nav>
element's ref, a callback function could be provided as a prop which could then be invoked with the DOM element. You could invoke it straight afterthis.menu
is assigned:
react-contextmenu/src/ContextMenu.js
Lines 218 to 220 in d2ec76e
- An alternative solution (which would only solve the test case shown above) would be to allow a custom function to be provided as props, which would then be used as the
onMouseDown
event handler. You could do this in either ContextMenu or MenuItem (I think). MenuItem already does a similar thing for theonClick
handler, so you could essentially replicate that.
I'm happy to make a PR with whatever changes you see fit.
stale commented
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.