fix(frontend): keep scroll position when a menu closes (#672) - #679
Merged
Conversation
MatMenuTrigger restores focus to the trigger when a menu closes, calling
focus() without any FocusOptions. The browser then scrolls the trigger back
into view, so the page jumped back to the scroll position it had when the
menu was opened.
Add a directive that shadows Material's [matMenuTriggerFor] selector, takes
focus restoration over from the trigger and restores focus with
{ preventScroll: true }. Keyboard users still get focus back on the trigger,
but the page no longer moves. Submenu triggers are left alone: they live
inside the fixed overlay and cannot scroll the page.
MenuTriggerRestoreFocusDirective only applies to components that list it in their imports, and a component that forgets it still compiles. Fail the frontend job when a component template renders a mat-menu trigger and its sibling component file does not reference the directive.
The mobile layout scrolls an inner div, but it was never registered with the CDK ScrollDispatcher, so overlays using the default reposition scroll strategy never saw its scroll events: an open menu stayed frozen in place while the content slid underneath it, and tooltips did not hide on scroll.
Owner
Author
Merged (2026-07-28)Changes Made
Issue #672 stays open until this reaches |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #672.
Problem
Opening a three-dot menu, scrolling the page, then clicking anywhere made the page jump back to the scroll position it had when the menu was opened.
MatMenuTrigger._destroyMenu()restores focus to the trigger on close by callingfocus()with noFocusOptions, which routes tonativeElement.focus(undefined). Without{ preventScroll: true }the browser scrolls the trigger back into view.Menus use the default reposition scroll strategy, so
BlockScrollStrategywas not involved, andMAT_MENU_DEFAULT_OPTIONShas norestoreFocusfield, so there is no provider-level lever.Changes
MenuTriggerRestoreFocusDirective(app/shared/) shadows Material's[matMenuTriggerFor]selector, takes focus restoration over from the trigger and restores focus with{ preventScroll: true }. Keyboard users still get focus back on the trigger, so this is not the[matMenuTriggerRestoreFocus]="false"trade that drops focus return (WCAG 2.4.3). Submenu triggers are skipped: they live inside the fixed overlay and cannot scroll the page. Added to the six components that render root-level triggers.Deliberately excluded:
@treo/components/navigation/horizontal/components/branch- the live layout usestreo-vertical-navigation, so that component is dead code, and wiring it would create a@treo -> app/shareddependency.CI guard in the
Test Frontendjob. The directive only applies to components that import it, and a component that forgets it still compiles, so the job fails when a component template renders a menu trigger and its sibling component file does not reference the directive.Separate commit:
cdkScrollableon the mobile layout's scroll container. That div is the scroller on narrow viewports but was never registered with the CDKScrollDispatcher, so overlays never saw its scroll events - an open menu stayed frozen in place while content slid underneath it, and tooltips did not hide on scroll.Tests
New spec
menu-trigger-restore-focus.directive.spec.ts(6 specs) runs the trigger inside its own overflow container so the assertions do not depend on the karma window size. It covers focus restoration withpreventScroll, the preserved scroll position, focus actually returning to the trigger, therestoreFocushandover, the explicit opt-out, submenu triggers being left alone, and a detached trigger.It also includes a control spec on a host without the directive, which asserts the scroll jump still happens. That proves the other specs are not tautological, and it will fail if Angular Material ever passes
preventScrollitself, at which point the directive can be deleted.Verification
npm run lint: cleannpx ng test --watch=false --browsers=ChromeHeadless: 160 passingnpm run build: cleanNot yet validated against a live instance; the Zeus test host was unreachable from the build session.