Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close Menu component when using tab key #1673

Merged
merged 4 commits into from Jul 14, 2022
Merged

Close Menu component when using tab key #1673

merged 4 commits into from Jul 14, 2022

Conversation

RobinMalfait
Copy link
Collaborator

This is a bit of an odd one, we always used to focus trap when the Menu component is open and I'm
pretty sure this was also recommend on some pages.

Looking at the new ARIA Authoring Practices Guide (APG) pages this is now recommended https://www.w3.org/WAI/ARIA/apg/patterns/menu/

When focus is on a menuitem in a menu or menubar, move focus out of the menu or menubar, and close all menus and submenus.

This is also what was mentioned by @eriese on the original issue #1634 and #1639

This PR is a continuation of #1639 with a few more improvements and cleanup, but I started from the
original PR just so that the original work from @eriese is includes (Thanks for starting this!)

I also did some digging around the internet and other libraries implement it either using focus
trapping or with the new proper "move focus out" code.

Closes: #1639
Fixes: #1634

eriese and others added 3 commits July 14, 2022 20:56
This is internal API, and the actual API is not 100% ideal. I started
refactoring this in a separate branch but it got out of hand and touches
a bit more pieces of the codebase that aren't related to this PR at all.

The idea of this function is just so that we can go Next/Previous but
from the given element not from the document.activeElement. This is
important for this feature. We also bolted this ontop of the existing
code which now means that we have this API:

```js
focusIn([], Focus.Previouw, true, DOMNode)
```

Luckily it's internal API only!
Just closing the Menu isn't 100% enough. If we do this, it means that
when the Menu is open, we press shift+tab, then we go to the
Menu.Button because the Menu.Items were the active element.

The other way is also incorrect because it can happen if you have an
`<a>` element as one of the Menu.Item elements then that `<a>` will
receive focus, then the `Menu` will close unmounting the focused `<a>`
and now that element is gone resulting in `document.body` being the
active element.

To fix this, we will make sure that we consider the `Menu` as 1 coherent
component. This means that using `<Tab>` will now go to the next element
after the `<Menu.Button>` once the Menu is closed.

Shift+Tab will go to the element before the `<Menu.Button>` even though
you are currently focused on the `Menu.Items` so depending on the timing
you go to the `Menu.Button` or not.

Considering the Menu as a single component it makes more sense use the
elements before / after the `Menu`
@vercel
Copy link

vercel bot commented Jul 14, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
headlessui-react ✅ Ready (Inspect) Visit Preview Jul 14, 2022 at 7:56PM (UTC)
headlessui-vue ✅ Ready (Inspect) Visit Preview Jul 14, 2022 at 7:56PM (UTC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

React Menu component doesn't allow tabbing away
2 participants