Skip to content

feat(bottom-sheet): allow autofocusing to be disabled #12193

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
Jul 18, 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
3 changes: 3 additions & 0 deletions src/lib/bottom-sheet/bottom-sheet-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ export class MatBottomSheetConfig<D = any> {

/** Whether the bottom sheet should close when the user goes backwards/forwards in history. */
closeOnNavigation?: boolean = true;

/** Whether the bottom sheet should focus the first focusable element on open. */
autoFocus?: boolean = true;
}
6 changes: 3 additions & 3 deletions src/lib/bottom-sheet/bottom-sheet-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ export class MatBottomSheetContainer extends BasePortalOutlet implements OnDestr
}

_onAnimationDone(event: AnimationEvent) {
if (event.toState === 'visible') {
this._trapFocus();
} else if (event.toState === 'hidden') {
if (event.toState === 'hidden') {
this._restoreFocus();
} else if (event.toState === 'visible' && this.bottomSheetConfig.autoFocus) {
this._trapFocus();
}

this._animationStateChanged.emit(event);
Expand Down
12 changes: 12 additions & 0 deletions src/lib/bottom-sheet/bottom-sheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,18 @@ describe('MatBottomSheet', () => {
.toBe('INPUT', 'Expected first tabbable element (input) in the sheet to be focused.');
}));

it('should allow disabling focus of the first tabbable element', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
autoFocus: false
});

viewContainerFixture.detectChanges();
flushMicrotasks();

expect(document.activeElement.tagName).not.toBe('INPUT');
}));

it('should re-focus trigger element when bottom sheet closes', fakeAsync(() => {
const button = document.createElement('button');
button.id = 'bottom-sheet-trigger';
Expand Down