@@ -18,6 +18,7 @@ import {
18
18
useNavigate ,
19
19
useNavigation ,
20
20
useResolvedPath ,
21
+ unstable_useBlocker as useBlocker ,
21
22
UNSAFE_DataRouterContext as DataRouterContext ,
22
23
UNSAFE_DataRouterStateContext as DataRouterStateContext ,
23
24
UNSAFE_NavigationContext as NavigationContext ,
@@ -1210,6 +1211,38 @@ export function useBeforeUnload(
1210
1211
} ;
1211
1212
} , [ callback , capture ] ) ;
1212
1213
}
1214
+
1215
+ /**
1216
+ * Wrapper around useBlocker to show a window.confirm prompt to users instead
1217
+ * of building a custom UI with useBlocker.
1218
+ *
1219
+ * Warning: This has *a lot of rough edges* and behaves very differently (and
1220
+ * very incorrectly in some cases) across browsers if user click addition
1221
+ * back/forward navigations while the confirm is open. Use at your own risk.
1222
+ */
1223
+ function usePrompt ( { when, message } : { when : boolean ; message : string } ) {
1224
+ let blocker = useBlocker ( when ) ;
1225
+
1226
+ React . useEffect ( ( ) => {
1227
+ if ( blocker . state === "blocked" && ! when ) {
1228
+ blocker . reset ( ) ;
1229
+ }
1230
+ } , [ blocker , when ] ) ;
1231
+
1232
+ React . useEffect ( ( ) => {
1233
+ if ( blocker . state === "blocked" ) {
1234
+ let proceed = window . confirm ( message ) ;
1235
+ if ( proceed ) {
1236
+ setTimeout ( blocker . proceed , 0 ) ;
1237
+ } else {
1238
+ blocker . reset ( ) ;
1239
+ }
1240
+ }
1241
+ } , [ blocker , message ] ) ;
1242
+ }
1243
+
1244
+ export { usePrompt as unstable_usePrompt } ;
1245
+
1213
1246
//#endregion
1214
1247
1215
1248
////////////////////////////////////////////////////////////////////////////////
0 commit comments