Skip to content

Commit eaf18ce

Browse files
committed
Alert user if he close the window before the end of undoable notification
1 parent e047cd1 commit eaf18ce

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

packages/ra-ui-materialui/src/layout/Notification.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export const Notification = (props: NotificationProps) => {
4343
const translate = useTranslate();
4444

4545
useEffect(() => {
46+
const beforeunload = (e: BeforeUnloadEvent) => {
47+
if (messageInfo?.notificationOptions?.undoable) {
48+
e.preventDefault();
49+
}
50+
};
51+
52+
window.addEventListener('beforeunload', beforeunload);
53+
4654
if (notifications.length && !messageInfo) {
4755
// Set a new snack when we don't have an active one
4856
setMessageInfo(takeNotification());
@@ -51,6 +59,10 @@ export const Notification = (props: NotificationProps) => {
5159
// Close an active snack when a new one is added
5260
setOpen(false);
5361
}
62+
63+
return () => {
64+
window.removeEventListener('beforeunload', beforeunload);
65+
};
5466
}, [notifications, messageInfo, open, takeNotification]);
5567

5668
const handleRequestClose = useCallback(() => {

0 commit comments

Comments
 (0)