Files
property-management-network/lib/hooks/use-warn-unsaved.ts
T

19 lines
499 B
TypeScript
Raw Normal View History

"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])
}