Skip to content

fix(menu): scrollable menu not scrolled to top when opened for the first time #11859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2018
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
10 changes: 10 additions & 0 deletions src/lib/menu/menu-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,15 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
_onAnimationDone(event: AnimationEvent) {
this._animationDone.next(event);
this._isAnimating = false;

// Scroll the content element to the top once the animation is done. This is necessary, because
// we move focus to the first item while it's still being animated, which can throw the browser
// off when it determines the scroll position. Alternatively we can move focus when the
// animation is done, however moving focus asynchronously will interrupt screen readers
// which are in the process of reading out the menu already. We take the `element` from
// the `event` since we can't use a `ViewChild` to access the pane.
if (event.toState === 'enter' && this._keyManager.activeItemIndex === 0) {
event.element.scrollTop = 0;
}
}
}
19 changes: 19 additions & 0 deletions src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,23 @@ describe('MatMenu', () => {
expect(document.activeElement).toBe(triggerEl);
}));

it('should scroll the panel to the top on open, when it is scrollable', fakeAsync(() => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();

// Add 50 items to make the menu scrollable
fixture.componentInstance.extraItems = new Array(50).fill('Hello there');
fixture.detectChanges();

const triggerEl = fixture.componentInstance.triggerEl.nativeElement;
dispatchFakeEvent(triggerEl, 'mousedown');
triggerEl.click();
fixture.detectChanges();
tick();

expect(overlayContainerElement.querySelector('.mat-menu-panel')!.scrollTop).toBe(0);
}));

it('should set the proper focus origin when restoring focus after opening by keyboard',
fakeAsync(() => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
Expand Down Expand Up @@ -1639,6 +1656,7 @@ describe('MatMenu default overrides', () => {
<fake-icon>unicorn</fake-icon>
Item with an icon
</button>
<button *ngFor="let item of extraItems" mat-menu-item> {{item}} </button>
</mat-menu>
`
})
Expand All @@ -1647,6 +1665,7 @@ class SimpleMenu {
@ViewChild('triggerEl') triggerEl: ElementRef;
@ViewChild(MatMenu) menu: MatMenu;
@ViewChildren(MatMenuItem) items: QueryList<MatMenuItem>;
extraItems: string[] = [];
closeCallback = jasmine.createSpy('menu closed callback');
backdropClass: string;
}
Expand Down