Skip to content

feat(dialog): expose current dialog state #16691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/material/dialog/dialog-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import {MatDialogContainer} from './dialog-container';
// Counter for unique dialog ids.
let uniqueId = 0;

/** Possible states of the lifecycle of a dialog. */
export const enum MatDialogState {OPEN, CLOSING, CLOSED}

/**
* Reference to a dialog opened via the MatDialog service.
*/
Expand All @@ -45,6 +48,9 @@ export class MatDialogRef<T, R = any> {
/** Handle to the timeout that's running as a fallback in case the exit animation doesn't fire. */
private _closeFallbackTimeout: number;

/** Current state of the dialog. */
private _state = MatDialogState.OPEN;

constructor(
private _overlayRef: OverlayRef,
public _containerInstance: MatDialogContainer,
Expand Down Expand Up @@ -108,6 +114,7 @@ export class MatDialogRef<T, R = any> {
.subscribe(event => {
this._beforeClosed.next(dialogResult);
this._beforeClosed.complete();
this._state = MatDialogState.CLOSED;
this._overlayRef.detachBackdrop();

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

this._containerInstance._startExitAnimation();
this._state = MatDialogState.CLOSING;
}

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

/** Gets the current state of the dialog's lifecycle. */
getState(): MatDialogState {
return this._state;
}

/** Fetches the position strategy object from the overlay ref. */
private _getPositionStrategy(): GlobalPositionStrategy {
return this._overlayRef.getConfig().positionStrategy as GlobalPositionStrategy;
Expand Down
16 changes: 15 additions & 1 deletion src/material/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
MatDialog,
MatDialogModule,
MatDialogRef,
MAT_DIALOG_DEFAULT_OPTIONS
MAT_DIALOG_DEFAULT_OPTIONS,
MatDialogState
} from './index';
import {Subject} from 'rxjs';

Expand Down Expand Up @@ -755,6 +756,19 @@ describe('MatDialog', () => {
expect(resolver.resolveComponentFactory).toHaveBeenCalled();
}));

it('should return the current state of the dialog', fakeAsync(() => {
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});

expect(dialogRef.getState()).toBe(MatDialogState.OPEN);
dialogRef.close();
viewContainerFixture.detectChanges();

expect(dialogRef.getState()).toBe(MatDialogState.CLOSING);
flush();

expect(dialogRef.getState()).toBe(MatDialogState.CLOSED);
}));

describe('passing in data', () => {
it('should be able to pass in data', () => {
let config = {
Expand Down
7 changes: 7 additions & 0 deletions tools/public_api_guard/material/dialog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,19 @@ export declare class MatDialogRef<T, R = any> {
beforeClose(): Observable<R | undefined>;
beforeClosed(): Observable<R | undefined>;
close(dialogResult?: R): void;
getState(): MatDialogState;
keydownEvents(): Observable<KeyboardEvent>;
removePanelClass(classes: string | string[]): this;
updatePosition(position?: DialogPosition): this;
updateSize(width?: string, height?: string): this;
}

export declare const enum MatDialogState {
OPEN = 0,
CLOSING = 1,
CLOSED = 2
}

export declare class MatDialogTitle implements OnInit {
id: string;
constructor(_dialogRef: MatDialogRef<any>, _elementRef: ElementRef<HTMLElement>, _dialog: MatDialog);
Expand Down