From 33a343986c08d315b7714d37b68548b6bfd23f9d Mon Sep 17 00:00:00 2001 From: crisbeto Date: Sun, 26 May 2019 12:03:15 +0200 Subject: [PATCH] fix(expansion-panel): header animating on init when using non-default height Works around the expansion panel header animating on init, if its header is different from the default one. The original issue comes from the code introduced in #13088, but we can't remove that code, because it's there to work around a bug in Angular. Fixes #16067. --- .../expansion/expansion-panel-header.ts | 17 +++++++++++++++++ tools/public_api_guard/material/expansion.d.ts | 2 ++ 2 files changed, 19 insertions(+) diff --git a/src/material/expansion/expansion-panel-header.ts b/src/material/expansion/expansion-panel-header.ts index 6fb800e20452..e22b68a7a1d6 100644 --- a/src/material/expansion/expansion-panel-header.ts +++ b/src/material/expansion/expansion-panel-header.ts @@ -58,6 +58,8 @@ import { '[class.mat-expanded]': '_isExpanded()', '(click)': '_toggle()', '(keydown)': '_keydown($event)', + '[@.disabled]': '_animationsDisabled', + '(@expansionHeight.start)': '_animationStarted()', '[@expansionHeight]': `{ value: _getExpandedState(), params: { @@ -70,6 +72,9 @@ import { export class MatExpansionPanelHeader implements OnDestroy, FocusableOption { private _parentChangeSubscription = Subscription.EMPTY; + /** Whether Angular animations in the panel header should be disabled. */ + _animationsDisabled = true; + constructor( @Host() public panel: MatExpansionPanel, private _element: ElementRef, @@ -108,6 +113,18 @@ export class MatExpansionPanelHeader implements OnDestroy, FocusableOption { } } + _animationStarted() { + // Currently the `expansionHeight` animation has a `void => collapsed` transition which is + // there to work around a bug in Angular (see #13088), however this introduces a different + // issue. The new transition will cause the header to animate in on init (see #16067), if the + // consumer has set a header height that is different from the default one. We work around it + // by disabling animations on the header and re-enabling them after the first animation has run. + // Note that Angular dispatches animation events even if animations are disabled. Ideally this + // wouldn't be necessary if we remove the `void => collapsed` transition, but we have to wait + // for https://github.com/angular/angular/issues/18847 to be resolved. + this._animationsDisabled = false; + } + /** Height of the header while the panel is expanded. */ @Input() expandedHeight: string; diff --git a/tools/public_api_guard/material/expansion.d.ts b/tools/public_api_guard/material/expansion.d.ts index 421f70164a6f..cdd7e494c266 100644 --- a/tools/public_api_guard/material/expansion.d.ts +++ b/tools/public_api_guard/material/expansion.d.ts @@ -70,11 +70,13 @@ export declare class MatExpansionPanelDescription { } export declare class MatExpansionPanelHeader implements OnDestroy, FocusableOption { + _animationsDisabled: boolean; collapsedHeight: string; readonly disabled: any; expandedHeight: string; panel: MatExpansionPanel; constructor(panel: MatExpansionPanel, _element: ElementRef, _focusMonitor: FocusMonitor, _changeDetectorRef: ChangeDetectorRef, defaultOptions?: MatExpansionPanelDefaultOptions); + _animationStarted(): void; _getExpandedState(): string; _getPanelId(): string; _isExpanded(): boolean;