[MdSidenav] - Sidenav Resizing Regression
Bug, feature request, or proposal:
Bug/Regression from 2.0.0-beta.8.
Also a feature request for an API to allow triggering recalculations for the MdSidenavContainer/MdDrawerContainer, or being able to update _styles manually and having the view update..
What is the expected behavior?
In the previous release 2.0.0-beta.8 the sidenav resized when the window was resized.
I have a responsive sidenav that uses Angular flex-layout for showing/hiding responsive content in the sidenav when the view changes from md to lg and vice-versa.
What is the current behavior?
In 2.0.0-beta.10 this is no longer the case. The changes made in #6189 made the sidenav update only when the sidenav is opening/closing.
Since flex-layout's changes occur without having to call open() or close() on the sidenav, the width recalculation doesn't occur.

What are the steps to reproduce?
Put dynamic/responsive content within a sidenav that is configured with "side".
What is the use-case or motivation for changing an existing behavior?
Being able to dynamically call a refresh of the MdSidenav/MdDrawer or MdSidenavContainer/MdDrawerContainer.
Even better would be an accessor to allow manually setting the margin of the sidenav to allow for pre-computing the _style variable and updating it when needed.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular 4.3.5 Angular Material 2.0.0-beta.10
Is there anything else we should know?
This is similar to #6583.
For right now I am using the following hack in my navigation directive to workaround whenever the ObservableMedia from angular flex-layout changes:
// Timeout required for flex-layout directives to update the child views/the width.
setTimeout(() => {
(this._container as any)._updateStyles();
(this._container as any)._changeDetectorRef.markForCheck();
}, 0);
+1. Same issue I have after upgrading to 2.0.0-beta.10
Same issue after upgrading from 2.0.0-beta.8 to 2.0.0-beta.10
My workarround :
app.component.html
<md-sidenav class="main-sidenav" #sidenav mode="side" opened="false">
app.component.ts
export class AppComponent implements AfterViewInit {
@ViewChild('sidenav')
private sidenav: MdSidenav;
ngAfterViewInit(): void {
setTimeout(() => {
this.sidenav.open();
}, 250);
}
}
that is exactly what i did @sbeaufort. It creates a small animation at the first page load but its ok
I have the exact same issue.
Here is my solution: HTML
<md-sidenav-container #container>
<md-sidenav mode="side" opened="{{showMenu}}" class="shadow"></md-sidenav>
<div class="my-content"></div>
</md-sidenav-container>
TS
export class MyComponent implements OnInit, AfterViewInit {
@ViewChild('container') private _container;
public showMenu: boolean = false;
...
ngAfterViewInit() {
setTimeout(()=>{
this.showMenu = true;
},0);
this._container._ngZone.onMicrotaskEmpty.subscribe(() => {
this._container._updateStyles();
this._container._changeDetectorRef.markForCheck();
})
};
...
}
@ghwyf Thank you very much! This workaround also worked for me.
Same issue for me upgrading to 2.0.0-beta.10. I used @ghwyf fix which works perfectly, thank you.
Closing as duplicate of #1130
Join the responses of @angularexample in #1130 and @ghwyf here, works for me. ngAfterViewInit seems too heavy for my application so I replace it with a specific call only when Subscription happens.
@josephperrott I think the issues are about different things. The one you referenced is about opening and closing the sidenav automatically when viewport width changes. This one is about being able to resize the sidenav width while keeping it open.
Before the last update this worked with no problem. Right now, the main content is not being resized when the sidebar width is resized. Some people have shared workarounds for this, but I think it would be nice if Angular Material would support this use case which is quite popular in places like firebase.
| Opened | Closed |
|---|---|
![]() |
![]() |
Reopening, sorry I misunderstood a bit there.
It would be really helpful to give a guide on this. Are we going to do this with animations? Will there be a function on the sidenav (like sidenav.resize(width))?
I am on the material2-builds and use the new fixedInViewport, which is great. But when I animate the sidenav from 220px to 75px the main content section does not adjust (as it used to before).
Any comments as to what we will expect here?
Same issue here.
After a while, I was forced to come back to 2.0.0-beta.8 when sidenav works perfect.
I would like to know if material team thinks this has to be fixed. If not, I will apply a workaround and stop waiting for a fix.
Thanks a lot!
Same problem here after upgrading to 2.0.0-beta.10 . The fix from @ghwyf doesn't work for me with angular 4.4.4
Here's my temporary workaround to fix it:
<md-sidenav-container #sideNavContainer>
<md-sidenav #sideNav align="start" mode="{{navMode}}" opened="true"
[@sideNavAnim] (@sideNavAnim.done)="onSideNavAnimDone($event)">
<!-- sidenav content -->
</md-sidenav>
<!-- primary content -->
<router-outlet *ngIf="sideNavAnimFinished"></router-outlet>
</md-sidenav-container>
export class MyComponent {
navMode = 'side';
sideNavAnimFinished = false;
...
onSideNavAnimDone(event) {
this.sideNavAnimFinished = true;
}
}
the example mentioned by @sbeaufort does not work well when the component has changeDetection: ChangeDetectionStrategy.OnPush. In order to make it work you need to manually detect changes to the UI, example:
ngAfterViewInit(): void {
const size = this.drawerState === 'open' ? 220 : 75;
setTimeout(() => {
this.sidenav.open();
this.cd.detectChanges();
}, size);
}
@crisbeto is the resize function on a roadmap for sidenav? Would it not suffice to add a "closeTo" option which, when in side mode and when set, does not transform to visibility hidden but sets the drawer width to whatever was closeTo was set to?
I was using @ghwyf workaround until beta.11 as well. What worked for me in beta.12 is the following:
export class MyComponent implements OnInit, AfterViewInit {
@ViewChild('container') private _container;
public showMenu: boolean = false;
...
ngAfterViewInit() {
setTimeout(()=>{
this.showMenu = true;
},0);
this._container._ngZone.onMicrotaskEmpty.subscribe(() => {
this._container._updateContentMargins();
this._container._changeDetectorRef.markForCheck();
})
};
...
}

