File tree 1 file changed +20
-0
lines changed
1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -48,6 +48,26 @@ function WarnIfUnsavedChanges() {
48
48
49
49
const currentLocation = useLocation ( ) ;
50
50
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.
51
71
return (
52
72
< Prompt
53
73
when = { hasUnsavedChanges }
You can’t perform that action at this time.
0 commit comments