Skip to content

Commit 30ba7df

Browse files
committed
fix(material/dialog): allow customizing animation duration
1 parent 49792f4 commit 30ba7df

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/material/dialog/dialog-container.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {MatDialogConfig} from './dialog-config';
2424
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
2525
import {cssClasses, numbers} from '@material/dialog';
2626
import {CdkDialogContainer} from '@angular/cdk/dialog';
27+
import {coerceNumberProperty} from '@angular/cdk/coercion';
2728

2829
/** Event that captures the state of dialog container animations. */
2930
interface LegacyDialogAnimationEvent {
@@ -85,6 +86,22 @@ export abstract class _MatDialogContainerBase extends CdkDialogContainer<MatDial
8586
}
8687
}
8788

89+
const TRANSITION_DURATION_PROPERTY = '--mat-dialog-transition-duration';
90+
91+
function parseCssTime(time: string | undefined): number | null {
92+
if (time == null) {
93+
return null;
94+
}
95+
if (time.endsWith('ms')) {
96+
return coerceNumberProperty(time.substring(0, time.length - 2));
97+
} else if (time.endsWith('s')) {
98+
return coerceNumberProperty(time.substring(0, time.length - 1));
99+
} else if (time === '0') {
100+
return 0;
101+
}
102+
return null; // anything else is invalid.
103+
}
104+
88105
/**
89106
* Internal component that wraps user-provided dialog content in a MDC dialog.
90107
* @docs-private
@@ -117,11 +134,11 @@ export class MatDialogContainer extends _MatDialogContainerBase implements OnDes
117134
private _hostElement: HTMLElement = this._elementRef.nativeElement;
118135
/** Duration of the dialog open animation. */
119136
private _openAnimationDuration = this._animationsEnabled
120-
? numbers.DIALOG_ANIMATION_OPEN_TIME_MS
137+
? parseCssTime(this._config.enterAnimationDuration) ?? numbers.DIALOG_ANIMATION_OPEN_TIME_MS
121138
: 0;
122139
/** Duration of the dialog close animation. */
123140
private _closeAnimationDuration = this._animationsEnabled
124-
? numbers.DIALOG_ANIMATION_CLOSE_TIME_MS
141+
? parseCssTime(this._config.exitAnimationDuration) ?? numbers.DIALOG_ANIMATION_CLOSE_TIME_MS
125142
: 0;
126143
/** Current timer for dialog animations. */
127144
private _animationTimer: number | null = null;
@@ -181,6 +198,10 @@ export class MatDialogContainer extends _MatDialogContainerBase implements OnDes
181198
if (this._animationsEnabled) {
182199
// One would expect that the open class is added once the animation finished, but MDC
183200
// uses the open class in combination with the opening class to start the animation.
201+
this._hostElement.style.setProperty(
202+
TRANSITION_DURATION_PROPERTY,
203+
`${this._openAnimationDuration}ms`,
204+
);
184205
this._hostElement.classList.add(cssClasses.OPENING);
185206
this._hostElement.classList.add(cssClasses.OPEN);
186207
this._waitForAnimationToComplete(this._openAnimationDuration, this._finishDialogOpen);
@@ -203,6 +224,10 @@ export class MatDialogContainer extends _MatDialogContainerBase implements OnDes
203224
this._hostElement.classList.remove(cssClasses.OPEN);
204225

205226
if (this._animationsEnabled) {
227+
this._hostElement.style.setProperty(
228+
TRANSITION_DURATION_PROPERTY,
229+
`${this._openAnimationDuration}ms`,
230+
);
206231
this._hostElement.classList.add(cssClasses.CLOSING);
207232
this._waitForAnimationToComplete(this._closeAnimationDuration, this._finishDialogClose);
208233
} else {

src/material/dialog/dialog.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ $mat-dialog-button-horizontal-margin: 8px !default;
5656
// The dialog container is focusable. We remove the default outline shown in browsers.
5757
outline: 0;
5858

59+
.mdc-dialog__container {
60+
transition-duration: var(--mat-dialog-transition-duration, 0ms);
61+
}
62+
5963
// Angular Material supports disabling all animations when NoopAnimationsModule is imported.
6064
// TODO(devversion): Look into using MDC's Sass queries to separate the animation styles and
6165
// conditionally add them. Consider the size cost and churn when deciding whether to switch.

0 commit comments

Comments
 (0)