19 lines
499 B
TypeScript
19 lines
499 B
TypeScript
"use client"
|
|||
|
|
|
||
|
|
import { useEffect } from "react"
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Adds a beforeunload warning when the form has unsaved changes.
|
||
|
|
* Pass `isDirty` — set to true as soon as any field changes.
|
||
|
|
*/
|
||
|
|
export function useWarnUnsaved(isDirty: boolean) {
|
||
|
|
useEffect(() => {
|
||
|
|
if (!isDirty) return
|
||
|
|
function handler(e: BeforeUnloadEvent) {
|
||
|
|
e.preventDefault()
|
||
|
|
}
|
||
|
|
window.addEventListener("beforeunload", handler)
|
||
|
|
return () => window.removeEventListener("beforeunload", handler)
|
||
|
|
}, [isDirty])
|
||
|
|
}
|