Clicking an unfocusable element inside of the menu closes it
See original GitHub issueIf there is any space between or around the menu items, clicking it will result in the menu closing. Also, clicking anything inside of the menu (such as a decorative arrow) will also close the menu.
This is because of this logic. When a menu item is blurred, the menu will close unless there is a newly focused element inside of the menu.
https://github.com/davidtheclark/react-aria-menubutton/blob/master/src/createManager.js#L106
function handleBlur() {
const self = this;
self.blurTimer = setTimeout(function() {
const buttonNode = ReactDOM.findDOMNode(self.button);
const menuNode = ReactDOM.findDOMNode(self.menu);
const activeEl = buttonNode.ownerDocument.activeElement;
if (buttonNode && activeEl === buttonNode) return;
if (menuNode && menuNode.contains(activeEl)) return;
if (self.isOpen) self.closeMenu({ focusButton: false });
}, 0);
}
One solution is to listen for click events on document in createManager.js, and save a reference to the target as lastClickedElement
. Then inside of this blur handler, we could check if the lastClickedElement
is inside of the menu.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Close javascript menu when click on item or lost focus
Here's what I've found. What I'm trying to do is modify it so that the menu closes after you click on an item,...
Read more >A CSS Approach to Trap Focus Inside of an Element
To summarize, when inside a dialog, pressing Tab or Shift+Tab should cycle the focus within the dialog only—amongst the focusable elements ...
Read more >Control focus with tabindex - web.dev
To focus an element, press the Tab key or call the element's focus() method. HTML; CSS. Result; Skip Results Iframe.
Read more >Catching the blur event on an element and its children
When user is tabbing between these menu items, blur event is triggered every time on the parent, followed by the focus event on...
Read more >How and when to use the tabindex attribute - bitsofcode
Modal containers are typically unfocusable elements like <div> or <section> . When a modal window is open, we may want to move focus...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Tentative answer: it goes back where is was, unless the newly focused item is an input/select/textarea. That would allow having a text input to filter the visible items, on top of the items list, in the menu.
I will PR this change, thank you!