Skip to content

Commit 3f0268f

Browse files
crisbetojelbourn
authored andcommitted
feat(dialog): expose current dialog state (#16691)
Exposes whether the dialog is currently open, closed or closing. This is useful when writing components around our dialog. Fixes #16636.
1 parent 17c8983 commit 3f0268f

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/material/dialog/dialog-ref.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import {MatDialogContainer} from './dialog-container';
2020
// Counter for unique dialog ids.
2121
let uniqueId = 0;
2222

23+
/** Possible states of the lifecycle of a dialog. */
24+
export const enum MatDialogState {OPEN, CLOSING, CLOSED}
25+
2326
/**
2427
* Reference to a dialog opened via the MatDialog service.
2528
*/
@@ -45,6 +48,9 @@ export class MatDialogRef<T, R = any> {
4548
/** Handle to the timeout that's running as a fallback in case the exit animation doesn't fire. */
4649
private _closeFallbackTimeout: number;
4750

51+
/** Current state of the dialog. */
52+
private _state = MatDialogState.OPEN;
53+
4854
constructor(
4955
private _overlayRef: OverlayRef,
5056
public _containerInstance: MatDialogContainer,
@@ -108,6 +114,7 @@ export class MatDialogRef<T, R = any> {
108114
.subscribe(event => {
109115
this._beforeClosed.next(dialogResult);
110116
this._beforeClosed.complete();
117+
this._state = MatDialogState.CLOSED;
111118
this._overlayRef.detachBackdrop();
112119

113120
// The logic that disposes of the overlay depends on the exit animation completing, however
@@ -121,6 +128,7 @@ export class MatDialogRef<T, R = any> {
121128
});
122129

123130
this._containerInstance._startExitAnimation();
131+
this._state = MatDialogState.CLOSING;
124132
}
125133

126134
/**
@@ -223,6 +231,11 @@ export class MatDialogRef<T, R = any> {
223231
return this.beforeClosed();
224232
}
225233

234+
/** Gets the current state of the dialog's lifecycle. */
235+
getState(): MatDialogState {
236+
return this._state;
237+
}
238+
226239
/** Fetches the position strategy object from the overlay ref. */
227240
private _getPositionStrategy(): GlobalPositionStrategy {
228241
return this._overlayRef.getConfig().positionStrategy as GlobalPositionStrategy;

src/material/dialog/dialog.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ import {
3434
MatDialog,
3535
MatDialogModule,
3636
MatDialogRef,
37-
MAT_DIALOG_DEFAULT_OPTIONS
37+
MAT_DIALOG_DEFAULT_OPTIONS,
38+
MatDialogState
3839
} from './index';
3940
import {Subject} from 'rxjs';
4041

@@ -755,6 +756,19 @@ describe('MatDialog', () => {
755756
expect(resolver.resolveComponentFactory).toHaveBeenCalled();
756757
}));
757758

759+
it('should return the current state of the dialog', fakeAsync(() => {
760+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
761+
762+
expect(dialogRef.getState()).toBe(MatDialogState.OPEN);
763+
dialogRef.close();
764+
viewContainerFixture.detectChanges();
765+
766+
expect(dialogRef.getState()).toBe(MatDialogState.CLOSING);
767+
flush();
768+
769+
expect(dialogRef.getState()).toBe(MatDialogState.CLOSED);
770+
}));
771+
758772
describe('passing in data', () => {
759773
it('should be able to pass in data', () => {
760774
let config = {

tools/public_api_guard/material/dialog.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,19 @@ export declare class MatDialogRef<T, R = any> {
117117
beforeClose(): Observable<R | undefined>;
118118
beforeClosed(): Observable<R | undefined>;
119119
close(dialogResult?: R): void;
120+
getState(): MatDialogState;
120121
keydownEvents(): Observable<KeyboardEvent>;
121122
removePanelClass(classes: string | string[]): this;
122123
updatePosition(position?: DialogPosition): this;
123124
updateSize(width?: string, height?: string): this;
124125
}
125126

127+
export declare const enum MatDialogState {
128+
OPEN = 0,
129+
CLOSING = 1,
130+
CLOSED = 2
131+
}
132+
126133
export declare class MatDialogTitle implements OnInit {
127134
id: string;
128135
constructor(_dialogRef: MatDialogRef<any>, _elementRef: ElementRef<HTMLElement>, _dialog: MatDialog);

0 commit comments

Comments
 (0)