diff --git a/frontend/src/features/public-form/components/FormFields/FormFields.tsx b/frontend/src/features/public-form/components/FormFields/FormFields.tsx index 54010aefc9..62bc0a07d8 100644 --- a/frontend/src/features/public-form/components/FormFields/FormFields.tsx +++ b/frontend/src/features/public-form/components/FormFields/FormFields.tsx @@ -7,6 +7,7 @@ import { isEmpty, times } from 'lodash' import { BasicField, FormFieldDto } from '~shared/types/field' import { FormColorTheme, LogicDto } from '~shared/types/form' +import { useNativeBlocker } from '~hooks/useNativeBlocker' import InlineMessage from '~components/InlineMessage' import { FormFieldValues } from '~templates/Field' import { createTableRow } from '~templates/Field/Table/utils/createRow' @@ -108,7 +109,8 @@ export const FormFields = ({ if (isDirty) setBlockNavigation(true) }, [isDirty, setBlockNavigation]) - useNavigationPrompt(blockNavigation) + // useNavigationPrompt(blockNavigation) + useNativeBlocker(blockNavigation) return ( diff --git a/frontend/src/hooks/useNativeBlocker.ts b/frontend/src/hooks/useNativeBlocker.ts new file mode 100644 index 0000000000..7cb319ffdd --- /dev/null +++ b/frontend/src/hooks/useNativeBlocker.ts @@ -0,0 +1,17 @@ +import { useCallback, useEffect } from 'react' + +export const useNativeBlocker = (when: boolean) => { + const listener = useCallback( + (e: BeforeUnloadEvent) => { + if (when) e.returnValue = '' + }, + [when], + ) + + useEffect(() => { + window?.addEventListener('beforeunload', listener) + return () => { + window?.removeEventListener('beforeunload', listener) + } + }, [listener]) +}