-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
[Menu] Fixed Menu item not toggling highlighted prop when submenu popup is opened using keyboard #1285
base: master
Are you sure you want to change the base?
[Menu] Fixed Menu item not toggling highlighted prop when submenu popup is opened using keyboard #1285
Conversation
…s, allowing for dynamic updates to the active (highlighted) item index. This fixes not updating hover out behaviour of navigation for keyboard events.
Netlify deploy preview |
...getMenuItemProps({ | ||
'aria-haspopup': 'menu' as const, | ||
...externalProps, | ||
onKeyDown: (event: MenuKeyboardEvent) => { | ||
if (event.key === 'ArrowRight' && highlighted) { | ||
// Clear parent menu's highlight state when entering submenu | ||
// This prevents multiple highlighted items across menu levels | ||
setActiveIndex(null); | ||
} | ||
onKeyDown?.(event); | ||
externalProps?.onKeyDown?.(event); | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the mergeReactProps
util will work better here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, Please check.
@@ -38,11 +44,20 @@ export function useMenuSubmenuTrigger( | |||
...getMenuItemProps({ | |||
'aria-haspopup': 'menu' as const, | |||
...externalProps, | |||
onKeyDown: (event: MenuKeyboardEvent) => { | |||
if (event.key === 'ArrowRight' && highlighted) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also regular onClick
to check for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added, Please check.
…ergeReactProps to merge externalProps
/** | ||
* Callback function that is triggered on key down events. | ||
* @param event - The keyboard event that triggered the callback. | ||
*/ | ||
onKeyDown?: (event: MenuKeyboardEvent) => void; | ||
/** | ||
* Callback function that is triggered on click events. | ||
* @param event - The click event that triggered the callback. | ||
*/ | ||
onClick?: (event: React.MouseEvent) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These aren't necessary with mergeReactProps
, it will automatically call them from the external props
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed it, Its being removed in latest commit.
onKeyDown: (event: MenuKeyboardEvent) => { | ||
if (event.key === 'ArrowRight' && highlighted) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works well overall, but in rtl
mode, it's ArrowLeft
instead of ArrowRight
I wonder if it's more robust to check for focus/blur, or the submenu opening? Explicitly checking for opening actions like onClick/ArrowLeft/Right can work ok, but there's edge cases to consider since it's not as general
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, Is it fine to just check the direction and use ArrowLeft | ArrowRight
? What are the other edge cases which could potentially not respect these events ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const direction = useDirection()
is the hook for checking the direction, yep.
I'm not certain, sometimes edge cases can pop up in the future. That will likely be good enough, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree. I debugged further and found that we get the latest open state for the submenu in the useMenuRoot hook. Even when we use it in useMenuSubmenuTrigger, it only works for keyboard events, not hover ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented the direction check using useDirection and toggled the keyboard event key accordingly.
Apologies for the commit mess in this PR—it happened while pulling the latest changes and resolving conflicts. To keep things clean, I created a fresh PR here: #1310.
Please have a look at it when you get a chance, @atomiks. Thanks!
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Michał Dudak <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Signed-off-by: Olivier Tassinari <[email protected]>
Co-authored-by: Michał Dudak <[email protected]>
…ergeReactProps to merge externalProps
…com/onehanddev/base-ui into menu/submenu-highlight-keyboard-focus
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Improve Menu Navigation and Fix Highlighting Issue
Fixes #1197