Skip to content

Commit b0d1bad

Browse files
crisbetojelbourn
authored andcommitted
fix(expansion-panel): header animating on init when using non-default height (#16122)
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.
1 parent b15a7ec commit b0d1bad

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/material/expansion/expansion-panel-header.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ import {MatAccordionTogglePosition} from './accordion-base';
6161
'[class.mat-expansion-toggle-indicator-before]': `_getTogglePosition() === 'before'`,
6262
'(click)': '_toggle()',
6363
'(keydown)': '_keydown($event)',
64+
'[@.disabled]': '_animationsDisabled',
65+
'(@expansionHeight.start)': '_animationStarted()',
6466
'[@expansionHeight]': `{
6567
value: _getExpandedState(),
6668
params: {
@@ -73,6 +75,9 @@ import {MatAccordionTogglePosition} from './accordion-base';
7375
export class MatExpansionPanelHeader implements OnDestroy, FocusableOption {
7476
private _parentChangeSubscription = Subscription.EMPTY;
7577

78+
/** Whether Angular animations in the panel header should be disabled. */
79+
_animationsDisabled = true;
80+
7681
constructor(
7782
@Host() public panel: MatExpansionPanel,
7883
private _element: ElementRef,
@@ -116,6 +121,18 @@ export class MatExpansionPanelHeader implements OnDestroy, FocusableOption {
116121
}
117122
}
118123

124+
_animationStarted() {
125+
// Currently the `expansionHeight` animation has a `void => collapsed` transition which is
126+
// there to work around a bug in Angular (see #13088), however this introduces a different
127+
// issue. The new transition will cause the header to animate in on init (see #16067), if the
128+
// consumer has set a header height that is different from the default one. We work around it
129+
// by disabling animations on the header and re-enabling them after the first animation has run.
130+
// Note that Angular dispatches animation events even if animations are disabled. Ideally this
131+
// wouldn't be necessary if we remove the `void => collapsed` transition, but we have to wait
132+
// for https://github.com/angular/angular/issues/18847 to be resolved.
133+
this._animationsDisabled = false;
134+
}
135+
119136
/** Height of the header while the panel is expanded. */
120137
@Input() expandedHeight: string;
121138

tools/public_api_guard/material/expansion.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ export declare class MatExpansionPanelDescription {
7575
}
7676

7777
export declare class MatExpansionPanelHeader implements OnDestroy, FocusableOption {
78+
_animationsDisabled: boolean;
7879
collapsedHeight: string;
7980
readonly disabled: any;
8081
expandedHeight: string;
8182
panel: MatExpansionPanel;
8283
constructor(panel: MatExpansionPanel, _element: ElementRef, _focusMonitor: FocusMonitor, _changeDetectorRef: ChangeDetectorRef, defaultOptions?: MatExpansionPanelDefaultOptions);
84+
_animationStarted(): void;
8385
_getExpandedState(): string;
8486
_getPanelId(): string;
8587
_getTogglePosition(): MatAccordionTogglePosition;

0 commit comments

Comments
 (0)