Skip to content
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

fix(material/sidenav): disable focus trap while closed #29548

Merged
merged 1 commit into from
Aug 7, 2024
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
23 changes: 23 additions & 0 deletions src/material/sidenav/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,29 @@ describe('MatDrawer', () => {
.withContext('Expected focus trap anchors to be enabled in over mode.')
.toBe(true);
}));

it('should disable the focus trap while closed', fakeAsync(() => {
testComponent.mode = 'over';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
flush();

const anchors = Array.from<HTMLElement>(
fixture.nativeElement.querySelectorAll('.cdk-focus-trap-anchor'),
);
expect(anchors.map(anchor => anchor.getAttribute('tabindex'))).toEqual([null, null]);

drawer.open();
fixture.detectChanges();
flush();
expect(anchors.map(anchor => anchor.getAttribute('tabindex'))).toEqual(['0', '0']);

drawer.close();
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
flush();
expect(anchors.map(anchor => anchor.getAttribute('tabindex'))).toEqual([null, null]);
}));
});

it('should mark the drawer content as scrollable', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/material/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy
if (this._focusTrap) {
// Trap focus only if the backdrop is enabled. Otherwise, allow end user to interact with the
// sidenav content.
this._focusTrap.enabled = !!this._container?.hasBackdrop;
this._focusTrap.enabled = !!this._container?.hasBackdrop && this.opened;
}
}

Expand Down
Loading