From 83fc823715dd736cbafa5553df659b51277de738 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 23 Oct 2018 20:02:23 +0200 Subject: [PATCH] feat(bottom-sheet): allow focus restoration to be disabled (#13153) Similarly to `MatDialog`, allows focus restoration to be disabled for `MatBottomSheet`. Fixes #13150. --- src/lib/bottom-sheet/bottom-sheet-config.ts | 6 ++++ .../bottom-sheet/bottom-sheet-container.ts | 4 +-- src/lib/bottom-sheet/bottom-sheet.spec.ts | 32 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/lib/bottom-sheet/bottom-sheet-config.ts b/src/lib/bottom-sheet/bottom-sheet-config.ts index 3bb9d9ea2cb..415d8f5aa9f 100644 --- a/src/lib/bottom-sheet/bottom-sheet-config.ts +++ b/src/lib/bottom-sheet/bottom-sheet-config.ts @@ -49,4 +49,10 @@ export class MatBottomSheetConfig { /** Whether the bottom sheet should focus the first focusable element on open. */ autoFocus?: boolean = true; + + /** + * Whether the bottom sheet should restore focus to the + * previously-focused element, after it's closed. + */ + restoreFocus?: boolean = true; } diff --git a/src/lib/bottom-sheet/bottom-sheet-container.ts b/src/lib/bottom-sheet/bottom-sheet-container.ts index 76f2e847988..8056ee5957f 100644 --- a/src/lib/bottom-sheet/bottom-sheet-container.ts +++ b/src/lib/bottom-sheet/bottom-sheet-container.ts @@ -190,12 +190,12 @@ export class MatBottomSheetContainer extends BasePortalOutlet implements OnDestr this._focusTrap.focusInitialElementWhenReady(); } - /** Restores focus to the element that was focused before the bottom sheet opened. */ + /** Restores focus to the element that was focused before the bottom sheet was opened. */ private _restoreFocus() { const toFocus = this._elementFocusedBeforeOpened; // We need the extra check, because IE can set the `activeElement` to null in some cases. - if (toFocus && typeof toFocus.focus === 'function') { + if (this.bottomSheetConfig.restoreFocus && toFocus && typeof toFocus.focus === 'function') { toFocus.focus(); } diff --git a/src/lib/bottom-sheet/bottom-sheet.spec.ts b/src/lib/bottom-sheet/bottom-sheet.spec.ts index 9abb25750a3..f473a329858 100644 --- a/src/lib/bottom-sheet/bottom-sheet.spec.ts +++ b/src/lib/bottom-sheet/bottom-sheet.spec.ts @@ -567,6 +567,38 @@ describe('MatBottomSheet', () => { document.body.removeChild(button); })); + it('should be able to disable focus restoration', fakeAsync(() => { + const button = document.createElement('button'); + button.id = 'bottom-sheet-trigger'; + document.body.appendChild(button); + button.focus(); + + const bottomSheetRef = bottomSheet.open(PizzaMsg, { + viewContainerRef: testViewContainerRef, + restoreFocus: false + }); + + flushMicrotasks(); + viewContainerFixture.detectChanges(); + flushMicrotasks(); + + expect(document.activeElement.id) + .not.toBe('bottom-sheet-trigger', 'Expected the focus to change when sheet was opened.'); + + bottomSheetRef.dismiss(); + expect(document.activeElement.id).not.toBe('bottom-sheet-trigger', + 'Expcted the focus not to have changed before the animation finishes.'); + + flushMicrotasks(); + viewContainerFixture.detectChanges(); + tick(500); + + expect(document.activeElement.id).not.toBe('bottom-sheet-trigger', + 'Expected the trigger not to be refocused on close.'); + + document.body.removeChild(button); + })); + }); });