File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed
Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments