Skip to content

Commit

Permalink
fix(sidenav): remove margin from content instead of setting zero (#11986
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jelbourn authored and josephperrott committed Jun 29, 2018
1 parent d64f94d commit 444fb38
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
32 changes: 29 additions & 3 deletions src/lib/sidenav/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ describe('MatDrawerContainer', () => {
fixture.detectChanges();
tick();

expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin);
expect(contentElement.style.marginLeft).toBe('');
}));

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

expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin);
expect(contentElement.style.marginLeft).toBe('');
}));

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

expect(parseInt(contentElement.style.marginLeft)).toBe(0);
expect(contentElement.style.marginLeft).toBe('');
expect(parseInt(contentElement.style.marginRight)).toBe(margin);
}));

Expand Down Expand Up @@ -651,6 +651,32 @@ describe('MatDrawerContainer', () => {
discardPeriodicTasks();
}));

it('should not set a style property if it would be zero', fakeAsync(() => {
const fixture = TestBed.createComponent(AutosizeDrawer);
fixture.detectChanges();

const content = fixture.debugElement.nativeElement.querySelector('.mat-drawer-content');
expect(content.style.marginLeft).toBe('', 'Margin should be omitted when drawer is closed');

// Open the drawer and resolve the open animation.
fixture.componentInstance.drawer.open();
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(content.style.marginLeft).not.toBe('', 'Margin should be present when drawer is open');

// Close the drawer and resolve the close animation.
fixture.componentInstance.drawer.close();
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(content.style.marginLeft).toBe('', 'Margin should be removed after drawer close.');

discardPeriodicTasks();
}));

it('should be able to toggle whether the container has a backdrop', fakeAsync(() => {
const fixture = TestBed.createComponent(BasicTestApp);
fixture.detectChanges();
Expand Down
8 changes: 7 additions & 1 deletion src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,13 @@ export class MatDrawerContainer implements AfterContentInit, DoCheck, OnDestroy
}

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

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

0 comments on commit 444fb38

Please sign in to comment.