Skip to content

fix(bottom-sheet): focus trap not being attached when autoFocus is disabled #15125

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
Feb 12, 2019
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: 4 additions & 2 deletions src/lib/bottom-sheet/bottom-sheet-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class MatBottomSheetContainer extends BasePortalOutlet implements OnDestr
_onAnimationDone(event: AnimationEvent) {
if (event.toState === 'hidden') {
this._restoreFocus();
} else if (event.toState === 'visible' && this.bottomSheetConfig.autoFocus) {
} else if (event.toState === 'visible') {
this._trapFocus();
}

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

this._focusTrap.focusInitialElementWhenReady();
if (this.bottomSheetConfig.autoFocus) {
this._focusTrap.focusInitialElementWhenReady();
}
}

/** Restores focus to the element that was focused before the bottom sheet was opened. */
Expand Down
14 changes: 14 additions & 0 deletions src/lib/bottom-sheet/bottom-sheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,20 @@ describe('MatBottomSheet', () => {
'Expected bottom sheet container to be focused.');
}));

it('should create a focus trap if autoFocus is disabled', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
autoFocus: false
});

viewContainerFixture.detectChanges();
flushMicrotasks();

const focusTrapAnchors = overlayContainerElement.querySelectorAll('.cdk-focus-trap-anchor');

expect(focusTrapAnchors.length).toBeGreaterThan(0);
}));

it('should focus the first tabbable element of the bottom sheet on open when' +
'autoFocus is enabled', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {
Expand Down