@@ -173,11 +173,22 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
173173 set disableClose ( value : boolean ) { this . _disableClose = coerceBooleanProperty ( value ) ; }
174174 private _disableClose : boolean = false ;
175175
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+ */
177181 @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+ }
179190 set autoFocus ( value : boolean ) { this . _autoFocus = coerceBooleanProperty ( value ) ; }
180- private _autoFocus : boolean = true ;
191+ private _autoFocus : boolean | undefined ;
181192
182193 /**
183194 * 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
253264 */
254265 readonly _modeChanged = new Subject < void > ( ) ;
255266
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-
261267 constructor ( private _elementRef : ElementRef < HTMLElement > ,
262268 private _focusTrapFactory : FocusTrapFactory ,
263269 private _focusMonitor : FocusMonitor ,
@@ -276,9 +282,7 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
276282 this . _elementFocusedBeforeDrawerWasOpened = this . _doc . activeElement as HTMLElement ;
277283 }
278284
279- if ( this . _isFocusTrapEnabled && this . _focusTrap ) {
280- this . _trapFocus ( ) ;
281- }
285+ this . _takeFocus ( ) ;
282286 } else {
283287 this . _restoreFocus ( ) ;
284288 }
@@ -316,9 +320,12 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
316320 } ) ;
317321 }
318322
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 ) {
322329 return ;
323330 }
324331
@@ -428,7 +435,8 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
428435 /** Updates the enabled state of the focus trap. */
429436 private _updateFocusTrapState ( ) {
430437 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' ;
432440 }
433441 }
434442
0 commit comments