Skip to content

Responsive drawer #6535

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 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
14 changes: 13 additions & 1 deletion src/demo-app/sidenav/sidenav-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1>My Content</h1>
</div>
</md-sidenav-container>

<h2>Sidenav Already Opened</h2>
<h2>Drawer Already Opened</h2>

<md-sidenav-container class="demo-sidenav-container">
<md-sidenav #start2 opened mode="side">
Expand All @@ -48,6 +48,18 @@ <h2>Sidenav Already Opened</h2>
</div>
</md-sidenav-container>

<h2>Responsive Drawer</h2>

<md-sidenav-container class="demo-sidenav-container" breakpointWidth="800" breakpointChangeMode>
<md-sidenav #start3 opened>
Drawer
</md-sidenav>

<div class="demo-sidenav-content">
<button md-button (click)="start3.toggle()">Toggle Start Side Drawer</button>
</div>
</md-sidenav-container>

<h2>Dynamic Position Sidenav</h2>

<md-sidenav-container class="demo-sidenav-container">
Expand Down
53 changes: 52 additions & 1 deletion src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import {animate, state, style, transition, trigger, AnimationEvent} from '@angul
import {Directionality, coerceBooleanProperty} from '../core';
import {FocusTrapFactory, FocusTrap} from '../core/a11y/focus-trap';
import {ESCAPE} from '../core/keyboard/keycodes';
import {first, takeUntil, startWith} from '../core/rxjs/index';
import {first, takeUntil, startWith, auditTime} from '../core/rxjs/index';
import {Subscription} from 'rxjs/Subscription';
import {of as observableOf} from 'rxjs/observable/of';
import {fromEvent} from 'rxjs/observable/fromEvent';
import {DOCUMENT} from '@angular/platform-browser';
import {merge} from 'rxjs/observable/merge';

Expand Down Expand Up @@ -325,6 +328,27 @@ export class MdDrawerContainer implements AfterContentInit {
/** Event emitted when the drawer backdrop is clicked. */
@Output() backdropClick = new EventEmitter<void>();

/**
* Width of the container at which it breaks
* e.g., breakpointWidth="500"
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just,

/** Breakpoint width (in px) at which the sidenav collapses. */

*/
@Input() breakpointWidth: number;
Copy link
Contributor

Choose a reason for hiding this comment

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


/** Whether the drawer changes modes when collapsing. */
@Input()
get breakpointChangeMode(): boolean { return this._breakpointChangeMode; }
set breakpointChangeMode(value: boolean) {
this._breakpointChangeMode = coerceBooleanProperty(value);
}
private _breakpointChangeMode: boolean = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Type should be implicit

private _breakpointChangeMode = true;


/**
* Responsively toggle the drawer using the breakpoint.
* Note that this only works when the window is rezied.
* If you resize it some other way, call _updateDrawer().
*/
_updateDrawer: Subscription | null = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe rename this to _updateDrawerSubscription and then pull out a method named _updateDrawer that can still manually be used? Or just rename it and update the comment.


/** The drawer at the start/end position, independent of direction. */
private _start: MdDrawer | null;
private _end: MdDrawer | null;
Expand Down Expand Up @@ -359,6 +383,33 @@ export class MdDrawerContainer implements AfterContentInit {
this._watchDrawerPosition(drawer);
});
});

const resize = typeof window !== 'undefined' ?
auditTime.call(fromEvent(window, 'resize'), 150) :
observableOf(null);

this._updateDrawer = startWith.call(resize, null).subscribe(() => {
if (this._element.nativeElement.offsetWidth < this.breakpointWidth) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't checked, but this needs to be consistent with any other responsive behavior in this lib or in flex-layout (i.e. < vs <=)

if (this.breakpointChangeMode) {
this._drawers.forEach(drawer => drawer.mode = 'over');
}
this.close();
}
if (this._element.nativeElement.offsetWidth > this.breakpointWidth) {
Copy link
Contributor

Choose a reason for hiding this comment

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

else here sounds better.

Copy link
Author

Choose a reason for hiding this comment

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

Didn't notice that, fixing.

if (this.breakpointChangeMode) {
this._drawers.forEach(drawer => drawer.mode = 'side');
}
this.open();
}
});

}

ngOnDestroy() {
if (this._updateDrawer) {
this._updateDrawer.unsubscribe();
this._updateDrawer = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think setting to null is necessary since the component is being destroyed

}
}

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