Skip to content

DialogCloseGuard

injectable

Service that provides guards for Material Dialog close operations.

This service allows you to intercept and control dialog close events, enabling confirmation dialogs or preventing accidental closure when there are unsaved changes or pending operations.

Example :

```typescript
readonly #dialogCloseGuard = inject(DialogCloseGuard);

openDialog() { const dialogRef = this.dialog.open(MyComponent);

// Prevent dialog from closing if there are unsaved changes this.#dialogCloseGuard.dialogCanClose(dialogRef, () => { return this.hasUnsavedChanges() ? this.confirmClose() : true; }).subscribe(); }

Example :

Guards against dialog closure when the backdrop is clicked.

This method disables the default close behavior and only allows the dialog to close when the provided check function returns true. The check is triggered specifically when the user clicks on the dialog backdrop.

Example :

Can return a boolean directly or an Observable<boolean>

Example :

```typescript
this.dialogCloseGuard.checkBackdropCanClose(dialogRef, () => {
return this.formIsDirty ? this.showConfirmDialog() : true;
}).subscribe(canClose => {
if (canClose) {
dialogRef.close();
}
});

Example :

checkBackdropCanClose(dialogRef: MatDialogRef<any>, checkFunction: function): Observable<boolean>
NameTypeDescription
dialogRefMatDialogRef<any>
checkFunctionfunction

Guards against dialog closure when the Escape key is pressed.

This method disables the default close behavior and only allows the dialog to close when the provided check function returns true. The check is triggered specifically when the user presses the Escape key.

Example :

Can return a boolean directly or an Observable<boolean>

Example :

```typescript
this.dialogCloseGuard.checkKeydownEventsCanClose(dialogRef, () => {
return this.hasUnsavedChanges ? this.confirmDiscardChanges() : true;
}).subscribe(canClose => {
if (canClose) {
dialogRef.close();
}
});

Example :

checkKeydownEventsCanClose(dialogRef: MatDialogRef<any>, checkFunction: function): Observable<boolean>
NameTypeDescription
dialogRefMatDialogRef<any>
checkFunctionfunction

Specialized dialog close guard that automatically checks for pending changes.

This convenience method provides automatic pending changes detection without requiring a custom check function. It integrates with the PendingChangesService to determine if there are any unsaved changes, and handles both backdrop clicks and Escape key presses automatically.

This method is ideal when you want to prevent dialog closure if there are pending changes anywhere in the application, without having to implement custom change detection logic.

Example :

preventing closure, or void when the dialog
is successfully closed
pendingChangesDialogCanClose(dialogRef: MatDialogRef<T>): Observable< | void>
NameTypeDescription
dialogRefMatDialogRef<T>

Comprehensive dialog close guard that handles both backdrop clicks and Escape key presses.

This method provides a unified approach to guard dialog closure by monitoring both backdrop clicks and Escape key events. When either event occurs, it runs the provided check function and automatically closes the dialog if the check passes.

This is the most convenient method when you want to guard against all common dialog close scenarios with a single setup.

Example :

Can return a boolean directly or an Observable<boolean>
or void when the dialog is successfully closed

Example :

```typescript
// Simple boolean check
this.dialogCloseGuard.dialogCanClose(dialogRef, () => !this.hasUnsavedChanges).subscribe();

// Async check with confirmation dialog this.dialogCloseGuard.dialogCanClose(dialogRef, () => { return this.hasUnsavedChanges ? this.confirmationService.confirm(‘Discard changes?’) : of(true); }).subscribe();

Example :

preventCloseUntil(dialogRef: MatDialogRef<T>, checkFunction: function): Observable< | void>
NameTypeDescription
dialogRefMatDialogRef<T>
checkFunctionfunction