Skip to content

Commit c70a473

Browse files
authored
fix(material/sidenav): incorrectly trapping focus in some cases (#32699)
In #27355 the sidenav was changed to trap focus if the container has a backdrop. This can be incorrect, because the sidenav that's causing the backdrop to be shown might not be open. These changes switch to enabling focus trapping if the backdrop is actually visible. Fixes #32661.
1 parent c71256d commit c70a473

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/material/sidenav/drawer.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,22 @@ describe('MatDrawer', () => {
714714
flush();
715715
expect(anchors.map(anchor => anchor.getAttribute('tabindex'))).toEqual([null, null]);
716716
}));
717+
718+
it('should not trap focus if the drawer container has a backdrop, but is not showing it', fakeAsync(() => {
719+
fixture.destroy();
720+
721+
const hiddenBackdropFixture = TestBed.createComponent(DrawerWithSideAndHiddenBackdrop);
722+
hiddenBackdropFixture.detectChanges();
723+
tick();
724+
hiddenBackdropFixture.detectChanges();
725+
726+
const anchors = Array.from<HTMLElement>(
727+
hiddenBackdropFixture.nativeElement.querySelectorAll('.cdk-focus-trap-anchor'),
728+
);
729+
730+
expect(anchors.length).toBeGreaterThan(0);
731+
expect(anchors.every(anchor => !anchor.hasAttribute('tabindex'))).toBe(true);
732+
}));
717733
});
718734

719735
it('should mark the drawer content as scrollable', () => {
@@ -1418,3 +1434,16 @@ class NestedDrawerContainers {
14181434
@ViewChild('innerContainer') innerContainer!: MatDrawerContainer;
14191435
@ViewChild('innerDrawer') innerDrawer!: MatDrawer;
14201436
}
1437+
1438+
@Component({
1439+
template: `
1440+
<mat-sidenav-container>
1441+
<mat-sidenav opened mode="side">
1442+
<button>Button inside</button>
1443+
</mat-sidenav>
1444+
<mat-sidenav mode="over" position="end">End content</mat-sidenav>
1445+
</mat-sidenav-container>
1446+
`,
1447+
imports: [MatSidenavModule],
1448+
})
1449+
class DrawerWithSideAndHiddenBackdrop {}

src/material/sidenav/drawer.ts

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

0 commit comments

Comments
 (0)