Skip to content

fix(sidenav): don't restore focus if focus isn't inside sidenav #4578

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 2 commits into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 38 additions & 10 deletions src/lib/sidenav/sidenav.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {fakeAsync, async, tick, ComponentFixture, TestBed} from '@angular/core/testing';
import {Component, ViewChild} from '@angular/core';
import {Component, ElementRef, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MdSidenav, MdSidenavModule, MdSidenavToggleResult, MdSidenavContainer} from './index';
import {A11yModule} from '../core/a11y/index';
Expand Down Expand Up @@ -310,19 +310,20 @@ describe('MdSidenav', () => {
expect(testComponent.closeCount).toBe(0);
}));

it('should restore focus to the trigger element on close', fakeAsync(() => {
it('should restore focus on close if focus is inside sidenav', fakeAsync(() => {
let fixture = TestBed.createComponent(BasicTestApp);
let sidenav: MdSidenav = fixture.debugElement
.query(By.directive(MdSidenav)).componentInstance;
let trigger = document.createElement('button');
let openButton = fixture.componentInstance.openButton.nativeElement;
let sidenavButton = fixture.componentInstance.sidenavButton.nativeElement;

document.body.appendChild(trigger);
trigger.focus();
openButton.focus();
sidenav.open();

fixture.detectChanges();
endSidenavTransition(fixture);
tick();
sidenavButton.focus();

sidenav.close();

Expand All @@ -331,9 +332,32 @@ describe('MdSidenav', () => {
tick();

expect(document.activeElement)
.toBe(trigger, 'Expected focus to be restored to the trigger on close.');
.toBe(openButton, 'Expected focus to be restored to the open button on close.');
}));

it('should not restore focus on close if focus is outside sidenav', fakeAsync(() => {
let fixture = TestBed.createComponent(BasicTestApp);
let sidenav: MdSidenav = fixture.debugElement
.query(By.directive(MdSidenav)).componentInstance;
let openButton = fixture.componentInstance.openButton.nativeElement;
let closeButton = fixture.componentInstance.closeButton.nativeElement;

openButton.focus();
sidenav.open();

fixture.detectChanges();
endSidenavTransition(fixture);
tick();
closeButton.focus();

sidenav.close();

trigger.parentNode.removeChild(trigger);
fixture.detectChanges();
endSidenavTransition(fixture);
tick();

expect(document.activeElement)
.toBe(closeButton, 'Expected focus not to be restored to the open button on close.');
}));
});

Expand Down Expand Up @@ -508,10 +532,10 @@ class SidenavContainerTwoSidenavTestApp {
(open)="open()"
(close-start)="closeStart()"
(close)="close()">
Content.
<button #sidenavButton>Content.</button>
</md-sidenav>
<button (click)="sidenav.open()" class="open"></button>
<button (click)="sidenav.close()" class="close"></button>
<button (click)="sidenav.open()" class="open" #openButton></button>
<button (click)="sidenav.close()" class="close" #closeButton></button>
</md-sidenav-container>`,
})
class BasicTestApp {
Expand All @@ -521,6 +545,10 @@ class BasicTestApp {
closeCount: number = 0;
backdropClickedCount: number = 0;

@ViewChild('sidenavButton') sidenavButton: ElementRef;
@ViewChild('openButton') openButton: ElementRef;
@ViewChild('closeButton') closeButton: ElementRef;

openStart() {
this.openStartCount++;
}
Expand Down
15 changes: 12 additions & 3 deletions src/lib/sidenav/sidenav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,24 @@ export class MdSidenav implements AfterContentInit, OnDestroy {
}
});

this.onClose.subscribe(() => {
this.onClose.subscribe(() => this._restoreFocus());
}

/**
* If focus is currently inside the sidenav, restores it to where it was before the sidenav
* opened.
*/
private _restoreFocus() {
let activeEl = document.activeElement;
Copy link
Contributor

@kara kara May 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we should be injecting the Document here rather than using the global (for Universal). This is user-interaction based, so may be okay?

if (activeEl && this._elementRef.nativeElement.contains(activeEl)) {
if (this._elementFocusedBeforeSidenavWasOpened instanceof HTMLElement) {
this._elementFocusedBeforeSidenavWasOpened.focus();
} else {
this._elementRef.nativeElement.blur();
}
}

this._elementFocusedBeforeSidenavWasOpened = null;
});
this._elementFocusedBeforeSidenavWasOpened = null;
}

ngAfterContentInit() {
Expand Down