Skip to content

Commit 83fc823

Browse files
crisbetommalerba
authored andcommitted
feat(bottom-sheet): allow focus restoration to be disabled (#13153)
Similarly to `MatDialog`, allows focus restoration to be disabled for `MatBottomSheet`. Fixes #13150.
1 parent 0cd7536 commit 83fc823

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/lib/bottom-sheet/bottom-sheet-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ export class MatBottomSheetConfig<D = any> {
4949

5050
/** Whether the bottom sheet should focus the first focusable element on open. */
5151
autoFocus?: boolean = true;
52+
53+
/**
54+
* Whether the bottom sheet should restore focus to the
55+
* previously-focused element, after it's closed.
56+
*/
57+
restoreFocus?: boolean = true;
5258
}

src/lib/bottom-sheet/bottom-sheet-container.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,12 @@ export class MatBottomSheetContainer extends BasePortalOutlet implements OnDestr
190190
this._focusTrap.focusInitialElementWhenReady();
191191
}
192192

193-
/** Restores focus to the element that was focused before the bottom sheet opened. */
193+
/** Restores focus to the element that was focused before the bottom sheet was opened. */
194194
private _restoreFocus() {
195195
const toFocus = this._elementFocusedBeforeOpened;
196196

197197
// We need the extra check, because IE can set the `activeElement` to null in some cases.
198-
if (toFocus && typeof toFocus.focus === 'function') {
198+
if (this.bottomSheetConfig.restoreFocus && toFocus && typeof toFocus.focus === 'function') {
199199
toFocus.focus();
200200
}
201201

src/lib/bottom-sheet/bottom-sheet.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,38 @@ describe('MatBottomSheet', () => {
567567
document.body.removeChild(button);
568568
}));
569569

570+
it('should be able to disable focus restoration', fakeAsync(() => {
571+
const button = document.createElement('button');
572+
button.id = 'bottom-sheet-trigger';
573+
document.body.appendChild(button);
574+
button.focus();
575+
576+
const bottomSheetRef = bottomSheet.open(PizzaMsg, {
577+
viewContainerRef: testViewContainerRef,
578+
restoreFocus: false
579+
});
580+
581+
flushMicrotasks();
582+
viewContainerFixture.detectChanges();
583+
flushMicrotasks();
584+
585+
expect(document.activeElement.id)
586+
.not.toBe('bottom-sheet-trigger', 'Expected the focus to change when sheet was opened.');
587+
588+
bottomSheetRef.dismiss();
589+
expect(document.activeElement.id).not.toBe('bottom-sheet-trigger',
590+
'Expcted the focus not to have changed before the animation finishes.');
591+
592+
flushMicrotasks();
593+
viewContainerFixture.detectChanges();
594+
tick(500);
595+
596+
expect(document.activeElement.id).not.toBe('bottom-sheet-trigger',
597+
'Expected the trigger not to be refocused on close.');
598+
599+
document.body.removeChild(button);
600+
}));
601+
570602
});
571603

572604
});

0 commit comments

Comments
 (0)