Skip to content

Commit 2b1f84e

Browse files
crisbetommalerba
authored andcommitted
fix(drawer): avoid initial animation when rendering on the server (#8828)
Prevents drawers that are open by default from animating on init if they're rendered on the server. Fixes #6865.
1 parent 3fbb28a commit 2b1f84e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/lib/sidenav/drawer.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {FocusTrap, FocusTrapFactory, FocusMonitor, FocusOrigin} from '@angular/c
1010
import {Directionality} from '@angular/cdk/bidi';
1111
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1212
import {ESCAPE} from '@angular/cdk/keycodes';
13+
import {Platform} from '@angular/cdk/platform';
1314
import {
15+
AfterContentChecked,
1416
AfterContentInit,
1517
ChangeDetectionStrategy,
1618
ChangeDetectorRef,
@@ -139,7 +141,7 @@ export class MatDrawerContent implements AfterContentInit {
139141
encapsulation: ViewEncapsulation.None,
140142
preserveWhitespaces: false,
141143
})
142-
export class MatDrawer implements AfterContentInit, OnDestroy {
144+
export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestroy {
143145
private _focusTrap: FocusTrap;
144146
private _elementFocusedBeforeDrawerWasOpened: HTMLElement | null = null;
145147

@@ -257,6 +259,7 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
257259
constructor(private _elementRef: ElementRef,
258260
private _focusTrapFactory: FocusTrapFactory,
259261
private _focusMonitor: FocusMonitor,
262+
private _platform: Platform,
260263
@Optional() @Inject(DOCUMENT) private _doc: any) {
261264
this.openedChange.subscribe((opened: boolean) => {
262265
if (opened) {
@@ -295,7 +298,16 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
295298
ngAfterContentInit() {
296299
this._focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement);
297300
this._focusTrap.enabled = this._isFocusTrapEnabled;
298-
this._enableAnimations = true;
301+
}
302+
303+
ngAfterContentChecked() {
304+
// Enable the animations after the lifecycle hooks have run, in order to avoid animating
305+
// drawers that are open by default. When we're on the server, we shouldn't enable the
306+
// animations, because we don't want the drawer to animate the first time the user sees
307+
// the page.
308+
if (this._platform.isBrowser) {
309+
this._enableAnimations = true;
310+
}
299311
}
300312

301313
ngOnDestroy() {

src/lib/sidenav/sidenav-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {CommonModule} from '@angular/common';
1212
import {NgModule} from '@angular/core';
1313
import {MatCommonModule} from '@angular/material/core';
1414
import {ScrollDispatchModule} from '@angular/cdk/scrolling';
15+
import {PlatformModule} from '@angular/cdk/platform';
1516
import {MatSidenav, MatSidenavContainer, MatSidenavContent} from './sidenav';
1617
import {
1718
MatDrawer,
@@ -28,6 +29,7 @@ import {
2829
A11yModule,
2930
OverlayModule,
3031
ScrollDispatchModule,
32+
PlatformModule,
3133
],
3234
exports: [
3335
MatCommonModule,

0 commit comments

Comments
 (0)