Skip to content

Commit 444fb38

Browse files
jelbournjosephperrott
authored andcommitted
fix(sidenav): remove margin from content instead of setting zero (#11986)
1 parent d64f94d commit 444fb38

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/lib/sidenav/drawer.spec.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ describe('MatDrawerContainer', () => {
564564
fixture.detectChanges();
565565
tick();
566566

567-
expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin);
567+
expect(contentElement.style.marginLeft).toBe('');
568568
}));
569569

570570
it('should recalculate the margin if the drawer mode is changed', fakeAsync(() => {
@@ -584,7 +584,7 @@ describe('MatDrawerContainer', () => {
584584
fixture.componentInstance.mode = 'over';
585585
fixture.detectChanges();
586586

587-
expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin);
587+
expect(contentElement.style.marginLeft).toBe('');
588588
}));
589589

590590
it('should recalculate the margin if the direction has changed', fakeAsync(() => {
@@ -604,7 +604,7 @@ describe('MatDrawerContainer', () => {
604604
fixture.componentInstance.direction = 'rtl';
605605
fixture.detectChanges();
606606

607-
expect(parseInt(contentElement.style.marginLeft)).toBe(0);
607+
expect(contentElement.style.marginLeft).toBe('');
608608
expect(parseInt(contentElement.style.marginRight)).toBe(margin);
609609
}));
610610

@@ -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

+7-1
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)