From 4d278dd91eeb895fff0c28a0edb874901df8561c Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 4 Oct 2017 23:53:33 +0200 Subject: [PATCH] fix(drawer): drawer container animating when open by default (#7129) Fixes #7007. --- src/lib/sidenav/drawer.spec.ts | 12 ++++++++++++ src/lib/sidenav/drawer.ts | 36 +++++++++++++++++++--------------- src/lib/sidenav/sidenav.ts | 2 +- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/lib/sidenav/drawer.spec.ts b/src/lib/sidenav/drawer.spec.ts index ce39efa443dc..c763866bd0c3 100644 --- a/src/lib/sidenav/drawer.spec.ts +++ b/src/lib/sidenav/drawer.spec.ts @@ -327,6 +327,7 @@ describe('MatDrawerContainer', () => { declarations: [ DrawerContainerTwoDrawerTestApp, DrawerDelayed, + DrawerSetToOpenedTrue, DrawerContainerStateChangesTestApp, ], }); @@ -418,6 +419,17 @@ describe('MatDrawerContainer', () => { expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin); })); + it('should not animate when the sidenav is open on load ', fakeAsync(() => { + const fixture = TestBed.createComponent(DrawerSetToOpenedTrue); + + fixture.detectChanges(); + tick(); + + const container = fixture.debugElement.nativeElement.querySelector('.mat-drawer-container'); + + expect(container.classList).not.toContain('mat-drawer-transition'); + })); + }); diff --git a/src/lib/sidenav/drawer.ts b/src/lib/sidenav/drawer.ts index 216ef4cf26f8..4691afe6e045 100644 --- a/src/lib/sidenav/drawer.ts +++ b/src/lib/sidenav/drawer.ts @@ -33,10 +33,8 @@ import { } from '@angular/core'; import {DOCUMENT} from '@angular/platform-browser'; import {merge} from 'rxjs/observable/merge'; -import {first} from 'rxjs/operator/first'; -import {startWith} from 'rxjs/operator/startWith'; -import {takeUntil} from 'rxjs/operator/takeUntil'; import {Subject} from 'rxjs/Subject'; +import {RxChain, filter, first, startWith, takeUntil} from '@angular/cdk/rxjs'; /** Throws an exception when two MatDrawer are matching the same position. */ @@ -114,7 +112,7 @@ export class MatDrawerContent implements AfterContentInit { host: { 'class': 'mat-drawer', '[@transform]': '_animationState', - '(@transform.start)': '_onAnimationStart()', + '(@transform.start)': '_onAnimationStart($event)', '(@transform.done)': '_onAnimationEnd($event)', '(keydown)': 'handleKeydown($event)', // must prevent the browser from aligning text based on value @@ -174,7 +172,7 @@ export class MatDrawer implements AfterContentInit, OnDestroy { private _opened: boolean = false; /** Emits whenever the drawer has started animating. */ - _animationStarted = new EventEmitter(); + _animationStarted = new EventEmitter(); /** Whether the drawer is animating. Used to prevent overlapping animations. */ _isAnimating = false; @@ -317,9 +315,9 @@ export class MatDrawer implements AfterContentInit, OnDestroy { } } - _onAnimationStart() { + _onAnimationStart(event: AnimationEvent) { this._isAnimating = true; - this._animationStarted.emit(); + this._animationStarted.emit(event); } _onAnimationEnd(event: AnimationEvent) { @@ -448,13 +446,19 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy { * is properly hidden. */ private _watchDrawerToggle(drawer: MatDrawer): void { - takeUntil.call(drawer._animationStarted, this._drawers.changes).subscribe(() => { - // Set the transition class on the container so that the animations occur. This should not - // be set initially because animations should only be triggered via a change in state. - this._renderer.addClass(this._element.nativeElement, 'mat-drawer-transition'); - this._updateContentMargins(); - this._changeDetectorRef.markForCheck(); - }); + RxChain.from(drawer._animationStarted) + .call(takeUntil, this._drawers.changes) + .call(filter, (event: AnimationEvent) => event.fromState !== event.toState) + .subscribe((event: AnimationEvent) => { + // Set the transition class on the container so that the animations occur. This should not + // be set initially because animations should only be triggered via a change in state. + if (event.toState !== 'open-instant') { + this._renderer.addClass(this._element.nativeElement, 'mat-drawer-transition'); + } + + this._updateContentMargins(); + this._changeDetectorRef.markForCheck(); + }); if (drawer.mode !== 'side') { takeUntil.call(merge(drawer.onOpen, drawer.onClose), this._drawers.changes).subscribe(() => @@ -463,8 +467,8 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy { } /** - * Subscribes to drawer onPositionChanged event in order to re-validate drawers when the position - * changes. + * Subscribes to drawer onPositionChanged event in order to + * re-validate drawers when the position changes. */ private _watchDrawerPosition(drawer: MatDrawer): void { if (!drawer) { diff --git a/src/lib/sidenav/sidenav.ts b/src/lib/sidenav/sidenav.ts index b152fd0fa7b9..04c7a8b8a407 100644 --- a/src/lib/sidenav/sidenav.ts +++ b/src/lib/sidenav/sidenav.ts @@ -62,7 +62,7 @@ export class MatSidenavContent extends MatDrawerContent { 'class': 'mat-drawer mat-sidenav', 'tabIndex': '-1', '[@transform]': '_animationState', - '(@transform.start)': '_onAnimationStart()', + '(@transform.start)': '_onAnimationStart($event)', '(@transform.done)': '_onAnimationEnd($event)', '(keydown)': 'handleKeydown($event)', // must prevent the browser from aligning text based on value