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="600" breakpointChangeMode>
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not so sure about this API, it doesn't seem to give much flexibility. You can't control what modes it switches between and you can't disable the auto-close/open feature

<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
59 changes: 57 additions & 2 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ import {
ChangeDetectorRef,
} from '@angular/core';
import {animate, state, style, transition, trigger, AnimationEvent} from '@angular/animations';
import {Directionality, coerceBooleanProperty} from '../core';
import {Directionality, coerceBooleanProperty, coerceNumberProperty} 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,31 @@ export class MdDrawerContainer implements AfterContentInit {
/** Event emitted when the drawer backdrop is clicked. */
@Output() backdropClick = new EventEmitter<void>();

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

@Input()
get breakpointWidth() { return 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.

@jelbourn have you added anything to cdk yet for dealing with breakpoints?

set breakpointWidth(v: number) {
this._breakpointWidth = coerceNumberProperty(v, this._breakpointWidth);
}
private _breakpointWidth: number;

/** Whether the drawer changes modes when collapsing. */
@Input()
get breakpointChangeMode() { return this._breakpointChangeMode; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a quick question... why did you remove the return type here?

Copy link
Author

Choose a reason for hiding this comment

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

It wasn't on the number one, so I removed it here

set breakpointChangeMode(value: boolean) {
this._breakpointChangeMode = coerceBooleanProperty(value);
}
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().
*/
private _updateDrawerSubscription: Subscription | null = null;
private _updateDrawer() {}

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

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

this._updateDrawer = () => {
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();
}
}
this._updateDrawerSubscription = startWith.call(resize, null).subscribe(this._updateDrawer);

}

ngOnDestroy() {
if (this._updateDrawerSubscription) {
this._updateDrawerSubscription.unsubscribe();
}
}

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