Skip to content

feat(sidenav): allow for auto focusing to be disabled #10933

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
Apr 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
16 changes: 15 additions & 1 deletion src/lib/sidenav/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,19 @@ describe('MatDrawer', () => {
expect(document.activeElement).toBe(drawerEl.nativeElement);
}));

it('should be able to disable auto focus', fakeAsync(() => {
testComponent.autoFocus = false;
testComponent.mode = 'push';
fixture.detectChanges();
lastFocusableElement.focus();

drawer.open();
fixture.detectChanges();
tick();

expect(document.activeElement).not.toBe(firstFocusableElement);
}));

});
});

Expand Down Expand Up @@ -790,14 +803,15 @@ class DrawerDynamicPosition {
// to be focusable across all platforms.
template: `
<mat-drawer-container>
<mat-drawer position="start" [mode]="mode">
<mat-drawer position="start" [mode]="mode" [autoFocus]="autoFocus">
<input type="text" class="input1"/>
</mat-drawer>
<input type="text" class="input2"/>
</mat-drawer-container>`,
})
class DrawerWithFocusableElements {
mode: string = 'over';
autoFocus = true;
}

@Component({
Expand Down
14 changes: 14 additions & 0 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
set disableClose(value: boolean) { this._disableClose = coerceBooleanProperty(value); }
private _disableClose: boolean = false;

/** Whether the drawer should focus the first focusable element automatically when opened. */
@Input()
get autoFocus(): boolean { return this._autoFocus; }
set autoFocus(value: boolean) { this._autoFocus = coerceBooleanProperty(value); }
private _autoFocus: boolean = true;

/** How the sidenav was opened (keypress, mouse click etc.) */
private _openedVia: FocusOrigin | null;

Expand Down Expand Up @@ -246,6 +252,10 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr

/** Traps focus inside the drawer. */
private _trapFocus() {
if (!this.autoFocus) {
return;
}

this._focusTrap.focusInitialElementWhenReady().then(hasMovedFocus => {
// If there were no focusable elements, focus the sidenav itself so the keyboard navigation
// still works. We need to check that `focus` is a function due to Universal.
Expand All @@ -260,6 +270,10 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
* opened.
*/
private _restoreFocus() {
if (!this.autoFocus) {
return;
}

const activeEl = this._doc && this._doc.activeElement;

if (activeEl && this._elementRef.nativeElement.contains(activeEl)) {
Expand Down