Skip to content

Commit 69f7cd1

Browse files
committed
fix(sidenav): remove margin from content instead of setting zero
In SSR scenarious, the sidenav always gets `0` for DOM measurements, causing it to always set a zero margin to the sidenav content. This prevents an app from setting a custom margin so that the sidenav appears correctly after SSR.
1 parent e0bbe07 commit 69f7cd1

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/lib/sidenav/drawer.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,32 @@ describe('MatDrawerContainer', () => {
651651
discardPeriodicTasks();
652652
}));
653653

654+
it('should not set a style property if it would be zero', fakeAsync(() => {
655+
const fixture = TestBed.createComponent(AutosizeDrawer);
656+
fixture.detectChanges();
657+
658+
const content = fixture.debugElement.nativeElement.querySelector('.mat-drawer-content');
659+
expect(content.style.marginLeft).toBe('', 'Margin should be omitted when drawer is closed');
660+
661+
// Open the drawer and resolve the open animation.
662+
fixture.componentInstance.drawer.open();
663+
fixture.detectChanges();
664+
tick();
665+
fixture.detectChanges();
666+
667+
expect(content.style.marginLeft).not.toBe('', 'Margin should be present when drawer is open');
668+
669+
// Close the drawer and resolve the close animation.
670+
fixture.componentInstance.drawer.close();
671+
fixture.detectChanges();
672+
tick();
673+
fixture.detectChanges();
674+
675+
expect(content.style.marginLeft).toBe('', 'Margin should be removed after drawer close.');
676+
677+
discardPeriodicTasks();
678+
}));
679+
654680
it('should be able to toggle whether the container has a backdrop', fakeAsync(() => {
655681
const fixture = TestBed.createComponent(BasicTestApp);
656682
fixture.detectChanges();

src/lib/sidenav/drawer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,13 @@ export class MatDrawerContainer implements AfterContentInit, DoCheck, OnDestroy
703703
}
704704

705705
if (left !== this._contentMargins.left || right !== this._contentMargins.right) {
706-
this._contentMargins = {left, right};
706+
this._contentMargins = {
707+
// If either `right` or `left` is zero, don't set a style to the element. This
708+
// allows users to specify a custom size via CSS class in SSR scenarios where the
709+
// measured widths will always be zero.
710+
left: left || null,
711+
right: right || null,
712+
};
707713

708714
// Pull back into the NgZone since in some cases we could be outside. We need to be careful
709715
// to do it only when something changed, otherwise we can end up hitting the zone too often.

0 commit comments

Comments
 (0)