Skip to content

Commit 4ae63b3

Browse files
crisbetoandrewseguin
authored andcommitted
feat(sidenav): allow for auto focusing to be disabled (#10933)
Along the same lines as the dialog, these changes allow for the auto focusing, that is done by the sidenav, to be disabled. Fixes #10402.
1 parent a275dc1 commit 4ae63b3

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/lib/sidenav/drawer.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,19 @@ describe('MatDrawer', () => {
469469
expect(document.activeElement).toBe(drawerEl.nativeElement);
470470
}));
471471

472+
it('should be able to disable auto focus', fakeAsync(() => {
473+
testComponent.autoFocus = false;
474+
testComponent.mode = 'push';
475+
fixture.detectChanges();
476+
lastFocusableElement.focus();
477+
478+
drawer.open();
479+
fixture.detectChanges();
480+
tick();
481+
482+
expect(document.activeElement).not.toBe(firstFocusableElement);
483+
}));
484+
472485
});
473486
});
474487

@@ -790,14 +803,15 @@ class DrawerDynamicPosition {
790803
// to be focusable across all platforms.
791804
template: `
792805
<mat-drawer-container>
793-
<mat-drawer position="start" [mode]="mode">
806+
<mat-drawer position="start" [mode]="mode" [autoFocus]="autoFocus">
794807
<input type="text" class="input1"/>
795808
</mat-drawer>
796809
<input type="text" class="input2"/>
797810
</mat-drawer-container>`,
798811
})
799812
class DrawerWithFocusableElements {
800813
mode: string = 'over';
814+
autoFocus = true;
801815
}
802816

803817
@Component({

src/lib/sidenav/drawer.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
150150
set disableClose(value: boolean) { this._disableClose = coerceBooleanProperty(value); }
151151
private _disableClose: boolean = false;
152152

153+
/** Whether the drawer should focus the first focusable element automatically when opened. */
154+
@Input()
155+
get autoFocus(): boolean { return this._autoFocus; }
156+
set autoFocus(value: boolean) { this._autoFocus = coerceBooleanProperty(value); }
157+
private _autoFocus: boolean = true;
158+
153159
/** How the sidenav was opened (keypress, mouse click etc.) */
154160
private _openedVia: FocusOrigin | null;
155161

@@ -246,6 +252,10 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
246252

247253
/** Traps focus inside the drawer. */
248254
private _trapFocus() {
255+
if (!this.autoFocus) {
256+
return;
257+
}
258+
249259
this._focusTrap.focusInitialElementWhenReady().then(hasMovedFocus => {
250260
// If there were no focusable elements, focus the sidenav itself so the keyboard navigation
251261
// still works. We need to check that `focus` is a function due to Universal.
@@ -260,6 +270,10 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
260270
* opened.
261271
*/
262272
private _restoreFocus() {
273+
if (!this.autoFocus) {
274+
return;
275+
}
276+
263277
const activeEl = this._doc && this._doc.activeElement;
264278

265279
if (activeEl && this._elementRef.nativeElement.contains(activeEl)) {

0 commit comments

Comments
 (0)