Warn on tab close when create-flow module or wizard edits have not been saved.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
adilallo
2026-08-31 11:00:40 -06:00
co-authored by Cursor
parent a820739d07
commit 7f67cc6271
19 changed files with 350 additions and 1 deletions
+33
View File
@@ -0,0 +1,33 @@
"use client";
import { useEffect } from "react";
/**
* Attach the browsers native leave-site prompt while `enabled` is true.
*
* Modern browsers ignore custom copy; this only blocks accidental tab or
* window close. No-ops during SSR and when `enabled` is false so clean
* sessions never register a listener.
*
* @param enabled Whether unsaved work would be lost on unload.
*
* @example
* useBeforeUnloadGuard(isWizardSessionDirty());
*/
export function useBeforeUnloadGuard(enabled: boolean): void {
useEffect(() => {
if (!enabled || typeof window === "undefined") {
return;
}
const onBeforeUnload = (event: BeforeUnloadEvent) => {
event.preventDefault();
event.returnValue = "";
};
window.addEventListener("beforeunload", onBeforeUnload);
return () => {
window.removeEventListener("beforeunload", onBeforeUnload);
};
}, [enabled]);
}