Skip to content

Commit 4d278dd

Browse files
crisbetokara
authored andcommitted
fix(drawer): drawer container animating when open by default (#7129)
Fixes #7007.
1 parent f0d20ca commit 4d278dd

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/lib/sidenav/drawer.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ describe('MatDrawerContainer', () => {
327327
declarations: [
328328
DrawerContainerTwoDrawerTestApp,
329329
DrawerDelayed,
330+
DrawerSetToOpenedTrue,
330331
DrawerContainerStateChangesTestApp,
331332
],
332333
});
@@ -418,6 +419,17 @@ describe('MatDrawerContainer', () => {
418419
expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin);
419420
}));
420421

422+
it('should not animate when the sidenav is open on load ', fakeAsync(() => {
423+
const fixture = TestBed.createComponent(DrawerSetToOpenedTrue);
424+
425+
fixture.detectChanges();
426+
tick();
427+
428+
const container = fixture.debugElement.nativeElement.querySelector('.mat-drawer-container');
429+
430+
expect(container.classList).not.toContain('mat-drawer-transition');
431+
}));
432+
421433
});
422434

423435

src/lib/sidenav/drawer.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ import {
3333
} from '@angular/core';
3434
import {DOCUMENT} from '@angular/platform-browser';
3535
import {merge} from 'rxjs/observable/merge';
36-
import {first} from 'rxjs/operator/first';
37-
import {startWith} from 'rxjs/operator/startWith';
38-
import {takeUntil} from 'rxjs/operator/takeUntil';
3936
import {Subject} from 'rxjs/Subject';
37+
import {RxChain, filter, first, startWith, takeUntil} from '@angular/cdk/rxjs';
4038

4139

4240
/** Throws an exception when two MatDrawer are matching the same position. */
@@ -114,7 +112,7 @@ export class MatDrawerContent implements AfterContentInit {
114112
host: {
115113
'class': 'mat-drawer',
116114
'[@transform]': '_animationState',
117-
'(@transform.start)': '_onAnimationStart()',
115+
'(@transform.start)': '_onAnimationStart($event)',
118116
'(@transform.done)': '_onAnimationEnd($event)',
119117
'(keydown)': 'handleKeydown($event)',
120118
// must prevent the browser from aligning text based on value
@@ -174,7 +172,7 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
174172
private _opened: boolean = false;
175173

176174
/** Emits whenever the drawer has started animating. */
177-
_animationStarted = new EventEmitter<void>();
175+
_animationStarted = new EventEmitter<AnimationEvent>();
178176

179177
/** Whether the drawer is animating. Used to prevent overlapping animations. */
180178
_isAnimating = false;
@@ -317,9 +315,9 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
317315
}
318316
}
319317

320-
_onAnimationStart() {
318+
_onAnimationStart(event: AnimationEvent) {
321319
this._isAnimating = true;
322-
this._animationStarted.emit();
320+
this._animationStarted.emit(event);
323321
}
324322

325323
_onAnimationEnd(event: AnimationEvent) {
@@ -448,13 +446,19 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
448446
* is properly hidden.
449447
*/
450448
private _watchDrawerToggle(drawer: MatDrawer): void {
451-
takeUntil.call(drawer._animationStarted, this._drawers.changes).subscribe(() => {
452-
// Set the transition class on the container so that the animations occur. This should not
453-
// be set initially because animations should only be triggered via a change in state.
454-
this._renderer.addClass(this._element.nativeElement, 'mat-drawer-transition');
455-
this._updateContentMargins();
456-
this._changeDetectorRef.markForCheck();
457-
});
449+
RxChain.from(drawer._animationStarted)
450+
.call(takeUntil, this._drawers.changes)
451+
.call(filter, (event: AnimationEvent) => event.fromState !== event.toState)
452+
.subscribe((event: AnimationEvent) => {
453+
// Set the transition class on the container so that the animations occur. This should not
454+
// be set initially because animations should only be triggered via a change in state.
455+
if (event.toState !== 'open-instant') {
456+
this._renderer.addClass(this._element.nativeElement, 'mat-drawer-transition');
457+
}
458+
459+
this._updateContentMargins();
460+
this._changeDetectorRef.markForCheck();
461+
});
458462

459463
if (drawer.mode !== 'side') {
460464
takeUntil.call(merge(drawer.onOpen, drawer.onClose), this._drawers.changes).subscribe(() =>
@@ -463,8 +467,8 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
463467
}
464468

465469
/**
466-
* Subscribes to drawer onPositionChanged event in order to re-validate drawers when the position
467-
* changes.
470+
* Subscribes to drawer onPositionChanged event in order to
471+
* re-validate drawers when the position changes.
468472
*/
469473
private _watchDrawerPosition(drawer: MatDrawer): void {
470474
if (!drawer) {

src/lib/sidenav/sidenav.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class MatSidenavContent extends MatDrawerContent {
6262
'class': 'mat-drawer mat-sidenav',
6363
'tabIndex': '-1',
6464
'[@transform]': '_animationState',
65-
'(@transform.start)': '_onAnimationStart()',
65+
'(@transform.start)': '_onAnimationStart($event)',
6666
'(@transform.done)': '_onAnimationEnd($event)',
6767
'(keydown)': 'handleKeydown($event)',
6868
// must prevent the browser from aligning text based on value

0 commit comments

Comments
 (0)