Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions packages/@react-aria/menu/src/useSubmenuTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {AriaMenuItemProps} from './useMenuItem';
import {AriaMenuOptions} from './useMenu';
import type {AriaPopoverProps, OverlayProps} from '@react-aria/overlays';
import {FocusableElement, FocusStrategy, KeyboardEvent, Node, PressEvent, RefObject} from '@react-types/shared';
import {focusWithoutScrolling, useEffectEvent, useId, useLayoutEffect} from '@react-aria/utils';
import {focusWithoutScrolling, useEffectEvent, useEvent, useId, useLayoutEffect} from '@react-aria/utils';
import type {SubmenuTriggerState} from '@react-stately/menu';
import {useCallback, useRef} from 'react';
import {useLocale} from '@react-aria/i18n';
Expand Down Expand Up @@ -223,11 +223,13 @@ export function useSubmenuTrigger<T>(props: AriaSubmenuTriggerProps, state: Subm
}
};

let onBlur = (e) => {
if (state.isOpen && (parentMenuRef.current?.contains(e.relatedTarget))) {
useEvent(parentMenuRef, 'focusin', (e) => {
// If we detect focus moved to a different item in the same menu that the currently open submenu trigger is in
// then close the submenu. This is for a case where the user hovers a root menu item when multiple submenus are open
if (state.isOpen && (parentMenuRef.current?.contains(e.target as HTMLElement) && e.target !== ref.current)) {
onSubmenuClose();
}
};
});

let shouldCloseOnInteractOutside = (target) => {
if (target !== ref.current) {
Expand All @@ -249,7 +251,6 @@ export function useSubmenuTrigger<T>(props: AriaSubmenuTriggerProps, state: Subm
onPress,
onHoverChange,
onKeyDown: submenuTriggerKeyDown,
onBlur,
isOpen: state.isOpen
},
submenuProps,
Expand Down
6 changes: 4 additions & 2 deletions packages/react-aria-components/src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ export const SubmenuTrigger = /*#__PURE__*/ createBranchComponent('submenutrigg
<Provider
values={[
[MenuItemContext, {...submenuTriggerProps, onAction: undefined, ref: itemRef}],
[MenuContext, submenuProps],
[MenuContext, {
ref: submenuRef,
Copy link
Member

@snowystinger snowystinger Aug 5, 2025

Choose a reason for hiding this comment

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

i assume this was just correcting a mistake? or was this necessary so we don't include submenus in the parentMenuRef check?

Copy link
Member Author

Choose a reason for hiding this comment

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

just something we forgot to update I believe when we moved to automatically making these menus wrapped in dialogs. This resulted in the ref being passed to useSubmenuTrigger resolving to the dialog instead of the submenu, thus meaning ArrowRight wouldnt cause focus to move to the first menu item since we weren't actually focusing the menu.

...submenuProps
}],
Comment on lines +134 to +137
Copy link
Member Author

@LFDanLu LFDanLu Aug 1, 2025

Choose a reason for hiding this comment

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

Fixes ArrowRight moving focus into the SubMenu after hovering it. Might cause iOS VO to close the submenu prematurely when focusing the dismiss button

EDIT: seems ok on my iPad but that is only on 17 since it is too old

[OverlayTriggerStateContext, submenuTriggerState],
[PopoverContext, {
ref: submenuRef,
trigger: 'SubmenuTrigger',
triggerRef: itemRef,
placement: 'end top',
Expand Down
5 changes: 3 additions & 2 deletions packages/react-aria-components/src/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,12 @@ function PopoverInner({state, isExiting, UNSTABLE_portalContainer, clearContexts
}, [ref, shouldBeDialog]);

// Focus the popover itself on mount, unless a child element is already focused.
// Skip this for submenus since hovering a submenutrigger should keep focus on the trigger
useEffect(() => {
if (isDialog && ref.current && !ref.current.contains(document.activeElement)) {
if (isDialog && props.trigger !== 'SubmenuTrigger' && ref.current && !ref.current.contains(document.activeElement)) {
Copy link
Member

Choose a reason for hiding this comment

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

:-/
I see we use this check in a few places though, so I'm ok with it. at some point we'll need to make this a more generic/defined behaviour check

Copy link
Member Author

Choose a reason for hiding this comment

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

a bit unfortunate for sure

focusSafely(ref.current);
}
}, [isDialog, ref]);
}, [isDialog, ref, props.trigger]);

let children = useMemo(() => {
let children = renderProps.children;
Expand Down
37 changes: 37 additions & 0 deletions packages/react-aria-components/test/AriaMenu.test-util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,43 @@ export const AriaMenuTests = ({renderers, setup, prefix}: AriaMenuTestProps): vo
expect(nestedSubmenu).not.toBeInTheDocument();
expect(document.activeElement).toBe(nestedSubmenuTrigger);
});

it('should close the submenu if another item in the same menu is focused', async () => {
let tree = (renderers.submenus!)();
let menuTester = testUtilUser.createTester('Menu', {user, root: tree.container});
await menuTester.open();
let menu = menuTester.menu;
let submenuTrigger = menuTester.submenuTriggers[0];
let submenuUtil = (await menuTester.openSubmenu({submenuTrigger}))!;
act(() => {jest.runAllTimers();});
let submenu = submenuUtil.menu;
expect(submenu).toBeInTheDocument();
let nestedSubmenuTrigger = submenuUtil.submenuTriggers[0];
let nestedSubmenuUtil = (await submenuUtil.openSubmenu({submenuTrigger: nestedSubmenuTrigger}))!;
act(() => {jest.runAllTimers();});
let nestedSubmenu = nestedSubmenuUtil.menu;
expect(submenu).toBeInTheDocument();
await user.hover(menuTester.options()[0]);
act(() => {jest.runAllTimers();});
expect(nestedSubmenu).not.toBeInTheDocument();
expect(submenu).not.toBeInTheDocument();
expect(menu).toBeInTheDocument();
});

it('should retain focus on the submenu trigger when hovering it', async () => {
let tree = (renderers.submenus!)();
let menuTester = testUtilUser.createTester('Menu', {user, root: tree.container});
await menuTester.open();
await user.hover(menuTester.submenuTriggers[0]);
act(() => {jest.runAllTimers();});
expect(menuTester.submenuTriggers[0]).toHaveAttribute('aria-expanded', 'true');
expect(document.activeElement).toBe(menuTester.submenuTriggers[0]);

// It should also allow the user to move focus into the submenu via ArrowRight
await user.keyboard('{ArrowRight}');
let submenu = tree.getAllByRole('menu')[1];
expect(document.activeElement).toBe(within(submenu).getAllByRole('menuitem')[0]);
});
});
}

Expand Down