question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clicking an unfocusable element inside of the menu closes it

See original GitHub issue

If 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.

screen shot 2017-04-17 at 3 54 43 pm

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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gouegdcommented, Jul 23, 2017

Another problem to consider in this issue: where does focus go when you click inside the menu but not on an item? To the menu itself?

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.

1reaction
taylorcodecommented, May 2, 2017

I will PR this change, thank you!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found