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 #1310

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
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 @@ -250,6 +250,7 @@ export function useMenuRoot(parameters: useMenuRoot.Parameters): useMenuRoot.Ret
() => ({
activeIndex,
allowMouseUpTriggerRef,
setActiveIndex,
floatingRootContext,
getItemProps,
getPopupProps,
Expand All @@ -269,6 +270,7 @@ export function useMenuRoot(parameters: useMenuRoot.Parameters): useMenuRoot.Ret
}),
[
activeIndex,
setActiveIndex,
floatingRootContext,
getItemProps,
getPopupProps,
Expand Down Expand Up @@ -351,6 +353,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 @@ -4,6 +4,12 @@ import { FloatingEvents } from '@floating-ui/react';
import { useMenuItem } from '../item/useMenuItem';
import { useForkRef } from '../../utils/useForkRef';
import { GenericHTMLProps } from '../../utils/types';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { useDirection } from '../../direction-provider/DirectionContext';

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

export function useMenuSubmenuTrigger(
parameters: useSubmenuTrigger.Parameters,
Expand All @@ -17,6 +23,7 @@ export function useMenuSubmenuTrigger(
setTriggerElement,
allowMouseUpTriggerRef,
typingRef,
setActiveIndex,
} = parameters;

const { getRootProps: getMenuItemProps, rootRef: menuItemRef } = useMenuItem({
Expand All @@ -32,17 +39,28 @@ export function useMenuSubmenuTrigger(

const menuTriggerRef = useForkRef(menuItemRef, setTriggerElement);

const direction = useDirection();

const getRootProps = React.useCallback(
(externalProps?: GenericHTMLProps) => {
return {
const openKey = direction === 'rtl' ? 'ArrowLeft' : 'ArrowRight';

return mergeReactProps(externalProps, {
Copy link
Contributor

Choose a reason for hiding this comment

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

mergeReactProps<'div'>(...) will be able to type the onKeyDown event automatically

...getMenuItemProps({
'aria-haspopup': 'menu' as const,
...externalProps,
onKeyDown: (event: MenuKeyboardEvent) => {
if (event.key === openKey && highlighted) {
// Clear parent menu's highlight state when entering submenu
// This prevents multiple highlighted items across menu levels
setActiveIndex(null);
}
},
onClick: () => highlighted && setActiveIndex(null),
}),
ref: menuTriggerRef,
};
});
},
[getMenuItemProps, menuTriggerRef],
[getMenuItemProps, menuTriggerRef, highlighted, setActiveIndex, direction],
);

return React.useMemo(
Expand Down Expand Up @@ -82,6 +100,11 @@ export namespace useSubmenuTrigger {
* A ref that is set to `true` when the user is using the typeahead feature.
*/
typingRef: React.RefObject<boolean>;
/**
* 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
Loading