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
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
b6f179b
Added setActiveIndex to the MenuRoot and MenuSubmenuTrigger component…
onehanddev Jan 5, 2025
d1aba74
Merge branch 'master' into menu/submenu-highlight-keyboard-focus
onehanddev Jan 5, 2025
8e93ab9
fix: ts errors
onehanddev Jan 5, 2025
0969a08
Handled click event as well for setting active item for menu, added m…
onehanddev Jan 6, 2025
a3c9339
addressed PR comments
onehanddev Jan 6, 2025
b511990
[ScrollArea] Read `DirectionProvider` and use logical positioning CSS…
mj12albert Jan 2, 2025
62220ba
Bump @mui/monorepo to v6.3.0 (#1252)
renovate[bot] Jan 2, 2025
436a1e4
[docs-infra] Remove Next.js production profiler (#1242)
oliviertassinari Jan 2, 2025
79da511
Bump @stefanprobst/rehype-extract-toc to ^2.2.1 (#1253)
renovate[bot] Jan 2, 2025
a7bb068
Bump @types/node to ^18.19.69 (#1254)
renovate[bot] Jan 2, 2025
b1e9b73
Bump prettier to ^3.4.2 (#1258)
renovate[bot] Jan 2, 2025
b15795d
Bump react-router-dom to ^6.28.1 (#1259)
renovate[bot] Jan 2, 2025
f7bccad
Bump shiki to ^1.25.1 (#1260)
renovate[bot] Jan 2, 2025
da29724
Bump chalk to ^5.4.1 (#1266)
renovate[bot] Jan 2, 2025
ce4bbb7
Bump framer-motion to ^11.15.0 (#1267)
renovate[bot] Jan 2, 2025
dbc3ff7
Bump github/codeql-action action to v3.28.0 (#1268)
renovate[bot] Jan 2, 2025
771c279
Bump markdownlint-cli2 to ^0.17.1 (#1269)
renovate[bot] Jan 2, 2025
d63eea4
Bump piscina to ^4.8.0 (#1271)
renovate[bot] Jan 2, 2025
36b6191
Bump pnpm to 9.15.2 (#1272)
renovate[bot] Jan 2, 2025
7ffc041
Bump stylelint to ^16.12.0 (#1273)
renovate[bot] Jan 2, 2025
f3bae72
Bump @types/prop-types to ^15.7.14 (#1255)
renovate[bot] Jan 2, 2025
09cbbf5
Bump webpack to ^5.97.1 (#1276)
renovate[bot] Jan 2, 2025
bc8450f
Bump tsx to ^4.19.2 (#1275)
renovate[bot] Jan 2, 2025
5c403b0
Bump @argos-ci/core to ^2.12.0 (#1262)
renovate[bot] Jan 2, 2025
5d7d71e
Bump Emotion to ^11.14.0 (#1264)
renovate[bot] Jan 2, 2025
7008045
Bump terser to ^5.37.0 (#1274)
renovate[bot] Jan 2, 2025
1931c76
[core] Remove tsbuildinfo from the built package (#1152)
michaldudak Jan 2, 2025
0f09c92
[core] Use stable React packages (#1113)
michaldudak Jan 2, 2025
3181e2e
Bump eslint (#1257)
renovate[bot] Jan 2, 2025
ef1e2a7
Bump babel (#1265)
renovate[bot] Jan 2, 2025
616775b
Bump terser-webpack-plugin to ^5.3.11 (#1261)
renovate[bot] Jan 2, 2025
d289505
Bump @next/mdx to ^15.1.3 (#1263)
renovate[bot] Jan 2, 2025
1ba3e4d
Bump @octokit/rest to ^21.0.2 (#1277)
renovate[bot] Jan 2, 2025
2d50a69
Bump Public packages' dependencies (#1158)
renovate[bot] Jan 2, 2025
5337c0e
Bump MUI (#1157)
renovate[bot] Jan 2, 2025
80ff4db
[useButton] Fix Enter key preventDefault when rendering links (#1251)
mj12albert Jan 2, 2025
04534a4
[docs] Add security coverage (#1150)
oliviertassinari Jan 2, 2025
7de8074
[Select] Fix highlight flash on Safari (#1233)
atomiks Jan 3, 2025
8cd3fcb
[Slider] Set `position: relative` on range slider indicator (#1175)
mj12albert Jan 3, 2025
88ad014
[test] Use vite and vitest for e2e and regression tests (#1127)
mj12albert Jan 4, 2025
30f0ef0
[docs-infra] Always document the `className` and `render` props (#1226)
mnajdova Jan 4, 2025
44f0557
fix: ts errors
onehanddev Jan 5, 2025
e1ab1b7
Handled click event as well for setting active item for menu, added m…
onehanddev Jan 6, 2025
3c394dd
addressed PR comments
onehanddev Jan 6, 2025
1e18fc8
added keyboard support for rtl direction for submenu toggling for Men…
onehanddev Jan 8, 2025
caedfec
Merge branch menu/submenu-highlight-keyboard-focus of https://github.…
onehanddev Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const testRootContext: MenuRootContext = {
getPopupProps: (p) => ({ ...p }),
getTriggerProps: (p) => ({ ...p }),
getItemProps: (p) => ({ ...p }),
setActiveIndex: () => {},
parentContext: undefined,
nested: false,
setTriggerElement: () => {},
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/menu/item/MenuItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const testRootContext: MenuRootContext = {
getPopupProps: (p) => ({ ...p }),
getTriggerProps: (p) => ({ ...p }),
getItemProps: (p) => ({ ...p }),
setActiveIndex: () => {},
parentContext: undefined,
nested: false,
setTriggerElement: () => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const testRootContext: MenuRootContext = {
getPopupProps: (p) => ({ ...p }),
getTriggerProps: (p) => ({ ...p }),
getItemProps: (p) => ({ ...p }),
setActiveIndex: () => {},
parentContext: undefined,
nested: false,
setTriggerElement: () => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const testRootContext: MenuRootContext = {
getPopupProps: (p) => ({ ...p }),
getTriggerProps: (p) => ({ ...p }),
getItemProps: (p) => ({ ...p }),
setActiveIndex: () => {},
parentContext: undefined,
nested: false,
setTriggerElement: () => {},
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/menu/root/MenuRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const MenuRoot: React.FC<MenuRoot.Props> = function MenuRoot(props) {
...menuRoot,
nested,
parentContext,
setActiveIndex: menuRoot.setActiveIndex,
disabled,
allowMouseUpTriggerRef:
parentContext?.allowMouseUpTriggerRef ?? menuRoot.allowMouseUpTriggerRef,
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/menu/root/useMenuRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export function useMenuRoot(parameters: useMenuRoot.Parameters): useMenuRoot.Ret
() => ({
activeIndex,
allowMouseUpTriggerRef,
setActiveIndex,
floatingRootContext,
getItemProps,
getPopupProps,
Expand All @@ -194,6 +195,7 @@ export function useMenuRoot(parameters: useMenuRoot.Parameters): useMenuRoot.Ret
}),
[
activeIndex,
setActiveIndex,
floatingRootContext,
getItemProps,
getPopupProps,
Expand Down Expand Up @@ -274,6 +276,7 @@ export namespace useMenuRoot {

export interface ReturnValue {
activeIndex: number | null;
setActiveIndex: (index: number | null) => void;
floatingRootContext: FloatingRootContext;
getItemProps: (externalProps?: GenericHTMLProps) => GenericHTMLProps;
getPopupProps: (externalProps?: GenericHTMLProps) => GenericHTMLProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ const MenuSubmenuTrigger = React.forwardRef(function SubmenuTriggerComponent(
throw new Error('Base UI: ItemTrigger must be placed in a nested Menu.');
}

const { activeIndex, getItemProps } = parentContext;
const { activeIndex, getItemProps, setActiveIndex } = parentContext;
const item = useCompositeListItem();

const highlighted = activeIndex === item.index;

const mergedRef = useForkRef(forwardedRef, item.ref);

const { events: menuEvents } = useFloatingTree()!;

const { getRootProps } = useMenuSubmenuTrigger({
id,
highlighted,
Expand All @@ -55,6 +54,7 @@ const MenuSubmenuTrigger = React.forwardRef(function SubmenuTriggerComponent(
setTriggerElement,
allowMouseUpTriggerRef,
typingRef,
setActiveIndex,
});

const state: MenuSubmenuTrigger.State = React.useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { useMenuItem } from '../item/useMenuItem';
import { useForkRef } from '../../utils/useForkRef';
import { GenericHTMLProps } from '../../utils/types';

type MenuKeyboardEvent = {
key: 'ArrowRight' | 'ArrowLeft' | 'ArrowUp' | 'ArrowDown';
} & React.KeyboardEvent;

export function useMenuSubmenuTrigger(
parameters: useSubmenuTrigger.Parameters,
): useSubmenuTrigger.ReturnValue {
Expand All @@ -17,6 +21,8 @@ export function useMenuSubmenuTrigger(
setTriggerElement,
allowMouseUpTriggerRef,
typingRef,
onKeyDown,
setActiveIndex,
} = parameters;

const { getRootProps: getMenuItemProps, rootRef: menuItemRef } = useMenuItem({
Expand All @@ -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.

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!

// 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.

}),
ref: menuTriggerRef,
};
},
[getMenuItemProps, menuTriggerRef],
[getMenuItemProps, menuTriggerRef, onKeyDown, highlighted, setActiveIndex],
);

return React.useMemo(
Expand Down Expand Up @@ -82,6 +97,16 @@ export namespace useSubmenuTrigger {
* A ref that is set to `true` when the user is using the typeahead feature.
*/
typingRef: React.RefObject<boolean>;
/**
* Callback function that is triggered on key down events.
* @param event - The keyboard event that triggered the callback.
*/
onKeyDown?: (event: MenuKeyboardEvent) => void;
/**
* Callback to update the active (highlighted) item index.
* Set to null to remove highlighting from all items.
*/
setActiveIndex: (index: number | null) => void;
}

export interface ReturnValue {
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/menu/trigger/MenuTrigger.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const testRootContext: MenuRootContext = {
getPopupProps: (p) => ({ ...p }),
getTriggerProps: (p) => ({ ...p }),
getItemProps: (p) => ({ ...p }),
setActiveIndex: () => {},
parentContext: undefined,
nested: false,
setTriggerElement: () => {},
Expand Down
Loading