Skip to content

Commit ba75450

Browse files
committed
add back margins on content
1 parent 71af7f2 commit ba75450

File tree

3 files changed

+84
-36
lines changed

3 files changed

+84
-36
lines changed

src/lib/sidenav/drawer-transitions.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
transition: {
77
duration: $swift-ease-out-duration;
88
timing-function: $swift-ease-out-timing-function;
9-
property: transform, margin-left, margin-right;
9+
property: margin-left, margin-right;
1010
}
1111
}
1212

src/lib/sidenav/drawer.ts

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
NgZone,
2323
OnDestroy,
2424
Inject,
25-
ChangeDetectorRef, ContentChild,
25+
ChangeDetectorRef, ContentChild, forwardRef,
2626
} from '@angular/core';
2727
import {animate, state, style, transition, trigger, AnimationEvent} from '@angular/animations';
2828
import {Directionality, coerceBooleanProperty} from '../core';
@@ -55,11 +55,28 @@ export class MdDrawerToggleResult {
5555
template: '<ng-content></ng-content>',
5656
host: {
5757
'class': 'mat-drawer-content',
58+
'[style.marginLeft.px]': '_margins.left',
59+
'[style.marginRight.px]': '_margins.right',
5860
},
5961
changeDetection: ChangeDetectionStrategy.OnPush,
6062
encapsulation: ViewEncapsulation.None,
6163
})
62-
export class MdDrawerContent {}
64+
export class MdDrawerContent implements AfterContentInit {
65+
/** Margins to be applied to the content. */
66+
_margins: {left: number, right: number} = {left: 0, right: 0};
67+
68+
constructor(
69+
private _changeDetectorRef: ChangeDetectorRef,
70+
@Inject(forwardRef(() => MdDrawerContainer)) private _container: MdDrawerContainer) {
71+
}
72+
73+
ngAfterContentInit() {
74+
this._container._contentMargins.subscribe((margins) => {
75+
this._margins = margins;
76+
this._changeDetectorRef.markForCheck();
77+
});
78+
}
79+
}
6380

6481

6582
/**
@@ -364,8 +381,7 @@ export class MdDrawerContainer implements AfterContentInit {
364381
private _left: MdDrawer | null;
365382
private _right: MdDrawer | null;
366383

367-
/** Inline styles to be applied to the container. */
368-
_styles: { marginLeft: string; marginRight: string; transform: string; };
384+
_contentMargins = new Subject<{left: number, right: number}>();
369385

370386
constructor(@Optional() private _dir: Directionality, private _element: ElementRef,
371387
private _renderer: Renderer2, private _ngZone: NgZone,
@@ -408,7 +424,7 @@ export class MdDrawerContainer implements AfterContentInit {
408424
// Set the transition class on the container so that the animations occur. This should not
409425
// be set initially because animations should only be triggered via a change in state.
410426
this._renderer.addClass(this._element.nativeElement, 'mat-drawer-transition');
411-
this._updateStyles();
427+
this._updateContentMargins();
412428
this._changeDetectorRef.markForCheck();
413429
});
414430

@@ -435,8 +451,10 @@ export class MdDrawerContainer implements AfterContentInit {
435451
/** Subscribes to changes in drawer mode so we can run change detection. */
436452
private _watchDrawerMode(drawer: MdDrawer): void {
437453
if (drawer) {
438-
takeUntil.call(drawer._modeChanged, this._drawers.changes).subscribe(
439-
() => this._changeDetectorRef.markForCheck());
454+
takeUntil.call(drawer._modeChanged, this._drawers.changes).subscribe(() => {
455+
this._updateContentMargins();
456+
this._changeDetectorRef.markForCheck()
457+
});
440458
}
441459
}
442460

@@ -502,29 +520,33 @@ export class MdDrawerContainer implements AfterContentInit {
502520
}
503521

504522
/**
505-
* Return the width of the drawer, if it's in the proper mode and opened.
506-
* This may relayout the view, so do not call this often.
507-
* @param drawer
508-
* @param mode
523+
* Recalculates and updates the inline styles for the content. Note that this should be used
524+
* sparingly, because it causes a reflow.
509525
*/
510-
private _getDrawerEffectiveWidth(drawer: MdDrawer, mode: string): number {
511-
return (this._isDrawerOpen(drawer) && drawer.mode == mode) ? drawer._width : 0;
512-
}
526+
private _updateContentMargins() {
527+
let left = 0;
528+
let right = 0;
529+
530+
if (this._left && this._left.opened) {
531+
if (this._left.mode == 'side') {
532+
left += this._left._width;
533+
} else if (this._left.mode == 'push') {
534+
let width = this._left._width;
535+
left += width;
536+
right -= width;
537+
}
538+
}
513539

514-
/**
515-
* Recalculates and updates the inline styles. Note that this
516-
* should be used sparingly, because it causes a reflow.
517-
*/
518-
private _updateStyles() {
519-
const marginLeft = this._left ? this._getDrawerEffectiveWidth(this._left, 'side') : 0;
520-
const marginRight = this._right ? this._getDrawerEffectiveWidth(this._right, 'side') : 0;
521-
const leftWidth = this._left ? this._getDrawerEffectiveWidth(this._left, 'push') : 0;
522-
const rightWidth = this._right ? this._getDrawerEffectiveWidth(this._right, 'push') : 0;
523-
524-
this._styles = {
525-
marginLeft: `${marginLeft}px`,
526-
marginRight: `${marginRight}px`,
527-
transform: `translate3d(${leftWidth - rightWidth}px, 0, 0)`
528-
};
540+
if (this._right && this._right.opened) {
541+
if (this._right.mode == 'side') {
542+
right += this._right._width;
543+
} else if (this._right.mode == 'push') {
544+
let width = this._right._width;
545+
right += width;
546+
left -= width;
547+
}
548+
}
549+
550+
this._contentMargins.next({left, right});
529551
}
530552
}

src/lib/sidenav/sidenav.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
*/
88

99
import {
10-
ChangeDetectionStrategy,
10+
ChangeDetectionStrategy, ChangeDetectorRef,
1111
Component, ContentChild,
12-
ContentChildren,
12+
ContentChildren, forwardRef, Inject, Input,
1313
ViewEncapsulation
1414
} from '@angular/core';
15-
import {MdDrawer, MdDrawerContainer} from './drawer';
15+
import {MdDrawer, MdDrawerContainer, MdDrawerContent} from './drawer';
1616
import {animate, state, style, transition, trigger} from '@angular/animations';
1717

1818

@@ -22,11 +22,19 @@ import {animate, state, style, transition, trigger} from '@angular/animations';
2222
template: '<ng-content></ng-content>',
2323
host: {
2424
'class': 'mat-drawer-content mat-sidenav-content',
25+
'[style.marginLeft.px]': '_margins.left',
26+
'[style.marginRight.px]': '_margins.right',
2527
},
2628
changeDetection: ChangeDetectionStrategy.OnPush,
2729
encapsulation: ViewEncapsulation.None,
2830
})
29-
export class MdSidenavContent {}
31+
export class MdSidenavContent extends MdDrawerContent {
32+
constructor(
33+
changeDetectorRef: ChangeDetectorRef,
34+
@Inject(forwardRef(() => MdSidenavContainer)) container: MdSidenavContainer) {
35+
super(changeDetectorRef, container);
36+
}
37+
}
3038

3139

3240
@Component({
@@ -49,6 +57,7 @@ export class MdSidenavContent {}
4957
],
5058
host: {
5159
'class': 'mat-drawer mat-sidenav',
60+
'tabIndex': '-1',
5261
'[@transform]': '_animationState',
5362
'(@transform.start)': '_onAnimationStart()',
5463
'(@transform.done)': '_onAnimationEnd($event)',
@@ -59,12 +68,29 @@ export class MdSidenavContent {}
5968
'[class.mat-drawer-over]': 'mode === "over"',
6069
'[class.mat-drawer-push]': 'mode === "push"',
6170
'[class.mat-drawer-side]': 'mode === "side"',
62-
'tabIndex': '-1',
71+
'[class.mat-app-sidenav-fixed]': 'fixedInViewport',
72+
'[style.top.px]': 'fixedInViewport ? fixedTopGap : null',
73+
'[style.bottom.px]': 'fixedInViewport ? fixedBottomGap : null',
6374
},
6475
changeDetection: ChangeDetectionStrategy.OnPush,
6576
encapsulation: ViewEncapsulation.None,
6677
})
67-
export class MdSidenav extends MdDrawer {}
78+
export class MdSidenav extends MdDrawer {
79+
/** Whether the sidenav is fixed in the viewport. */
80+
@Input() fixedInViewport = true;
81+
82+
/**
83+
* The gap between the top of the sidenav and the top of the viewport when the sidenav is in fixed
84+
* mode.
85+
*/
86+
@Input() fixedTopGap = 0;
87+
88+
/**
89+
* The gap between the bottom of the sidenav and the bottom of the viewport when the sidenav is in
90+
* fixed mode.
91+
*/
92+
@Input() fixedBottomGap = 0;
93+
}
6894

6995

7096
@Component({

0 commit comments

Comments
 (0)