Skip to content

fix(menu): return focus to root trigger when closed by mouse #8348

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
Nov 20, 2017
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
7 changes: 4 additions & 3 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,10 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
private _resetMenu(): void {
this._setIsMenuOpen(false);

// Focus only needs to be reset to the host element if the menu was opened
// by the keyboard and manually shifted to the first menu item.
if (!this._openedByMouse) {
// We should reset focus if the user is navigating using a keyboard or
// if we have a top-level trigger which might cause focus to be lost
// when clicking on the backdrop.
if (!this._openedByMouse || !this.triggersSubmenu()) {
this.focus();
}

Expand Down
59 changes: 59 additions & 0 deletions src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,43 @@ describe('MatMenu', () => {
expect(overlayContainerElement.textContent).toBe('');
}));

it('should restore focus to the trigger when the menu was opened by keyboard', fakeAsync(() => {
const fixture = TestBed.createComponent(SimpleMenu);
fixture.detectChanges();

const triggerEl = fixture.componentInstance.triggerEl.nativeElement;

// A click without a mousedown before it is considered a keyboard open.
triggerEl.click();
fixture.detectChanges();

expect(overlayContainerElement.querySelector('.mat-menu-panel')).toBeTruthy();

fixture.componentInstance.trigger.closeMenu();
fixture.detectChanges();
tick(500);

expect(document.activeElement).toBe(triggerEl);
}));

it('should restore focus to the root trigger when the menu was opened by mouse', fakeAsync(() => {
const fixture = TestBed.createComponent(SimpleMenu);
fixture.detectChanges();

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

expect(overlayContainerElement.querySelector('.mat-menu-panel')).toBeTruthy();

fixture.componentInstance.trigger.closeMenu();
fixture.detectChanges();
tick(500);

expect(document.activeElement).toBe(triggerEl);
}));

it('should close the menu when pressing ESCAPE', fakeAsync(() => {
const fixture = TestBed.createComponent(SimpleMenu);
fixture.detectChanges();
Expand Down Expand Up @@ -1082,6 +1119,28 @@ describe('MatMenu', () => {
expect(overlay.querySelectorAll('.mat-menu-panel').length).toBe(2, 'Expected two open menus');
}));

it('should not re-focus a child menu trigger when hovering another trigger', fakeAsync(() => {
compileTestComponent();

dispatchFakeEvent(instance.rootTriggerEl.nativeElement, 'mousedown');
instance.rootTriggerEl.nativeElement.click();
fixture.detectChanges();

const items = Array.from(overlay.querySelectorAll('.mat-menu-panel [mat-menu-item]'));
const levelOneTrigger = overlay.querySelector('#level-one-trigger')!;

dispatchMouseEvent(levelOneTrigger, 'mouseenter');
fixture.detectChanges();
expect(overlay.querySelectorAll('.mat-menu-panel').length).toBe(2, 'Expected two open menus');

dispatchMouseEvent(items[items.indexOf(levelOneTrigger) + 1], 'mouseenter');
fixture.detectChanges();
tick(500);

expect(document.activeElement)
.not.toBe(levelOneTrigger, 'Expected focus not to be returned to the initial trigger.');
}));

});

});
Expand Down