Skip to content

feat(bottom-sheet): allow focus restoration to be disabled #13153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/lib/bottom-sheet/bottom-sheet-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ export class MatBottomSheetConfig<D = any> {

/** 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;
}
4 changes: 2 additions & 2 deletions src/lib/bottom-sheet/bottom-sheet-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
32 changes: 32 additions & 0 deletions src/lib/bottom-sheet/bottom-sheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,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);
}));

});

});
Expand Down