import { useCallback, useEffect } from "react";
function useBeforeUnload(enabled = true, message) {
  const handler = useCallback(
    (event) => {
      const finalEnabled = typeof enabled === "function" ? enabled() : true;
      if (!finalEnabled) {
        return;
      }
      event.preventDefault();
      {
        event.returnValue = message;
      }
      return message;
    },
    [enabled, message]
  );
  useEffect(() => {
    if (!enabled) {
      return;
    }
    window.addEventListener("beforeunload", handler);
    return () => window.removeEventListener("beforeunload", handler);
  }, [enabled, handler]);
}
export {
  useBeforeUnload as default
};
//# sourceMappingURL=use-beforeunload.js.map
