Skip to content

feat(sidenav): add responsive sidenav #6525

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/demo-app/sidenav/sidenav-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ <h2>Sidenav Already Opened</h2>
</div>
</md-sidenav-container>

<h2>Responsive Sidenav</h2>

<md-sidenav-container class="demo-sidenav-container" breakpointWidth="500" breakpointChangeMode="true">
<md-sidenav #start2 opened mode="side">
Drawer
</md-sidenav>

<div class="demo-sidenav-content">
The sidenav will responsively open and close, resize your window!
<button md-button (click)="start2.toggle()">Toggle Start Side Drawer</button>
</div>
</md-sidenav-container>

<h2>Dynamic Alignment Sidenav</h2>

<md-sidenav-container class="demo-sidenav-container">
Expand Down
31 changes: 31 additions & 0 deletions src/lib/sidenav/sidenav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
OnDestroy,
Inject,
ChangeDetectorRef,
HostListener
} from '@angular/core';
import {animate, state, style, transition, trigger, AnimationEvent} from '@angular/animations';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
Expand Down Expand Up @@ -316,6 +317,35 @@ export class MdSidenavContainer implements AfterContentInit {
/** The sidenav child with the `end` alignment. */
get end() { return this._end; }

/**
* Width of the container at which it breaks
* e.g., breakpointWidth="500"
*/
@Input() breakpointWidth: number;

/** Mode of the breakpoint; either true or false */
@Input() breakpointChangeMode: true | false = true;

/**
* Responsively toggle the sidenav using the breakpoint.
* Note that this only works when the window is rezied.
* If you resize it some other way, call checkBreakpoint().
*/
@HostListener('window:resize', ['$event'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkBreakpoint() {
if (this._element.nativeElement.offsetWidth < this.breakpointWidth) {
if (this.breakpointChangeMode)
this._sidenavs.forEach(sidenav => sidenav.mode = "over");
Copy link
Contributor

@willshowell willshowell Aug 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use curly braces here and below

this.close();
}
if (this._element.nativeElement.offsetWidth > this.breakpointWidth) {
if (this.breakpointChangeMode)
this._sidenavs.forEach(sidenav => sidenav.mode = "side");
this.open();
}
this._updateStyles();
}

/** Event emitted when the sidenav backdrop is clicked. */
@Output() backdropClick = new EventEmitter<void>();

Expand Down Expand Up @@ -353,6 +383,7 @@ export class MdSidenavContainer implements AfterContentInit {
this._watchSidenavAlign(sidenav);
});
});
this.checkBreakpoint();
}

/** Calls `open` of both start and end sidenavs */
Expand Down