fix(contextual-menu): route Tab into the menu when opened with the mouse - #1418
Open
guillaume-flambard wants to merge 1 commit into
Open
Conversation
The dropdown renders through a portal, so when the menu was opened with the mouse the menu items sat after every other page element in tab order: pressing Tab walked the page instead of entering the menu. Keyboard-opened menus already moved focus to the first item. When the menu is open and Tab is pressed while focus is still on the toggle, focus now moves to the first menu item. Tab from anywhere else is untouched, mouse behaviour is unchanged (no autofocus on click), and the existing wrap-around behaviour is preserved. Signed-off-by: Guillaume Flambard <g.flambard@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Done
Fixes #1363, following the smaller-step direction suggested in the issue thread by @edlerd: focus the first item when Tab is pressed while the menu is open and no menu item is focused yet.
ContextualMenuDropdownrenders through a portal, so after opening the menu with the mouse the menu items sit after every other page element in tab order: pressing Tab walked the rest of the page instead of entering the menu. Keyboard-opened menus already moved focus to the first item (they satisfy thedetail === 0gate), which is why the bug only reproduces after a mouse click.activeElementbeing the toggle).does not autofocus when opened by a mousetest still passes).The full ARIA menu pattern discussed in the issue (arrow-key navigation, roving tabindex) can build on this as a follow-up.
QA
Pinging @canonical/react-library-maintainers for a review.
Storybook
yarn start, open the ContextualMenu > Toggle story.Tests
yarn jest src/components/ContextualMenu:New tests:
routes Tab into the menu when it was opened by a mouse(fails before the fix, passes after) andleaves Tab alone when focus is not on the toggle(guards against over-eager routing).eslintandtsc --noEmitclean.