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

[Menu] Fixed Menu item not toggling highlighted prop when submenu popup is opened using keyboard #1285

Open
wants to merge 46 commits into
base: master
Choose a base branch
from

Conversation

onehanddev
Copy link
Contributor

@onehanddev onehanddev commented Jan 5, 2025

Improve Menu Navigation and Fix Highlighting Issue

  • Fixed bug where parent expandable menu item is getting highlighted along with the its sub menu with keyboard navigation.
  • Improved navigation by resetting parent menu's highlight state when entering a submenu.

Fixes #1197

…s, allowing for dynamic updates to the active (highlighted) item index. This fixes not updating hover out behaviour of navigation for keyboard events.
@mui-bot
Copy link

mui-bot commented Jan 5, 2025

Netlify deploy preview

https://deploy-preview-1285--base-ui.netlify.app/

Generated by 🚫 dangerJS against caedfec

@onehanddev onehanddev marked this pull request as ready for review January 5, 2025 14:55
Comment on lines 44 to 55
...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);
},
Copy link
Contributor

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

Copy link
Contributor Author

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) {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, Please check.

Comment on lines 106 to 115
/**
* 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;
Copy link
Contributor

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

Copy link
Contributor Author

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.

@zannager zannager added the component: menu This is the name of the generic UI component, not the React module! label Jan 6, 2025
Comment on lines 46 to 47
onKeyDown: (event: MenuKeyboardEvent) => {
if (event.key === 'ArrowRight' && highlighted) {
Copy link
Contributor

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

Copy link
Contributor Author

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 ?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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!

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 8, 2025
mj12albert and others added 14 commits January 8, 2025 19:06
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>
renovate bot and others added 27 commits January 8, 2025 19:06
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>
Copy link

netlify bot commented Jan 8, 2025

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit caedfec
🔍 Latest deploy log https://app.netlify.com/sites/base-ui/deploys/677e8002d9f0d200082d5b1f
😎 Deploy Preview https://deploy-preview-1285--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: menu This is the name of the generic UI component, not the React module! PR: out-of-date The pull request has merge conflicts and can't be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Menu] Submenu triggers retain data-highlighted when focus has moved to submenu popup using keyboard
8 participants