I made this simple side nav in angular 2 material. I want to keep oppened="true" only for desktop and keep the default behavior for mobiles and laptops. This used to be present in Angular JS material. How do I implement this in Angular 2 ?
<md-sidenav-container fullscreen>
<md-sidenav #sideNav>
<md-nav-list>
<a>One</a>
<a>Two</a>
</md-nav-list>
</md-sidenav>
<md-toolbar color="primary">
<button md-icon-button (click)="sideNav.toggle()">
<md-icon>menu</md-icon>
</button>
<span>Title</span>
</md-toolbar>
<h1>
{{title}}
</h1>
</md-sidenav-container>
You should be using disableClose property as below
HTML will be as
LIVE DEMO
After struggling myself a bit for this similar post, I used what seems like a bit of a hack that I found at https://github.com/angular/material2/issues/1130, and which I've put in this plunker.
The key in this solution is that it checks the media size on open, and on resize event, showing/hiding the side navigation panel depending on width available. Credit for this goes to sikemullivan
I call it a hack, because handling this through a resize event handler seems like taking out a sledge hammer to solve the problem. I would think there's something easier, but couldn't find anything easier myself.
Another issue with the above, is that if you're like me, maybe you'd like something that keeps open, then you'd need an answer to Prevent md-sidenav from being closed using Escape key when opened
@vinagreti pointed out the incredibly useful angular/flex-layout project, which reintroduces some of the layout functionality that used to be part of Angular Material, but was removed in versions 2+. I was already using that library so it was pretty easy to use it in conjunction with the sidenav from Angular Material to do what you've described here: have a sidenav that is always open on large screens, but toggleable on smaller screens.
You can make use of the disableClose, mode, and opened attributes of the sidenav component to get what you want.
The markup:
The component: