Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using responsive API with md-sidenav or other html elements #64

Closed
geelus opened this issue Dec 23, 2016 · 11 comments
Closed

Using responsive API with md-sidenav or other html elements #64

geelus opened this issue Dec 23, 2016 · 11 comments

Comments

@geelus
Copy link

geelus commented Dec 23, 2016

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]>
@ThomasBurleson
Copy link
Contributor

ThomasBurleson commented Dec 23, 2016

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:

@geelus
Copy link
Author

geelus commented Dec 23, 2016

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

@ThomasBurleson
Copy link
Contributor

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

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

@ThomasBurleson
Copy link
Contributor

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

I did not build a Plunkr to verify ;-)

@ThomasBurleson
Copy link
Contributor

@geelus
Copy link
Author

geelus commented Dec 23, 2016

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

@ThomasBurleson
Copy link
Contributor

@geelus - np. Give us a shoutout on Twitter. 🤓

@ThomasBurleson
Copy link
Contributor

ThomasBurleson commented Dec 24, 2016

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

@geelus
Copy link
Author

geelus commented Dec 24, 2016

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

@joejordanbrown
Copy link

joejordanbrown commented Jan 7, 2017

@ThomasBurleson your example has a spelling mistake ngOnDestory

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants