@@ -22,7 +22,7 @@ import {
2222 NgZone ,
2323 OnDestroy ,
2424 Inject ,
25- ChangeDetectorRef , ContentChild ,
25+ ChangeDetectorRef , ContentChild , forwardRef ,
2626} from '@angular/core' ;
2727import { animate , state , style , transition , trigger , AnimationEvent } from '@angular/animations' ;
2828import { 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}
0 commit comments