@modern-kit/react
Version:
21 lines (18 loc) • 611 B
JavaScript
import { isFunction } from '@modern-kit/utils';
import { useEffect } from 'react';
function useBeforeUnload(enabled = true) {
const enabledToUse = isFunction(enabled) ? enabled() : enabled;
useEffect(() => {
if (!enabledToUse) return;
const handleBeforeUnload = (event) => {
event.preventDefault();
return event.returnValue = "";
};
window.addEventListener("beforeunload", handleBeforeUnload);
return () => {
window.removeEventListener("beforeunload", handleBeforeUnload);
};
}, [enabledToUse]);
}
export { useBeforeUnload };
//# sourceMappingURL=index.mjs.map