Skip to content

Commit a8e5e31

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 4b15b78 commit a8e5e31

10 files changed

+80
-5
lines changed

src/demo-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/demo-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: {

src/lib/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/lib/dialog/dialog-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,11 @@ export class MatDialogConfig<D = any> {
111111
*/
112112
closeOnNavigation?: boolean = true;
113113

114+
/** Duration of the enter animation. Has to be a valid CSS value (e.g. 100ms). */
115+
enterAnimationDuration?: string = '150ms';
116+
117+
/** Duration of the exit animation. Has to be a valid CSS value (e.g. 50ms). */
118+
exitAnimationDuration?: string = '75ms';
119+
114120
// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
115121
}

src/lib/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/lib/dialog/dialog.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ export class YourDialog {
8080
### Dialog content
8181
Several directives are available to make it easier to structure your dialog content:
8282

83-
| Name | Description |
84-
|-----------------------|---------------------------------------------------------------------------------------------------------------|
83+
| Name | Description |
84+
|------------------------|---------------------------------------------------------------------------------------------------------------|
8585
| `mat-dialog-title` | \[Attr] Dialog title, applied to a heading element (e.g., `<h1>`, `<h2>`) |
8686
| `<mat-dialog-content>` | Primary scrollable content of the dialog |
8787
| `<mat-dialog-actions>` | Container for action buttons at the bottom of the dialog |
@@ -139,6 +139,13 @@ the `ComponentFactory` for it.
139139
export class AppModule {}
140140
```
141141

142+
### Controlling the dialog animation
143+
You can control the duration of the dialog's enter and exit animations using the
144+
`enterAnimationDuration` and `exitAnimationDuration` options. If you want to disable the dialog's
145+
animation completely, you can do so by setting the properties to `0ms`.
146+
147+
<!-- example(dialog-animations) -->
148+
142149
### Accessibility
143150
By default, each dialog has `role="dialog"` on the root element. The role can be changed to
144151
`alertdialog` via the `MatDialogConfig` when opening.
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';
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+
}

0 commit comments

Comments
 (0)