Skip to content

Commit 4a2964e

Browse files
committed
feat: add popup on unsaved changes
1 parent 4fd12af commit 4a2964e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/welcome/admin.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,24 @@ const Settings = () => {
563563
}, [ settings ] )
564564

565565
const hasUnsavedChanges = useMemo( () => Object.keys( unsavedChanges ).length > 0, [ unsavedChanges ] )
566+
567+
useEffect( () => {
568+
const handleBeforeUnload = event => {
569+
if ( hasUnsavedChanges ) {
570+
event.preventDefault()
571+
// Most browsers ignore the custom message, but returning a value triggers the dialog
572+
// https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent/returnValue
573+
event.returnValue = true
574+
}
575+
}
576+
577+
window.addEventListener( 'beforeunload', handleBeforeUnload )
578+
579+
return () => {
580+
window.removeEventListener( 'beforeunload', handleBeforeUnload )
581+
}
582+
}, [ hasUnsavedChanges ] )
583+
566584
const filteredSearchTree = useMemo( () => {
567585
if ( ! currentSearch ) {
568586
return SEARCH_TREE

0 commit comments

Comments
 (0)