Skip to content

fix(aria/menu): close only the focused submenu on escape - #34085

Open
prath47 wants to merge 1 commit into
angular:mainfrom
prath47:aria-menu-escape-one-level
Open

prath47 wants to merge 1 commit into
angular:mainfrom
prath47:aria-menu-escape-one-level

Conversation

@prath47

@prath47 prath47 commented Nov 6, 2026

Copy link
Copy Markdown

Fixes #34071

Escape inside a nested ngMenu was calling closeAll(), so the whole menu tree closed and focus jumped back to the trigger. The APG menu pattern says Escape should close the menu that has focus and put focus back on whatever opened it, and that's also how cdk/menu and mat-menu already behave.

I added a closeCurrent() on MenuPattern and pointed the Escape binding at it. If the menu is a nested submenu (its parent item isn't in a menubar), it closes just that submenu with refocus: true, the same path ArrowLeft/collapse() already uses. Otherwise it falls back to closeAll(), so first-level menus under a trigger or a menubar close exactly as before.

Tests

New specs:

  • private/menu/menu.spec.ts: Escape from a trigger's submenu, and from a nested submenu under a menubar menu
  • menu/menu.spec.ts: directive test using the existing Berries submenu. Escape from Blueberry leaves the main menu open and focuses Berries
bazel test //src/aria/private/menu:unit_tests //src/aria/menu:unit_tests

//src/aria/menu:unit_tests_chromium              PASSED
//src/aria/menu:unit_tests_firefox               PASSED
//src/aria/private/menu:unit_tests_chromium      PASSED
//src/aria/private/menu:unit_tests_firefox       PASSED

I also checked that the new tests fail without the change to menu.ts (all three fail with Expected false to be true.).

Manual check

I used the repro from the issue (https://github.com/artaommahe/aria-menu-repro, section A): open menu A, go to Categorize, ArrowRight into the submenu, press Escape. The focused element is outlined in red.

Before (22.2.6): both menus close and focus goes back to "Open menu A"

before

After (same build with this change applied): the submenu closes, the root menu stays open and focus is on "Categorize"

after

Ref: https://www.w3.org/WAI/ARIA/apg/patterns/menubar/ (Escape: "Close the menu that contains focus and return focus to the element or context, e.g., menu button or parent menuitem, from which the menu was opened.")

Pressing Escape inside a nested submenu used to close the entire menu
tree and send focus back to the trigger. Following the APG menu pattern
(and what cdk/menu and mat-menu already do), Escape now closes just the
submenu that has focus and returns focus to the item that opened it.
Top-level menus keep closing the same way as before.

Fixes angular#34071

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(aria/menu): Escape inside a submenu closes the whole menu instead of only the submenu

1 participant