@@ -296,7 +296,7 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
296
296
297
297
this . _takeFocus ( ) ;
298
298
} else if ( this . _isFocusWithinDrawer ( ) ) {
299
- this . _restoreFocus ( ) ;
299
+ this . _restoreFocus ( this . _openedVia || 'program' ) ;
300
300
}
301
301
} ) ;
302
302
@@ -400,20 +400,18 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
400
400
* Restores focus to the element that was originally focused when the drawer opened.
401
401
* If no element was focused at that time, the focus will be restored to the drawer.
402
402
*/
403
- private _restoreFocus ( ) {
403
+ private _restoreFocus ( focusOrigin : Exclude < FocusOrigin , null > ) {
404
404
if ( this . autoFocus === 'dialog' ) {
405
405
return ;
406
406
}
407
407
408
- // Note that we don't check via `instanceof HTMLElement` so that we can cover SVGs as well.
409
408
if ( this . _elementFocusedBeforeDrawerWasOpened ) {
410
- this . _focusMonitor . focusVia ( this . _elementFocusedBeforeDrawerWasOpened , this . _openedVia ) ;
409
+ this . _focusMonitor . focusVia ( this . _elementFocusedBeforeDrawerWasOpened , focusOrigin ) ;
411
410
} else {
412
411
this . _elementRef . nativeElement . blur ( ) ;
413
412
}
414
413
415
414
this . _elementFocusedBeforeDrawerWasOpened = null ;
416
- this . _openedVia = null ;
417
415
}
418
416
419
417
/** Whether focus is currently within the drawer. */
@@ -467,8 +465,8 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
467
465
_closeViaBackdropClick ( ) : Promise < MatDrawerToggleResult > {
468
466
// If the drawer is closed upon a backdrop click, we always want to restore focus. We
469
467
// don't need to check whether focus is currently in the drawer, as clicking on the
470
- // backdrop causes blurring of the active element.
471
- return this . _setOpen ( /* isOpen */ false , /* restoreFocus */ true ) ;
468
+ // backdrop causes blurs the active element.
469
+ return this . _setOpen ( /* isOpen */ false , /* restoreFocus */ true , 'mouse' ) ;
472
470
}
473
471
474
472
/**
@@ -481,28 +479,36 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
481
479
: Promise < MatDrawerToggleResult > {
482
480
// If the focus is currently inside the drawer content and we are closing the drawer,
483
481
// restore the focus to the initially focused element (when the drawer opened).
484
- return this . _setOpen (
485
- isOpen , /* restoreFocus */ ! isOpen && this . _isFocusWithinDrawer ( ) , openedVia ) ;
482
+ if ( isOpen && openedVia ) {
483
+ this . _openedVia = openedVia ;
484
+ }
485
+
486
+ const result = this . _setOpen ( isOpen , /* restoreFocus */ ! isOpen && this . _isFocusWithinDrawer ( ) ,
487
+ this . _openedVia || 'program' ) ;
488
+
489
+ if ( ! isOpen ) {
490
+ this . _openedVia = null ;
491
+ }
492
+
493
+ return result ;
486
494
}
487
495
488
496
/**
489
497
* Toggles the opened state of the drawer.
490
498
* @param isOpen Whether the drawer should open or close.
491
499
* @param restoreFocus Whether focus should be restored on close.
492
- * @param openedVia Focus origin that can be optionally set when opening a drawer. The
493
- * origin will be used later when focus is restored on drawer close.
500
+ * @param focusOrigin Origin to use when restoring focus.
494
501
*/
495
- private _setOpen ( isOpen : boolean , restoreFocus : boolean , openedVia : FocusOrigin = 'program' )
496
- : Promise < MatDrawerToggleResult > {
502
+ private _setOpen ( isOpen : boolean , restoreFocus : boolean , focusOrigin : Exclude < FocusOrigin , null > ) :
503
+ Promise < MatDrawerToggleResult > {
497
504
this . _opened = isOpen ;
498
505
499
506
if ( isOpen ) {
500
507
this . _animationState = this . _enableAnimations ? 'open' : 'open-instant' ;
501
- this . _openedVia = openedVia ;
502
508
} else {
503
509
this . _animationState = 'void' ;
504
510
if ( restoreFocus ) {
505
- this . _restoreFocus ( ) ;
511
+ this . _restoreFocus ( focusOrigin ) ;
506
512
}
507
513
}
508
514
0 commit comments