Skip to content

Commit 3787695

Browse files
crisbetojelbourn
authored andcommitted
fix(bottom-sheet): focus trap not being attached when autoFocus is disabled (#15125)
Fixes the bottom sheet container not creating a focus trap, if `autoFocus` is set to `false`. Fixes #15119.
1 parent 123976f commit 3787695

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class MatBottomSheetContainer extends BasePortalOutlet implements OnDestr
146146
_onAnimationDone(event: AnimationEvent) {
147147
if (event.toState === 'hidden') {
148148
this._restoreFocus();
149-
} else if (event.toState === 'visible' && this.bottomSheetConfig.autoFocus) {
149+
} else if (event.toState === 'visible') {
150150
this._trapFocus();
151151
}
152152

@@ -187,7 +187,9 @@ export class MatBottomSheetContainer extends BasePortalOutlet implements OnDestr
187187
this._focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement);
188188
}
189189

190-
this._focusTrap.focusInitialElementWhenReady();
190+
if (this.bottomSheetConfig.autoFocus) {
191+
this._focusTrap.focusInitialElementWhenReady();
192+
}
191193
}
192194

193195
/** Restores focus to the element that was focused before the bottom sheet was opened. */

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,20 @@ describe('MatBottomSheet', () => {
547547
'Expected bottom sheet container to be focused.');
548548
}));
549549

550+
it('should create a focus trap if autoFocus is disabled', fakeAsync(() => {
551+
bottomSheet.open(PizzaMsg, {
552+
viewContainerRef: testViewContainerRef,
553+
autoFocus: false
554+
});
555+
556+
viewContainerFixture.detectChanges();
557+
flushMicrotasks();
558+
559+
const focusTrapAnchors = overlayContainerElement.querySelectorAll('.cdk-focus-trap-anchor');
560+
561+
expect(focusTrapAnchors.length).toBeGreaterThan(0);
562+
}));
563+
550564
it('should focus the first tabbable element of the bottom sheet on open when' +
551565
'autoFocus is enabled', fakeAsync(() => {
552566
bottomSheet.open(PizzaMsg, {

0 commit comments

Comments
 (0)