diff --git a/modules/core/src/web/hooks/use-unsaved-changes-notifier/use-unsaved-changes-notifier.tsx b/modules/core/src/web/hooks/use-unsaved-changes-notifier/use-unsaved-changes-notifier.tsx index 59e07acf..eb0e9381 100644 --- a/modules/core/src/web/hooks/use-unsaved-changes-notifier/use-unsaved-changes-notifier.tsx +++ b/modules/core/src/web/hooks/use-unsaved-changes-notifier/use-unsaved-changes-notifier.tsx @@ -49,12 +49,12 @@ export const UnsavedChangesProvider = ({ isDirty, children }: UnsavedChangesProv }); }, [isDirty]); - const handleConfirm = useCallback((discardChanges: boolean) => { - if (discardChanges) { + const handleConfirm = useCallback((confirmed: boolean) => { + if (confirmed) { allowNavigationRef.current = true; } - resolverRef.current?.(discardChanges); + resolverRef.current?.(confirmed); resolverRef.current = null; setOpen(false); }, []); @@ -94,6 +94,25 @@ export const UnsavedChangesProvider = ({ isDirty, children }: UnsavedChangesProv }; }, []); + useEffect(() => { + if (!isDirty) return; + + const handleBeforeUnload = (event: BeforeUnloadEvent) => { + if (allowNavigationRef.current) return; + + event.preventDefault(); + + // Necesario para compatibilidad con navegadores legacy. + event.returnValue = ""; + }; + + window.addEventListener("beforeunload", handleBeforeUnload); + + return () => { + window.removeEventListener("beforeunload", handleBeforeUnload); + }; + }, [isDirty]); + const contextValue = useMemo( () => ({ requestConfirm,