Skip to content

Commit 626164b

Browse files
authored
fix(material/sidenav): disable focus trap while closed (#29548)
Completely disables the sidenav's focus trap while it's closed so users can't tab to the anchors. Fixes #29545.
1 parent d22a24d commit 626164b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/material/sidenav/drawer.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,29 @@ describe('MatDrawer', () => {
710710
.withContext('Expected focus trap anchors to be enabled in over mode.')
711711
.toBe(true);
712712
}));
713+
714+
it('should disable the focus trap while closed', fakeAsync(() => {
715+
testComponent.mode = 'over';
716+
fixture.changeDetectorRef.markForCheck();
717+
fixture.detectChanges();
718+
flush();
719+
720+
const anchors = Array.from<HTMLElement>(
721+
fixture.nativeElement.querySelectorAll('.cdk-focus-trap-anchor'),
722+
);
723+
expect(anchors.map(anchor => anchor.getAttribute('tabindex'))).toEqual([null, null]);
724+
725+
drawer.open();
726+
fixture.detectChanges();
727+
flush();
728+
expect(anchors.map(anchor => anchor.getAttribute('tabindex'))).toEqual(['0', '0']);
729+
730+
drawer.close();
731+
fixture.changeDetectorRef.markForCheck();
732+
fixture.detectChanges();
733+
flush();
734+
expect(anchors.map(anchor => anchor.getAttribute('tabindex'))).toEqual([null, null]);
735+
}));
713736
});
714737

715738
it('should mark the drawer content as scrollable', () => {

src/material/sidenav/drawer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ export class MatDrawer implements AfterViewInit, AfterContentChecked, OnDestroy
612612
if (this._focusTrap) {
613613
// Trap focus only if the backdrop is enabled. Otherwise, allow end user to interact with the
614614
// sidenav content.
615-
this._focusTrap.enabled = !!this._container?.hasBackdrop;
615+
this._focusTrap.enabled = !!this._container?.hasBackdrop && this.opened;
616616
}
617617
}
618618

0 commit comments

Comments
 (0)