Skip to content

Commit b984328

Browse files
committedAug 7, 2024·
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. (cherry picked from commit 626164b)
1 parent f229e7b commit b984328

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed
 

‎src/material/sidenav/drawer.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,29 @@ describe('MatDrawer', () => {
714714
.withContext('Expected focus trap anchors to be enabled in over mode.')
715715
.toBe(true);
716716
}));
717+
718+
it('should disable the focus trap while closed', fakeAsync(() => {
719+
testComponent.mode = 'over';
720+
fixture.changeDetectorRef.markForCheck();
721+
fixture.detectChanges();
722+
flush();
723+
724+
const anchors = Array.from<HTMLElement>(
725+
fixture.nativeElement.querySelectorAll('.cdk-focus-trap-anchor'),
726+
);
727+
expect(anchors.map(anchor => anchor.getAttribute('tabindex'))).toEqual([null, null]);
728+
729+
drawer.open();
730+
fixture.detectChanges();
731+
flush();
732+
expect(anchors.map(anchor => anchor.getAttribute('tabindex'))).toEqual(['0', '0']);
733+
734+
drawer.close();
735+
fixture.changeDetectorRef.markForCheck();
736+
fixture.detectChanges();
737+
flush();
738+
expect(anchors.map(anchor => anchor.getAttribute('tabindex'))).toEqual([null, null]);
739+
}));
717740
});
718741

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

‎src/material/sidenav/drawer.ts

+1-1
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)
Please sign in to comment.