@@ -173,11 +173,22 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
173
173
set disableClose ( value : boolean ) { this . _disableClose = coerceBooleanProperty ( value ) ; }
174
174
private _disableClose : boolean = false ;
175
175
176
- /** Whether the drawer should focus the first focusable element automatically when opened. */
176
+ /**
177
+ * Whether the drawer should focus the first focusable element automatically when opened.
178
+ * Defaults to false in when `mode` is set to `side`, otherwise defaults to `true`. If explicitly
179
+ * enabled, focus will be moved into the sidenav in `side` mode as well.
180
+ */
177
181
@Input ( )
178
- get autoFocus ( ) : boolean { return this . _autoFocus ; }
182
+ get autoFocus ( ) : boolean {
183
+ const value = this . _autoFocus ;
184
+
185
+ // Note that usually we disable auto focusing in `side` mode, because we don't know how the
186
+ // sidenav is being used, but in some cases it still makes sense to do it. If the consumer
187
+ // explicitly enabled `autoFocus`, we take it as them always wanting to enable it.
188
+ return value == null ? this . mode !== 'side' : value ;
189
+ }
179
190
set autoFocus ( value : boolean ) { this . _autoFocus = coerceBooleanProperty ( value ) ; }
180
- private _autoFocus : boolean = true ;
191
+ private _autoFocus : boolean | undefined ;
181
192
182
193
/**
183
194
* Whether the drawer is opened. We overload this because we trigger an event when it
@@ -253,11 +264,6 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
253
264
*/
254
265
readonly _modeChanged = new Subject < void > ( ) ;
255
266
256
- get _isFocusTrapEnabled ( ) : boolean {
257
- // The focus trap is only enabled when the drawer is open in any mode other than side.
258
- return this . opened && this . mode !== 'side' ;
259
- }
260
-
261
267
constructor ( private _elementRef : ElementRef < HTMLElement > ,
262
268
private _focusTrapFactory : FocusTrapFactory ,
263
269
private _focusMonitor : FocusMonitor ,
@@ -276,9 +282,7 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
276
282
this . _elementFocusedBeforeDrawerWasOpened = this . _doc . activeElement as HTMLElement ;
277
283
}
278
284
279
- if ( this . _isFocusTrapEnabled && this . _focusTrap ) {
280
- this . _trapFocus ( ) ;
281
- }
285
+ this . _takeFocus ( ) ;
282
286
} else {
283
287
this . _restoreFocus ( ) ;
284
288
}
@@ -316,9 +320,12 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
316
320
} ) ;
317
321
}
318
322
319
- /** Traps focus inside the drawer. */
320
- private _trapFocus ( ) {
321
- if ( ! this . autoFocus ) {
323
+ /**
324
+ * Moves focus into the drawer. Note that this works even if
325
+ * the focus trap is disabled in `side` mode.
326
+ */
327
+ private _takeFocus ( ) {
328
+ if ( ! this . autoFocus || ! this . _focusTrap ) {
322
329
return ;
323
330
}
324
331
@@ -428,7 +435,8 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
428
435
/** Updates the enabled state of the focus trap. */
429
436
private _updateFocusTrapState ( ) {
430
437
if ( this . _focusTrap ) {
431
- this . _focusTrap . enabled = this . _isFocusTrapEnabled ;
438
+ // The focus trap is only enabled when the drawer is open in any mode other than side.
439
+ this . _focusTrap . enabled = this . opened && this . mode !== 'side' ;
432
440
}
433
441
}
434
442
0 commit comments