Skip to content

Commit 8ce03c9

Browse files
crisbetojosephperrott
authored andcommitted
feat(bottom-sheet): allow autofocusing to be disabled (#12193)
1 parent d21554a commit 8ce03c9

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ export class MatBottomSheetConfig<D = any> {
4242

4343
/** Whether the bottom sheet should close when the user goes backwards/forwards in history. */
4444
closeOnNavigation?: boolean = true;
45+
46+
/** Whether the bottom sheet should focus the first focusable element on open. */
47+
autoFocus?: boolean = true;
4548
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ export class MatBottomSheetContainer extends BasePortalOutlet implements OnDestr
144144
}
145145

146146
_onAnimationDone(event: AnimationEvent) {
147-
if (event.toState === 'visible') {
148-
this._trapFocus();
149-
} else if (event.toState === 'hidden') {
147+
if (event.toState === 'hidden') {
150148
this._restoreFocus();
149+
} else if (event.toState === 'visible' && this.bottomSheetConfig.autoFocus) {
150+
this._trapFocus();
151151
}
152152

153153
this._animationStateChanged.emit(event);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,18 @@ describe('MatBottomSheet', () => {
509509
.toBe('INPUT', 'Expected first tabbable element (input) in the sheet to be focused.');
510510
}));
511511

512+
it('should allow disabling focus of the first tabbable element', fakeAsync(() => {
513+
bottomSheet.open(PizzaMsg, {
514+
viewContainerRef: testViewContainerRef,
515+
autoFocus: false
516+
});
517+
518+
viewContainerFixture.detectChanges();
519+
flushMicrotasks();
520+
521+
expect(document.activeElement.tagName).not.toBe('INPUT');
522+
}));
523+
512524
it('should re-focus trigger element when bottom sheet closes', fakeAsync(() => {
513525
const button = document.createElement('button');
514526
button.id = 'bottom-sheet-trigger';

0 commit comments

Comments
 (0)