-
Notifications
You must be signed in to change notification settings - Fork 15
fix(container-menu): improve menu dropdown expansion and focus behavior on blur #691
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5d90d78
fix(container-menu): improve menu expansion and focus behavior on blur
ze-flo 6bccd75
fix(useMenu): restore natural tab order
ze-flo b586c41
fix(useMenu): return focus to trigger once on outside menu click
ze-flo fcb4579
refactor(useMenu): restore previous behavior on TAB keydown
ze-flo 16da5cf
chore: fix typo
ze-flo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -375,16 +375,38 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
| [closeMenu, returnFocusToTrigger] | ||
| ); | ||
|
|
||
| /** | ||
| * 1. When an element loses focus (on blur) and focus shifts to the `body` or | ||
| * a non-focusable element, `event.relatedTarget` should be `null`. | ||
| * However, jsdom incorrectly sets it to `Document`. | ||
| * This bug was fixed in [email protected]. Currently, `jest-environment-jsdom` (v29.7.0) | ||
| * depends on [email protected], which still contains the issue. | ||
| * Until a newer version is released, this simple workaround | ||
| * is necessary to accurately test the "focus return" behavior. | ||
| * @see https://github.com/jsdom/jsdom/pull/3767 | ||
| * @see https://github.com/jsdom/jsdom/releases/tag/24.1.2 | ||
| * @see https://github.com/jestjs/jest/blob/v29.7.0/packages/jest-environment-jsdom/package.json | ||
| */ | ||
| const handleMenuBlur = useCallback( | ||
| (event: MouseEvent) => { | ||
| const path = event.composedPath(); | ||
|
|
||
| if (!path.includes(menuRef.current!) && !path.includes(triggerRef.current!)) { | ||
| returnFocusToTrigger(); | ||
| closeMenu(StateChangeTypes.MenuBlur); | ||
| } | ||
| (event: React.FocusEvent) => { | ||
| const win = environment || window; | ||
|
|
||
| setTimeout(() => { | ||
| // Timeout is required to ensure blur is handled after focus | ||
| const activeElement = win.document.activeElement; | ||
|
|
||
| if ( | ||
| !menuRef.current?.contains(activeElement) && | ||
| !triggerRef.current?.contains(activeElement) | ||
| ) { | ||
| // Only return focus to trigger if focus moved to a non-focusable element | ||
| if (!event.relatedTarget || event.relatedTarget?.nodeName === '#document' /* [1] */) | ||
| returnFocusToTrigger(); | ||
| closeMenu(StateChangeTypes.MenuBlur); | ||
| } | ||
| }); | ||
| }, | ||
| [closeMenu, menuRef, returnFocusToTrigger, triggerRef] | ||
| [environment, menuRef, triggerRef, returnFocusToTrigger, closeMenu] | ||
| ); | ||
|
|
||
| const handleMenuMouseLeave = useCallback(() => { | ||
|
|
@@ -602,15 +624,12 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
| const win = environment || window; | ||
|
|
||
| if (controlledIsExpanded) { | ||
| win.document.addEventListener('click', handleMenuBlur, true); | ||
| win.document.addEventListener('keydown', handleMenuKeyDown, true); | ||
| } else if (!controlledIsExpanded) { | ||
| win.document.removeEventListener('click', handleMenuBlur, true); | ||
| win.document.removeEventListener('keydown', handleMenuKeyDown, true); | ||
| } | ||
|
|
||
| return () => { | ||
| win.document.removeEventListener('click', handleMenuBlur, true); | ||
| win.document.removeEventListener('keydown', handleMenuKeyDown, true); | ||
| }; | ||
| }, [controlledIsExpanded, handleMenuBlur, handleMenuKeyDown, environment]); | ||
|
|
@@ -690,7 +709,15 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
| */ | ||
|
|
||
| const getTriggerProps = useCallback<IUseMenuReturnValue['getTriggerProps']>( | ||
| ({ onClick, onKeyDown, type = 'button', role = 'button', disabled, ...other } = {}) => ({ | ||
| ({ | ||
| onClick, | ||
| onKeyDown, | ||
| onBlur, | ||
| type = 'button', | ||
| role = 'button', | ||
| disabled, | ||
| ...other | ||
| } = {}) => ({ | ||
| ...other, | ||
| 'data-garden-container-id': 'containers.menu.trigger', | ||
| 'data-garden-container-version': PACKAGE_VERSION, | ||
|
|
@@ -703,13 +730,21 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
| type: type === null ? undefined : type, | ||
| role: role === null ? undefined : role, | ||
| onKeyDown: composeEventHandlers(onKeyDown, handleTriggerKeyDown), | ||
| onClick: composeEventHandlers(onClick, handleTriggerClick) | ||
| onClick: composeEventHandlers(onClick, handleTriggerClick), | ||
| onBlur: composeEventHandlers(onBlur, handleMenuBlur) | ||
| }), | ||
| [triggerRef, controlledIsExpanded, handleTriggerClick, handleTriggerKeyDown, triggerId] | ||
| [ | ||
| triggerRef, | ||
| triggerId, | ||
| controlledIsExpanded, | ||
| handleTriggerKeyDown, | ||
| handleTriggerClick, | ||
| handleMenuBlur | ||
| ] | ||
| ); | ||
|
|
||
| const getMenuProps = useCallback<IUseMenuReturnValue['getMenuProps']>( | ||
| ({ role = 'menu', onMouseLeave, ...other } = {}) => ({ | ||
| ({ role = 'menu', onBlur, onMouseLeave, ...other } = {}) => ({ | ||
| ...other, | ||
| ...getGroupProps({ | ||
| onMouseLeave: composeEventHandlers(onMouseLeave, handleMenuMouseLeave) | ||
|
|
@@ -719,9 +754,10 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
| 'aria-labelledby': triggerId, | ||
| tabIndex: -1, | ||
| role: role === null ? undefined : role, | ||
| ref: menuRef as any | ||
| ref: menuRef as any, | ||
| onBlur: composeEventHandlers(onBlur, handleMenuBlur) | ||
| }), | ||
| [triggerId, menuRef, getGroupProps, handleMenuMouseLeave] | ||
| [getGroupProps, handleMenuMouseLeave, triggerId, menuRef, handleMenuBlur] | ||
| ); | ||
|
|
||
| const getSeparatorProps = useCallback<IUseMenuReturnValue['getSeparatorProps']>( | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.