File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -48,6 +48,26 @@ function WarnIfUnsavedChanges() {
4848
4949 const currentLocation = useLocation ( ) ;
5050
51+ // beforeunload handles closing or refreshing the window.
52+ useEffect ( ( ) => {
53+ const handleUnload = ( e ) => {
54+ // See: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#browser_compatibility
55+ e . preventDefault ( ) ;
56+ e . returnValue = t ( 'Nav.WarningUnsavedChanges' ) ;
57+ } ;
58+
59+ if ( hasUnsavedChanges ) {
60+ window . addEventListener ( 'beforeunload' , handleUnload ) ;
61+ } else {
62+ window . removeEventListener ( 'beforeunload' , handleUnload ) ;
63+ }
64+
65+ return ( ) => {
66+ window . removeEventListener ( 'beforeunload' , handleUnload ) ;
67+ } ;
68+ } , [ t , hasUnsavedChanges ] ) ;
69+
70+ // Prompt handles internal navigation between pages.
5171 return (
5272 < Prompt
5373 when = { hasUnsavedChanges }
You can’t perform that action at this time.
0 commit comments