UNPKG

mod-arch-shared

Version:

Shared UI components and utilities for modular architecture micro-frontend projects

23 lines 799 B
import { useEffect, useCallback } from 'react'; /** * Use this hook to block the browser from unloading when the user has unsaved changes. * @param shouldBlock - Whether to block the browser from unloading. */ export function useBrowserUnloadBlocker(shouldBlock) { const handleBeforeUnload = useCallback((e) => { if (shouldBlock) { e.preventDefault(); e.returnValue = ''; } }, [shouldBlock]); useEffect(() => { if (!shouldBlock) { return; } window.addEventListener('beforeunload', handleBeforeUnload); return () => { window.removeEventListener('beforeunload', handleBeforeUnload); }; }, [shouldBlock, handleBeforeUnload]); } //# sourceMappingURL=useBrowserUnloadBlocker.js.map