Skip to content

Commit ce47109

Browse files
authored
fix(material/sidenav): update inert value if mode changes (#33461)
Whether we mark the sidenav content as inert depends on the sidenav's mode. These changes add some code to account for the mode changing after the sidenav has been opened. Fixes #33441.
1 parent a1ce1ad commit ce47109

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

goldens/material/sidenav/index.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ export class MatDrawerContent extends CdkScrollable implements AfterContentInit
122122
// (undocumented)
123123
_container: MatDrawerContainer;
124124
// (undocumented)
125+
_drawerModeChanged(): void;
126+
// (undocumented)
125127
_drawerToggled(drawer: MatDrawer): void;
126128
// (undocumented)
127129
ngAfterContentInit(): void;

src/material/sidenav/drawer.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,32 @@ describe('MatDrawer', () => {
751751

752752
expect(content.hasAttribute('inert')).toBe(false);
753753
});
754+
755+
it('should toggle `inert` on the content if the `mode` changes', async () => {
756+
testComponent.mode = 'over';
757+
fixture.changeDetectorRef.markForCheck();
758+
fixture.detectChanges();
759+
lastFocusableElement.focus();
760+
761+
const content = fixture.nativeElement.querySelector('.mat-drawer-content');
762+
expect(content.hasAttribute('inert')).toBe(false);
763+
764+
drawer.open();
765+
fixture.detectChanges();
766+
await wait(100);
767+
fixture.detectChanges();
768+
expect(content.getAttribute('inert')).toBe('true');
769+
770+
testComponent.mode = 'side';
771+
fixture.changeDetectorRef.markForCheck();
772+
fixture.detectChanges();
773+
expect(content.hasAttribute('inert')).toBe(false);
774+
775+
testComponent.mode = 'over';
776+
fixture.changeDetectorRef.markForCheck();
777+
fixture.detectChanges();
778+
expect(content.getAttribute('inert')).toBe('true');
779+
});
754780
});
755781

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

src/material/sidenav/drawer.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ export class MatDrawerContent extends CdkScrollable implements AfterContentInit
123123
}
124124
}
125125

126+
_drawerModeChanged() {
127+
this._updateInert();
128+
}
129+
126130
private _updateInert() {
127131
const newValue = this._container._isShowingBackdrop();
128132

@@ -235,6 +239,7 @@ export class MatDrawer implements AfterViewInit, OnDestroy {
235239
this._mode = value;
236240
this._updateFocusTrapState();
237241
this._modeChanged.next();
242+
this._getContent()?._drawerModeChanged();
238243
}
239244
private _mode: MatDrawerMode = 'over';
240245

@@ -579,7 +584,7 @@ export class MatDrawer implements AfterViewInit, OnDestroy {
579584
}
580585

581586
this._opened.set(isOpen);
582-
(this._container?._content || this._container?._userContent)?._drawerToggled(this);
587+
this._getContent()?._drawerToggled(this);
583588

584589
if (this._container?._transitionsEnabled) {
585590
// Note: it's important to set this as early as possible,
@@ -613,6 +618,11 @@ export class MatDrawer implements AfterViewInit, OnDestroy {
613618
});
614619
}
615620

621+
/** Gets the current content element. */
622+
private _getContent() {
623+
return this._container?._content || this._container?._userContent;
624+
}
625+
616626
/** Toggles whether the drawer is currently animating. */
617627
private _setIsAnimating(isAnimating: boolean) {
618628
this._elementRef.nativeElement.classList.toggle('mat-drawer-animating', isAnimating);

0 commit comments

Comments
 (0)