Flex-layout: Using responsive API with md-sidenav or other html elements

Created on 23 Dec 2016  路  11Comments  路  Source: angular/flex-layout

At the moment thanks for Flex-Layout we can easily show or hide an element using the breakpoints.

Is there anyway to use flex-api to open/close the side nav in different resolutions? Something lik:

<md-sidenav opened.gt-sm>side nav contents </md-sidenav]>

Most helpful comment

So that is not currently possible. Only the Flex-Layout API understands the responsive aliases (e.g. .gt-md). But you can use an inject MatchMediaObservable; which allows you to be easily notified when a mediaQuery activates.

html, body, material-app, md-sidenav-container, .my-content {
  margin: 0;
  width: 100%;
  height: 100%;
}
<app>
     <responsive-sidenav>
        <div sidenav-content>
            Hello, I am a responsive sideNav component
       </div>
       <div  main-content>
           Hello, I am the primary, main content
       </div>
     </responsive-sidenav>
</app>

Now let's use multi-slot, content projection within our Responsive-Sidenav component:

@Component({  
   selector : 'responsive-sidenav',
   template : `
     <md-sidenav-container>
      <md-sidenav mode="side" [opened]="isOpen">
        <ng-content  select="[sidenav-content]">Projected Drawer Content</ng-content>
      </md-sidenav>    
      <div class="my-content">
        <ng-content  select="[main-content]">Main content</ng-content>
     </div>
    </md-sidenav-container>
`
})
export class ResponsiveSideNav implements OnDestroy {
   private var _subscription; 
   isOpen = true;

   constructor(@Inject(MatchMediaObservable) media$) {
      this._subscription = media$.subscribe(change:MediaChange) => {
          let isMobile = (change.alias == 'xs') || (change.alias == 'sm');
          this.isOpen = !isMobile;
      });
   }

  ngOnDestory() {
    this._subscription.unsubscribe();
  }
}

Demos

Also you can review the source to our online Demo App:

All 11 comments

So that is not currently possible. Only the Flex-Layout API understands the responsive aliases (e.g. .gt-md). But you can use an inject MatchMediaObservable; which allows you to be easily notified when a mediaQuery activates.

html, body, material-app, md-sidenav-container, .my-content {
  margin: 0;
  width: 100%;
  height: 100%;
}
<app>
     <responsive-sidenav>
        <div sidenav-content>
            Hello, I am a responsive sideNav component
       </div>
       <div  main-content>
           Hello, I am the primary, main content
       </div>
     </responsive-sidenav>
</app>

Now let's use multi-slot, content projection within our Responsive-Sidenav component:

@Component({  
   selector : 'responsive-sidenav',
   template : `
     <md-sidenav-container>
      <md-sidenav mode="side" [opened]="isOpen">
        <ng-content  select="[sidenav-content]">Projected Drawer Content</ng-content>
      </md-sidenav>    
      <div class="my-content">
        <ng-content  select="[main-content]">Main content</ng-content>
     </div>
    </md-sidenav-container>
`
})
export class ResponsiveSideNav implements OnDestroy {
   private var _subscription; 
   isOpen = true;

   constructor(@Inject(MatchMediaObservable) media$) {
      this._subscription = media$.subscribe(change:MediaChange) => {
          let isMobile = (change.alias == 'xs') || (change.alias == 'sm');
          this.isOpen = !isMobile;
      });
   }

  ngOnDestory() {
    this._subscription.unsubscribe();
  }
}

Demos

Also you can review the source to our online Demo App:

You guys are awesome! Thank you very much for your rapid response. I didn't expect it to be this quick honestly. Thanks Thomas

@geelus - In the future, please post usage questions:

Note: the Gitter room is not yet available. Check back after X-mas.

Thx @geelus . Sorry, I just made some corrections to my sample code ^.

I did not build a Plunkr to verify ;-)

Sure, I will. thanks again for your great help.

@geelus - np. Give us a shoutout on Twitter. 馃

@geelus - I prepared a Responsive-SideNav Plunkr showing how to use the MatchMediaObservable... only to discover a major bug: see issue #65

The Plunkr ^ has a work around and shows have a sidenav is now responsive.

Thanks for posting this issue... it helped us make the library better!

@ThomasBurleson glad I could help. Yesterday I used your solution and it worked like a charm! I didn't notice the bug, maybe because I was so excited to see this working!

Merry Christmas and Happy new year!

@ThomasBurleson your example has a spelling mistake ngOnDestory

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings