Skip to content

Commit 01734b3

Browse files
authored
fix(material/sidenav): restore focus with correct origin when closing via the backdrop (#23492)
Currently when a sidenav is closed, we restore the focus origin as when it was focused because we don't know what kind of event closed it, however if it happens through the backdrop, we can be fairly certain that it was via mouse. These changes pass in the `mouse` origin when closing through the backdrop. Fixes #23484.
1 parent 875f00d commit 01734b3

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/material/sidenav/drawer.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
296296

297297
this._takeFocus();
298298
} else if (this._isFocusWithinDrawer()) {
299-
this._restoreFocus();
299+
this._restoreFocus(this._openedVia || 'program');
300300
}
301301
});
302302

@@ -400,20 +400,18 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
400400
* Restores focus to the element that was originally focused when the drawer opened.
401401
* If no element was focused at that time, the focus will be restored to the drawer.
402402
*/
403-
private _restoreFocus() {
403+
private _restoreFocus(focusOrigin: Exclude<FocusOrigin, null>) {
404404
if (this.autoFocus === 'dialog') {
405405
return;
406406
}
407407

408-
// Note that we don't check via `instanceof HTMLElement` so that we can cover SVGs as well.
409408
if (this._elementFocusedBeforeDrawerWasOpened) {
410-
this._focusMonitor.focusVia(this._elementFocusedBeforeDrawerWasOpened, this._openedVia);
409+
this._focusMonitor.focusVia(this._elementFocusedBeforeDrawerWasOpened, focusOrigin);
411410
} else {
412411
this._elementRef.nativeElement.blur();
413412
}
414413

415414
this._elementFocusedBeforeDrawerWasOpened = null;
416-
this._openedVia = null;
417415
}
418416

419417
/** Whether focus is currently within the drawer. */
@@ -467,8 +465,8 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
467465
_closeViaBackdropClick(): Promise<MatDrawerToggleResult> {
468466
// If the drawer is closed upon a backdrop click, we always want to restore focus. We
469467
// 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');
472470
}
473471

474472
/**
@@ -481,28 +479,36 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
481479
: Promise<MatDrawerToggleResult> {
482480
// If the focus is currently inside the drawer content and we are closing the drawer,
483481
// 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;
486494
}
487495

488496
/**
489497
* Toggles the opened state of the drawer.
490498
* @param isOpen Whether the drawer should open or close.
491499
* @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.
494501
*/
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> {
497504
this._opened = isOpen;
498505

499506
if (isOpen) {
500507
this._animationState = this._enableAnimations ? 'open' : 'open-instant';
501-
this._openedVia = openedVia;
502508
} else {
503509
this._animationState = 'void';
504510
if (restoreFocus) {
505-
this._restoreFocus();
511+
this._restoreFocus(focusOrigin);
506512
}
507513
}
508514

0 commit comments

Comments
 (0)