An open source design system that's fully customizable and agent ready
TypeScript
fix(SideNavHeading, TopNavHeading): one arrow key moves one item in a NavHeadingMenu (#9019)
Cindy Zhang257380d6d39c
diff --git a/.changeset/nav-heading-menu-arrow-once.md b/.changeset/nav-heading-menu-arrow-once.mdnew file mode 100644index 0000000000..482a11cfa2--- /dev/null+++ b/.changeset/nav-heading-menu-arrow-once.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] In a SideNavHeading or TopNavHeading menu built with NavHeadingMenu, one ArrowDown or ArrowUp now moves focus one item. It used to move two, skipping every other item, and in a two-item menu ArrowDown never moved focus.++@cixzhangdiff --git a/packages/core/src/NavMenu/NavHeadingMenu.spec.md b/packages/core/src/NavMenu/NavHeadingMenu.spec.mdindex 23afe2aa80..c671a98559 100644--- a/packages/core/src/NavMenu/NavHeadingMenu.spec.md+++ b/packages/core/src/NavMenu/NavHeadingMenu.spec.md@@ -44,7 +44,7 @@ system_specs: [spec:AST-005/DEC-1, spec:AST-020, spec:AST-085]| Behavior | A `role="menu"` list of `menuitem` rows inside a heading popover, with arrow, Home, End, typeahead, Enter, Space, and Escape support. Selecting an enabled item runs it and, where the host heading supplies a close action, closes the heading menu. || End-user impact | Users switch products or accounts from the heading menu by pointer or keyboard; disabled rows are skipped by keyboard and typeahead. || Builder impact | None. |-| Compatibility/readiness | Records shipped behavior; no public change. Six implementation gaps (see Current-state impact). |+| Compatibility/readiness | Records shipped behavior; no public change. Five implementation gaps (see Current-state impact). || Review checks | Reject losing `menu`/`menuitem` roles to caller props, keyboard focus landing on a disabled item, or a caller `onKeyDown` that silently disables the keyboard model. || Governing rules | `family:navigation-destinations` FR7; `architecture:interaction-modality`; `architecture:component-theming-surface`; `architecture:layer-runtime`. |@@ -81,10 +81,6 @@ Consumer migration instructions belong in consumer docs and release notes.`role="menu"` sits directly inside the heading's own `role="menu"` wrapper.WAI-ARIA 1.2 allows a `menu` to own only `menuitem`-family items and groups,so assistive technology meets a menu nested in a menu.-- **Inside a heading, arrow keys move twice.** Inside SideNavHeading or- TopNavHeading, the heading's own list handler also moves focus over every- `menuitem`, so one ArrowDown moves two items, and in a two-item menu ArrowDown- never moves focus. The APG menu pattern moves focus one item per arrow press.- **The menu has no name of its own.** NavHeadingMenu's `menu` carries noaccessible name unless the caller passes `aria-label`; inside a heading onlythe heading's wrapper `menu` is named. The APG menu pattern, whichdiff --git a/packages/core/src/SideNav/SideNav.test.tsx b/packages/core/src/SideNav/SideNav.test.tsxindex 9027e561fd..cf0b0f5bed 100644--- a/packages/core/src/SideNav/SideNav.test.tsx+++ b/packages/core/src/SideNav/SideNav.test.tsx@@ -492,6 +492,33 @@ describe('SideNavHeading', () => {);});+ it('moves focus one item per arrow key in a NavHeadingMenu (APG menu)', async () => {+ __resetMenuPressForTest();+ const user = userEvent.setup();+ render(+ <SideNavHeading+ heading="My App"+ menu={+ <NavHeadingMenu>+ <NavHeadingMenuItem label="One" onClick={() => {}} />+ <NavHeadingMenuItem label="Two" onClick={() => {}} />+ <NavHeadingMenuItem label="Three" onClick={() => {}} />+ </NavHeadingMenu>+ }+ />,+ );+ await user.click(screen.getByRole('button', {name: 'Open menu'}));+ const item = (name: string) =>+ screen.getByRole('menuitem', {name, hidden: true});+ item('One').focus();+ fireEvent.keyDown(item('One'), {key: 'ArrowDown'});+ expect(item('Two')).toHaveFocus();+ fireEvent.keyDown(item('Two'), {key: 'ArrowDown'});+ expect(item('Three')).toHaveFocus();+ fireEvent.keyDown(item('Three'), {key: 'ArrowUp'});+ expect(item('Two')).toHaveFocus();+ });+it('closes when a mouse pressed on a NavHeadingMenu item is released outside the menu (useMenuPress FR3)', async () => {__resetMenuPressForTest();const user = userEvent.setup();diff --git a/packages/core/src/TopNav/TopNav.test.tsx b/packages/core/src/TopNav/TopNav.test.tsxindex 0d510da231..0112630b12 100644--- a/packages/core/src/TopNav/TopNav.test.tsx+++ b/packages/core/src/TopNav/TopNav.test.tsx@@ -377,6 +377,33 @@ describe('TopNavHeading', () => {);});+ it('moves focus one item per arrow key in a NavHeadingMenu (APG menu)', async () => {+ __resetMenuPressForTest();+ const user = userEvent.setup();+ render(+ <TopNavHeading+ heading="My App"+ menu={+ <NavHeadingMenu>+ <NavHeadingMenuItem label="One" onClick={() => {}} />+ <NavHeadingMenuItem label="Two" onClick={() => {}} />+ <NavHeadingMenuItem label="Three" onClick={() => {}} />+ </NavHeadingMenu>+ }+ />,+ );+ await user.click(screen.getByRole('button', {name: 'Open menu'}));+ const item = (name: string) =>+ screen.getByRole('menuitem', {name, hidden: true});+ item('One').focus();+ fireEvent.keyDown(item('One'), {key: 'ArrowDown'});+ expect(item('Two')).toHaveFocus();+ fireEvent.keyDown(item('Two'), {key: 'ArrowDown'});+ expect(item('Three')).toHaveFocus();+ fireEvent.keyDown(item('Three'), {key: 'ArrowUp'});+ expect(item('Two')).toHaveFocus();+ });+it('after a pointer pick, returns focus to the trigger without a keyboard ring (interaction-modality INV1)', async () => {__resetMenuPressForTest();const user = userEvent.setup();diff --git a/packages/core/src/hooks/useMenuHover.ts b/packages/core/src/hooks/useMenuHover.tsindex 35ea2ddc4d..03c293f972 100644--- a/packages/core/src/hooks/useMenuHover.ts+++ b/packages/core/src/hooks/useMenuHover.ts@@ -288,6 +288,20 @@ export function useMenuHover<T extends HTMLElement = HTMLElement>(}}, [hide, menuRef]);+ // A menu placed in the content can own its own list keys (NavHeadingMenu+ // runs useListFocus over the same `menuitem`s); a key it consumed must not+ // move focus a second time here as it bubbles out (APG menu: one item per+ // arrow press).+ const handleContentKeyDown = useCallback(+ (event: React.KeyboardEvent) => {+ if (event.defaultPrevented) {+ return;+ }+ handleListKeyDown(event);+ },+ [handleListKeyDown],+ );+useEffect(() => {escapeHandlerRef.current = () => {clearTimeouts();@@ -535,7 +549,7 @@ export function useMenuHover<T extends HTMLElement = HTMLElement>(contentProps: {onMouseEnter: handleContentMouseEnter,onMouseLeave: handleMouseLeave,- onKeyDown: handleListKeyDown,+ onKeyDown: handleContentKeyDown,},menuRef,focusFirst,