Initial push of Plunk Next

This commit is contained in:
Dries Augustyns
2025-12-01 09:56:56 +01:00
parent 07cea20262
commit ff1876d580
566 changed files with 89036 additions and 28423 deletions
+1
View File
@@ -0,0 +1 @@
export * from './useBeforeUnload';
+28
View File
@@ -0,0 +1,28 @@
import {useEffect} from 'react';
/**
* Hook that warns users before they leave the page with unsaved changes
* Works with both browser navigation (tab close, back button) and Next.js routing
* @param enabled - Whether to show the warning (typically when hasChanges is true)
* @param message - Optional custom message (note: most browsers ignore custom messages)
*/
export function useBeforeUnload(enabled: boolean, message?: string): void {
useEffect(() => {
if (!enabled) return;
const handleBeforeUnload = (e: BeforeUnloadEvent) => {
// Cancel the event to show the browser's confirmation dialog
e.preventDefault();
// Chrome requires returnValue to be set (modern browsers show their own message)
e.returnValue = '';
// Some older browsers use the return value
return '';
};
window.addEventListener('beforeunload', handleBeforeUnload);
return () => {
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, [enabled]);
}