My goal is to display an Angular Material Dialog Box (Popup window) when the user clicks the Chrome Window Close button. The Dialog modal should prompt the user if they want to save changes or cancel.
However, the modal only appears for a brief moment and then closes without waiting for user input. How can I resolve this issue?
How can we detect when the user closes the browser?
@HostListener('window:beforeunload', ['$event'])
beforeunloadHandler(event) {
this.openDocumentSaveDialog();
}
public openDocumentSaveDialog(): void {
const documentSaveDialogRef = this.documentSaveDialog.open(DocumentSaveDialogComponent, {
width: '600px',
height: '200px',
disableClose: true,
autoFocus: false,
data: null
});
documentSaveDialogRef.afterClosed().subscribe(result => {
this.closeMenu.emit()
});
}
https://i.sstatic.net/5U0b1.png
Note: We want to avoid displaying the native Chrome browser popup but instead show a custom popup.
Angular Material Dialog Box: https://material.angular.io/components/dialog