Skip to content

Commit 15a1d63

Browse files
committed
feat(dialog): add the ability to control the animation duration
Since the dialog animation is on the `MatDialogContainer`, consumers aren't able to disable the animation. These changes add properties to the dialog config that allow consumers to set the duration of the dialog's enter and exit animations. Fixes #3616.
1 parent 7f6972f commit 15a1d63

12 files changed

+113
-3
lines changed

src/dev-app/dialog/dialog-demo.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ <h2>Other options</h2>
102102
</mat-form-field>
103103
</p>
104104

105+
<p>
106+
<mat-form-field>
107+
<mat-label>Enter duration</mat-label>
108+
<input matInput [(ngModel)]="config.enterAnimationDuration">
109+
</mat-form-field>
110+
<mat-form-field>
111+
<mat-label>Exit duration</mat-label>
112+
<input matInput [(ngModel)]="config.exitAnimationDuration">
113+
</mat-form-field>
114+
</p>
115+
105116
<p>
106117
<mat-checkbox [(ngModel)]="config.disableClose">Disable close</mat-checkbox>
107118
</p>

src/dev-app/dialog/dialog-demo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export class DialogDemo {
3333
height: '',
3434
minWidth: '',
3535
minHeight: '',
36+
enterAnimationDuration: defaultDialogConfig.enterAnimationDuration,
37+
exitAnimationDuration: defaultDialogConfig.exitAnimationDuration,
3638
maxWidth: defaultDialogConfig.maxWidth,
3739
maxHeight: '',
3840
position: {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h1 mat-dialog-title>Delete file</h1>
2+
<div mat-dialog-content>
3+
Would you like to delete cat.jpeg?
4+
</div>
5+
<div mat-dialog-actions>
6+
<button mat-button mat-dialog-close>No</button>
7+
<button mat-button mat-dialog-close cdkFocusInitial>Ok</button>
8+
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
button {
2+
margin-right: 8px;
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<button mat-raised-button (click)="openDialog('0ms', '0ms')">Open dialog without animation</button>
2+
<button mat-raised-button (click)="openDialog('3000ms', '1500ms')">Open dialog slowly</button>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {Component} from '@angular/core';
2+
import {MatDialog, MatDialogRef} from '@angular/material/dialog';
3+
4+
/**
5+
* @title Dialog Animations
6+
*/
7+
@Component({
8+
selector: 'dialog-animations-example',
9+
styleUrls: ['dialog-animations-example.css'],
10+
templateUrl: 'dialog-animations-example.html',
11+
})
12+
export class DialogAnimationsExample {
13+
constructor(public dialog: MatDialog) {}
14+
15+
openDialog(enterAnimationDuration: string, exitAnimationDuration: string): void {
16+
this.dialog.open(DialogAnimationsExampleDialog, {
17+
width: '250px',
18+
enterAnimationDuration,
19+
exitAnimationDuration
20+
});
21+
}
22+
}
23+
24+
@Component({
25+
selector: 'dialog-animations-example-dialog',
26+
templateUrl: 'dialog-animations-example-dialog.html',
27+
})
28+
export class DialogAnimationsExampleDialog {
29+
constructor(public dialogRef: MatDialogRef<DialogAnimationsExampleDialog>) {}
30+
}

src/material/dialog/dialog-animations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const animationBody = [
2020
// decimate the animation performance. Leaving it as `none` solves both issues.
2121
state('void, exit', style({opacity: 0, transform: 'scale(0.7)'})),
2222
state('enter', style({transform: 'none'})),
23-
transition('* => enter', animate('150ms cubic-bezier(0, 0, 0.2, 1)',
23+
transition('* => enter', animate('{{enterAnimationDuration}} cubic-bezier(0, 0, 0.2, 1)',
2424
style({transform: 'none', opacity: 1}))),
2525
transition('* => void, * => exit',
26-
animate('75ms cubic-bezier(0.4, 0.0, 0.2, 1)', style({opacity: 0}))),
26+
animate('{{exitAnimationDuration}} cubic-bezier(0.4, 0.0, 0.2, 1)', style({opacity: 0}))),
2727
];
2828

2929
/**

src/material/dialog/dialog-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,11 @@ export class MatDialogConfig<D = any> {
117117
/** Alternate `ComponentFactoryResolver` to use when resolving the associated component. */
118118
componentFactoryResolver?: ComponentFactoryResolver;
119119

120+
/** Duration of the enter animation. Has to be a valid CSS value (e.g. 100ms). */
121+
enterAnimationDuration?: string = '150ms';
122+
123+
/** Duration of the exit animation. Has to be a valid CSS value (e.g. 50ms). */
124+
exitAnimationDuration?: string = '75ms';
125+
120126
// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
121127
}

src/material/dialog/dialog-container.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ export function throwMatDialogContentAlreadyAttachedError() {
6565
'[attr.aria-labelledby]': '_config.ariaLabel ? null : _ariaLabelledBy',
6666
'[attr.aria-label]': '_config.ariaLabel',
6767
'[attr.aria-describedby]': '_config.ariaDescribedBy || null',
68-
'[@dialogContainer]': '_state',
68+
'[@dialogContainer]': `{
69+
value: _state,
70+
params: {
71+
enterAnimationDuration: _config.enterAnimationDuration,
72+
exitAnimationDuration: _config.exitAnimationDuration
73+
}
74+
}`,
6975
'(@dialogContainer.start)': '_onAnimationStart($event)',
7076
'(@dialogContainer.done)': '_onAnimationDone($event)',
7177
},

src/material/dialog/dialog.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,44 @@ You can control which elements are tab stops with the `tabindex` attribute
149149

150150
<!-- example(dialog-content) -->
151151

152+
### Configuring dialog content via `entryComponents`
153+
154+
Because `MatDialog` instantiates components at run-time, the Angular compiler needs extra
155+
information to create the necessary `ComponentFactory` for your dialog content component.
156+
157+
For any component loaded into a dialog, you must include your component class in the list of
158+
`entryComponents` in your NgModule definition so that the Angular compiler knows to create
159+
the `ComponentFactory` for it.
160+
161+
```ts
162+
@NgModule({
163+
imports: [
164+
// ...
165+
MatDialogModule
166+
],
167+
168+
declarations: [
169+
AppComponent,
170+
ExampleDialogComponent
171+
],
172+
173+
entryComponents: [
174+
ExampleDialogComponent
175+
],
176+
177+
providers: [],
178+
bootstrap: [AppComponent]
179+
})
180+
export class AppModule {}
181+
```
182+
183+
### Controlling the dialog animation
184+
You can control the duration of the dialog's enter and exit animations using the
185+
`enterAnimationDuration` and `exitAnimationDuration` options. If you want to disable the dialog's
186+
animation completely, you can do so by setting the properties to `0ms`.
187+
188+
<!-- example(dialog-animations) -->
189+
152190
### Accessibility
153191
By default, each dialog has `role="dialog"` on the root element. The role can be changed to
154192
`alertdialog` via the `MatDialogConfig` when opening.

src/material/dialog/dialog.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,8 @@ describe('MatDialog with default options', () => {
14271427
minHeight: '50px',
14281428
maxWidth: '150px',
14291429
maxHeight: '150px',
1430+
enterAnimationDuration: '100ms',
1431+
exitAnimationDuration: '50ms',
14301432
autoFocus: false,
14311433
};
14321434

tools/public_api_guard/material/dialog.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export declare class MatDialogConfig<D = any> {
6666
data?: D | null;
6767
direction?: Direction;
6868
disableClose?: boolean;
69+
enterAnimationDuration?: string;
70+
exitAnimationDuration?: string;
6971
hasBackdrop?: boolean;
7072
height?: string;
7173
id?: string;

0 commit comments

Comments
 (0)