Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(dropdown-menu): follow input modality for initial focus so pointer opens don't highlight the first item (#4477) (#4594)

Jiun Shin6123d394e32e
diff --git a/.changeset/dropdown-menu-pointer-open-focus.md b/.changeset/dropdown-menu-pointer-open-focus.md
new file mode 100644
index 0000000000..f7c01d8e14
--- /dev/null
+++ b/.changeset/dropdown-menu-pointer-open-focus.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] DropdownMenu/MoreMenu: opening with a pointer no longer highlights the first item as if it were selected (#4477). Initial focus now follows the input modality: keyboard opens (Enter/Space/ArrowDown on the trigger) still focus the first enabled item per the APG menu-button pattern, while pointer opens focus the menu container itself so the first ArrowDown moves to item 1. Synthesized clicks (detail 0, e.g. screen reader activation) and programmatic controlled opens keep the first-item focus behavior. Covers data-driven items mode, compound mode, and MoreMenu, which share the open path.
+@jiunshinn
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
index 225d8e4e09..531edc8d2e 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
@@ -10,8 +10,9 @@
*/
import {describe, it, expect, vi, beforeEach} from 'vitest';
-import {render, screen, fireEvent} from '@testing-library/react';
+import {render, screen, fireEvent, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
+import {useState} from 'react';
import {DropdownMenu} from './DropdownMenu';
import {DropdownMenuItem} from './DropdownMenuItem';
import {Divider} from '../Divider';
@@ -214,9 +215,8 @@ describe('DropdownMenu', () => {
const trigger = screen.getByRole('button', {name: /Actions/});
trigger.focus();
await user.click(trigger);
- expect(
- screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
- ).toHaveFocus();
+ // Pointer opens focus the menu container, not the first item (#4477).
+ expect(screen.getByRole('menu', {hidden: true})).toHaveFocus();
const popoverEl = screen
.getByRole('menu', {hidden: true})
@@ -1057,3 +1057,192 @@ describe('DropdownMenu keyboard access for menuitemradio/menuitemcheckbox (#3829
expect(edit).toHaveFocus();
});
});
+
+describe('DropdownMenu open focus follows input modality (#4477)', () => {
+ const items = [{label: 'Edit'}, {label: 'Duplicate'}, {label: 'Delete'}];
+
+ it('pointer open focuses the menu container, not the first item (items mode)', async () => {
+ const user = userEvent.setup();
+ render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
+
+ await user.click(screen.getByRole('button', {name: /Actions/}));
+
+ const menu = screen.getByRole('menu', {hidden: true});
+ await waitFor(() => expect(menu).toHaveFocus());
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).not.toHaveFocus();
+ });
+
+ it('pointer open focuses the menu container in compound mode', async () => {
+ const user = userEvent.setup();
+ render(
+ <DropdownMenu button={{label: 'Actions'}}>
+ <DropdownMenuItem label="Edit" onClick={() => {}} />
+ <DropdownMenuItem label="Delete" onClick={() => {}} />
+ </DropdownMenu>,
+ );
+
+ await user.click(screen.getByRole('button', {name: /Actions/}));
+
+ const menu = screen.getByRole('menu', {hidden: true});
+ await waitFor(() => expect(menu).toHaveFocus());
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).not.toHaveFocus();
+ });
+
+ it('first ArrowDown after a pointer open moves focus to the first enabled item', async () => {
+ const user = userEvent.setup();
+ render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
+
+ await user.click(screen.getByRole('button', {name: /Actions/}));
+ const menu = screen.getByRole('menu', {hidden: true});
+ await waitFor(() => expect(menu).toHaveFocus());
+
+ fireEvent.keyDown(menu, {key: 'ArrowDown'});
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).toHaveFocus();
+ });
+
+ it('ArrowDown after a pointer open skips a disabled leading item', async () => {
+ const user = userEvent.setup();
+ render(
+ <DropdownMenu
+ button={{label: 'Actions'}}
+ items={[{label: 'Edit', isDisabled: true}, {label: 'Delete'}]}
+ />,
+ );
+
+ await user.click(screen.getByRole('button', {name: /Actions/}));
+ const menu = screen.getByRole('menu', {hidden: true});
+ await waitFor(() => expect(menu).toHaveFocus());
+
+ fireEvent.keyDown(menu, {key: 'ArrowDown'});
+ expect(
+ screen.getByRole('menuitem', {name: 'Delete', hidden: true}),
+ ).toHaveFocus();
+ });
+
+ it('keyboard open via Enter focuses the first enabled item', async () => {
+ const user = userEvent.setup();
+ render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
+
+ screen.getByRole('button', {name: /Actions/}).focus();
+ await user.keyboard('{Enter}');
+
+ await waitFor(() =>
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).toHaveFocus(),
+ );
+ });
+
+ it('keyboard open via ArrowDown skips a disabled leading item', async () => {
+ const user = userEvent.setup();
+ render(
+ <DropdownMenu
+ button={{label: 'Actions'}}
+ items={[{label: 'Edit', isDisabled: true}, {label: 'Delete'}]}
+ />,
+ );
+
+ screen.getByRole('button', {name: /Actions/}).focus();
+ await user.keyboard('{ArrowDown}');
+
+ await waitFor(() =>
+ expect(
+ screen.getByRole('menuitem', {name: 'Delete', hidden: true}),
+ ).toHaveFocus(),
+ );
+ });
+
+ it('a synthesized click (detail 0, AT activation) still focuses the first item', async () => {
+ render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
+
+ // fireEvent.click dispatches a MouseEvent with detail 0 (the shape of a
+ // screen reader / AT activation), so it must keep the keyboard behavior.
+ fireEvent.click(screen.getByRole('button', {name: /Actions/}));
+
+ await waitFor(() =>
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).toHaveFocus(),
+ );
+ });
+
+ it('controlled pointer open focuses the menu container', async () => {
+ function Controlled() {
+ const [isOpen, setIsOpen] = useState(false);
+ return (
+ <DropdownMenu
+ button={{label: 'Actions'}}
+ items={items}
+ isMenuOpen={isOpen}
+ onOpenChange={setIsOpen}
+ />
+ );
+ }
+ const user = userEvent.setup();
+ render(<Controlled />);
+
+ await user.click(screen.getByRole('button', {name: /Actions/}));
+
+ const menu = screen.getByRole('menu', {hidden: true});
+ await waitFor(() => expect(menu).toHaveFocus());
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).not.toHaveFocus();
+ });
+
+ it('programmatic controlled open still focuses the first item', async () => {
+ const {rerender} = render(
+ <DropdownMenu
+ button={{label: 'Actions'}}
+ items={items}
+ isMenuOpen={false}
+ onOpenChange={() => {}}
+ />,
+ );
+
+ rerender(
+ <DropdownMenu
+ button={{label: 'Actions'}}
+ items={items}
+ isMenuOpen={true}
+ onOpenChange={() => {}}
+ />,
+ );
+
+ await waitFor(() =>
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).toHaveFocus(),
+ );
+ });
+
+ it('Escape still closes the menu after a pointer open', async () => {
+ const user = userEvent.setup();
+ render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
+
+ await user.click(screen.getByRole('button', {name: /Actions/}));
+ const menu = screen.getByRole('menu', {hidden: true});
+ await waitFor(() => expect(menu).toHaveFocus());
+
+ fireEvent.keyDown(menu, {key: 'Escape'});
+ expect(HTMLElement.prototype.hidePopover).toHaveBeenCalled();
+ });
+
+ it('Tab still closes the menu after a pointer open (APG menu-button)', async () => {
+ const user = userEvent.setup();
+ render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
+
+ await user.click(screen.getByRole('button', {name: /Actions/}));
+ const menu = screen.getByRole('menu', {hidden: true});
+ await waitFor(() => expect(menu).toHaveFocus());
+
+ fireEvent.keyDown(menu, {key: 'Tab'});
+ expect(HTMLElement.prototype.hidePopover).toHaveBeenCalled();
+ });
+});
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsx
index bbc55070c1..c79adb9df6 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx
@@ -14,6 +14,11 @@
*
* Both modes use useListFocus for DOM-based keyboard navigation.
*
+ * Initial focus on open follows the input modality: a keyboard open
+ * (Enter / Space / ArrowDown on the trigger) focuses the first enabled item
+ * (APG menu-button); a pointer open focuses the menu container itself so no
+ * item reads as pre-selected, and the first ArrowDown then moves to item 1.
+ *
* SYNC: When modified, update these files to stay in sync:
* - /packages/core/src/DropdownMenu/DropdownMenu.doc.mjs
* - /packages/core/src/DropdownMenu/DropdownMenu.test.tsx
@@ -266,6 +271,13 @@ export function DropdownMenu({
// captures the trigger instead of the first menu item.
const shouldFocusOnOpenRef = useRef(false);
+ // How the next open was initiated. Keyboard (and programmatic) opens focus
+ // the first enabled item per the APG menu-button pattern; pointer opens
+ // focus the menu container instead, so no item is visually highlighted as
+ // if pre-selected (#4477). Reset to 'keyboard' after every open so
+ // programmatic controlled opens keep the item-focus behavior.
+ const openModalityRef = useRef<'keyboard' | 'pointer'>('keyboard');
+
const handleLayerShow = useCallback(() => {
onOpenChange?.(true);
if (!isControlled) {
@@ -331,14 +343,26 @@ export function DropdownMenu({
}
}, [controlledIsOpen, isControlled, popover]);
- // Move focus into the menu only after the layer has committed open.
+ // Move focus into the menu only after the layer has committed open,
+ // honoring the input modality: keyboard (and programmatic) opens land on
+ // the first enabled item per the APG menu-button pattern; pointer opens
+ // focus the menu container itself (tabIndex={-1}) so no item is
+ // highlighted as if pre-selected (#4477). Container focus keeps arrows,
+ // typeahead, Escape and Tab in the menu's onKeyDown, and is also the
+ // fallback when no item is focusable (e.g. all disabled), mirroring the
+ // submenu flyout fallback.
useEffect(() => {
if (!popover.isOpen || !shouldFocusOnOpenRef.current) {
return;
}
shouldFocusOnOpenRef.current = false;
- requestAnimationFrame(() => focusFirst());
- }, [popover.isOpen, focusFirst]);
+ requestAnimationFrame(() => {
+ if (openModalityRef.current === 'pointer' || !focusFirst()) {
+ listRef.current?.focus();
+ }
+ openModalityRef.current = 'keyboard';
+ });
+ }, [popover.isOpen, focusFirst, listRef]);
// Extend useListFocus with Enter/Space activation + typeahead
const listKeyDown = useCallback(
@@ -378,36 +402,51 @@ export function DropdownMenu({
[listNavKeyDown, closeMenu, typeahead, ownsEvent],
);
- const openAndFocus = useCallback(() => {
- shouldFocusOnOpenRef.current = true;
- popover.show();
- }, [popover]);
+ const openAndFocus = useCallback(
+ (modality: 'keyboard' | 'pointer' = 'keyboard') => {
+ openModalityRef.current = modality;
+ shouldFocusOnOpenRef.current = true;
+ popover.show();
+ },
+ [popover],
+ );
- const handleButtonClick = useCallback(() => {
- // If the menu was just closed by light dismiss (e.g. iOS Safari fires
- // pointerdown → hide before the trigger's click), the click would
- // otherwise immediately re-open it. Short-circuit within the guard window.
- if (Date.now() - lastHideTimeRef.current < 50) {
- return;
- }
- onClick?.();
- if (isControlled) {
- onOpenChange?.(!controlledIsOpen);
- } else {
- if (popover.isOpen) {
- popover.hide();
+ const handleButtonClick = useCallback(
+ (e: React.MouseEvent<HTMLButtonElement>) => {
+ // If the menu was just closed by light dismiss (e.g. iOS Safari fires
+ // pointerdown → hide before the trigger's click), the click would
+ // otherwise immediately re-open it. Short-circuit within the guard
+ // window.
+ if (Date.now() - lastHideTimeRef.current < 50) {
+ return;
+ }
+ onClick?.();
+ // detail === 0 marks a synthesized click (screen reader / AT
+ // activation): treat it as keyboard so those users still land on the
+ // first item. Real pointer clicks report detail >= 1.
+ const modality = e.detail === 0 ? 'keyboard' : 'pointer';
+ if (isControlled) {
+ if (!controlledIsOpen) {
+ openModalityRef.current = modality;
+ }
+ onOpenChange?.(!controlledIsOpen);
} else {
- openAndFocus();
+ if (popover.isOpen) {
+ popover.hide();
+ } else {
+ openAndFocus(modality);
+ }
}
- }
- }, [
- onClick,
- isControlled,
- onOpenChange,
- controlledIsOpen,
- popover,
- openAndFocus,
- ]);
+ },
+ [
+ onClick,
+ isControlled,
+ onOpenChange,
+ controlledIsOpen,
+ popover,
+ openAndFocus,
+ ],
+ );
const handleButtonKeyDown = useCallback(
(e: React.KeyboardEvent) => {
@@ -480,6 +519,11 @@ export function DropdownMenu({
ref={listRef}
id={menuId}
role="menu"
+ // Focus target for pointer opens (not in the Tab order): holding
+ // focus on the container keeps key events (arrows, typeahead,
+ // Escape, Tab) inside the menu without highlighting any item.
+ // Mirrors the DropdownMenuSubMenu flyout container.
+ tabIndex={-1}
// Give the menu an accessible name from its trigger's label, so
// screen readers announce e.g. "Actions menu" rather than an unnamed
// menu (menus-13).
diff --git a/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx
index 23a2ace15b..e84168c4c0 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx
@@ -89,12 +89,12 @@ describe('DropdownMenuSubMenu', () => {
name: /Move to/,
hidden: true,
});
- // The menu focuses its first item (Rename) on open via rAF; wait for that
- // to settle before moving focus to the submenu trigger, so the deferred
- // focus can't steal it back mid-test.
+ // A pointer open focuses the menu container via rAF (#4477); wait for
+ // that to settle before moving focus to the submenu trigger, so the
+ // deferred focus can't steal it back mid-test.
await waitFor(() => {
expect(
- screen.getByRole('menuitem', {name: 'Rename', hidden: true}),
+ screen.getByRole('menu', {name: 'Actions', hidden: true}),
).toHaveFocus();
});
trigger.focus();
@@ -176,11 +176,12 @@ describe('DropdownMenuSubMenu', () => {
name: /Move to/,
hidden: true,
});
- // Let the root menu's open-focus (Rename, via rAF) settle before moving
- // focus to the submenu trigger, so it can't steal focus back mid-test.
+ // Let the root menu's pointer-open focus (the container, via rAF) settle
+ // before moving focus to the submenu trigger, so it can't steal focus
+ // back mid-test.
await waitFor(() => {
expect(
- screen.getByRole('menuitem', {name: 'Rename', hidden: true}),
+ screen.getByRole('menu', {name: 'Actions', hidden: true}),
).toHaveFocus();
});
trigger.focus();
@@ -235,6 +236,13 @@ describe('DropdownMenuSubMenu', () => {
name: /Move to/,
hidden: true,
});
+ // Let the pointer-open container focus (rAF, #4477) settle so it can't
+ // steal focus from the manually focused trigger mid-test.
+ await waitFor(() => {
+ expect(
+ screen.getByRole('menu', {name: 'Actions', hidden: true}),
+ ).toHaveFocus();
+ });
trigger.focus();
await user.keyboard('{ArrowRight}');
// Opens and focuses the first item.
@@ -334,13 +342,20 @@ describe('DropdownMenu data-driven submenus', () => {
/>,
);
await user.click(screen.getByRole('button', {name: /Actions/}));
+ // Pointer open focuses the container (#4477); the first ArrowDown then
+ // moves onto Rename.
+ await waitFor(() => {
+ expect(
+ screen.getByRole('menu', {name: 'Actions', hidden: true}),
+ ).toHaveFocus();
+ });
+ await user.keyboard('{ArrowDown}');
await waitFor(() => {
expect(
screen.getByRole('menuitem', {name: 'Rename', hidden: true}),
).toHaveFocus();
});
- // Rename → Move to → Delete (three ArrowDowns, no stalling on hidden
- // flyout items).
+ // Rename → Move to → Delete (no stalling on hidden flyout items).
await user.keyboard('{ArrowDown}');
await waitFor(() => {
expect(
@@ -372,7 +387,7 @@ describe('DropdownMenuSubMenu accessibility (WCAG 2.2 / APG)', () => {
});
await waitFor(() => {
expect(
- screen.getByRole('menuitem', {name: 'Rename', hidden: true}),
+ screen.getByRole('menu', {name: 'Actions', hidden: true}),
).toHaveFocus();
});
trigger.focus();
@@ -409,6 +424,13 @@ describe('DropdownMenuSubMenu accessibility (WCAG 2.2 / APG)', () => {
name: /Move to/,
hidden: true,
});
+ // Let the pointer-open container focus (rAF, #4477) settle so it can't
+ // steal focus from the manually focused trigger mid-test.
+ await waitFor(() => {
+ expect(
+ screen.getByRole('menu', {name: 'Actions', hidden: true}),
+ ).toHaveFocus();
+ });
trigger.focus();
await user.keyboard('{ArrowRight}');
await waitFor(() => {
@@ -463,11 +485,12 @@ describe('DropdownMenuSubMenu accessibility (WCAG 2.2 / APG)', () => {
name: /Share to/,
hidden: true,
});
- // Let the root menu's open-focus (Copy link, via rAF) settle first so it
- // can't steal focus back after we move to the submenu trigger.
+ // Let the root menu's pointer-open focus (the container, via rAF, #4477)
+ // settle first so it can't steal focus back after we move to the submenu
+ // trigger.
await waitFor(() => {
expect(
- screen.getByRole('menuitem', {name: 'Copy link', hidden: true}),
+ screen.getByRole('menu', {name: 'Share', hidden: true}),
).toHaveFocus();
});
shareTo.focus();
@@ -584,7 +607,7 @@ describe('DropdownMenuSubMenu accessibility (WCAG 2.2 / APG)', () => {
});
await waitFor(() => {
expect(
- screen.getByRole('menuitem', {name: 'Rename', hidden: true}),
+ screen.getByRole('menu', {name: 'Actions', hidden: true}),
).toHaveFocus();
});
trigger.focus();
@@ -650,6 +673,13 @@ describe('DropdownMenuSubMenu accessibility (WCAG 2.2 / APG)', () => {
name: /Move to/,
hidden: true,
});
+ // Let the pointer-open container focus (rAF, #4477) settle so it can't
+ // steal focus from the manually focused trigger mid-test.
+ await waitFor(() => {
+ expect(
+ screen.getByRole('menu', {name: 'Actions', hidden: true}),
+ ).toHaveFocus();
+ });
trigger.focus();
await user.keyboard('{ArrowRight}');
// Children resolve on open; ArrowDown moves from the container to Folder A.
diff --git a/packages/core/src/MoreMenu/MoreMenu.test.tsx b/packages/core/src/MoreMenu/MoreMenu.test.tsx
index e20a7e30d7..9d5fc470ae 100644
--- a/packages/core/src/MoreMenu/MoreMenu.test.tsx
+++ b/packages/core/src/MoreMenu/MoreMenu.test.tsx
@@ -10,7 +10,7 @@
*/
import {describe, it, expect, vi, beforeEach} from 'vitest';
-import {render, screen} from '@testing-library/react';
+import {render, screen, fireEvent, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {MoreMenu} from './MoreMenu';
@@ -197,4 +197,31 @@ describe('MoreMenu', () => {
const menu = screen.getByRole('menu', {hidden: true});
expect(menu.className).toContain('astryx-more-menu');
});
+
+ it('pointer open focuses the menu container, not the first item (#4477)', async () => {
+ const user = userEvent.setup();
+ render(<MoreMenu items={defaultItems} />);
+
+ await user.click(screen.getByRole('button', {name: 'More options'}));
+
+ const menu = screen.getByRole('menu', {hidden: true});
+ await waitFor(() => expect(menu).toHaveFocus());
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).not.toHaveFocus();
+ });
+
+ it('first ArrowDown after a pointer open moves focus to the first item (#4477)', async () => {
+ const user = userEvent.setup();
+ render(<MoreMenu items={defaultItems} />);
+
+ await user.click(screen.getByRole('button', {name: 'More options'}));
+ const menu = screen.getByRole('menu', {hidden: true});
+ await waitFor(() => expect(menu).toHaveFocus());
+
+ fireEvent.keyDown(menu, {key: 'ArrowDown'});
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).toHaveFocus();
+ });
});