Skip to main content

NavigationBlocker

Storybook

Go to Story

Defined in: src/components/NavigationBlocker/NavigationBlocker.tsx:23

Extends

  • Omit<ComponentProps<typeof Dialog>, "open">

Properties

PropertyTypeDescriptionInherited fromDefined in
children?ReactNode-Omit.childrennode_modules/@radix-ui/react-dialog/dist/index.d.mts:10
defaultOpen?boolean-Omit.defaultOpennode_modules/@radix-ui/react-dialog/dist/index.d.mts:12
onOpenChange?(open) => void-Omit.onOpenChangenode_modules/@radix-ui/react-dialog/dist/index.d.mts:13
modal?boolean-Omit.modalnode_modules/@radix-ui/react-dialog/dist/index.d.mts:14
enableBeforeUnload?booleanControls whether the native browser beforeunload prompt should be enabled. When true and shouldBlock is true, a native prompt will appear on page reload/close. Default false-src/components/NavigationBlocker/NavigationBlocker.tsx:31
shouldBlock?booleanIndicates whether there are unsaved changes that should block navigation.-src/components/NavigationBlocker/NavigationBlocker.tsx:33
status?BlockerStatusWhen set to "blocked", the confirmation dialog is shown.-src/components/NavigationBlocker/NavigationBlocker.tsx:35
proceed?() => voidWhen status is blocked, this function allows navigation to continue.-src/components/NavigationBlocker/NavigationBlocker.tsx:37
reset?() => voidWhen status is blocked, this function cancels navigation (status will be reset to 'idle')-src/components/NavigationBlocker/NavigationBlocker.tsx:39
title?ReactNode--src/components/NavigationBlocker/NavigationBlocker.tsx:40
description?ReactNode--src/components/NavigationBlocker/NavigationBlocker.tsx:41
stayLabel?ReactNode--src/components/NavigationBlocker/NavigationBlocker.tsx:42
leaveLabel?ReactNode--src/components/NavigationBlocker/NavigationBlocker.tsx:43

BlockerStatus

type BlockerStatus = "idle" | "blocked";

Defined in: src/components/NavigationBlocker/NavigationBlocker.tsx:21


function NavigationBlocker(__namedParameters): Element;

Defined in: src/components/NavigationBlocker/NavigationBlocker.tsx:92

A small confirmation dialog to guard against accidental navigation when there are unsaved changes. This component is router-agnostic. It does not import or depend on any specific router library.

Parameters

ParameterType
__namedParametersNavigationBlockerProps

Returns

Element

Examples

import { useBlocker } from "@tanstack/react-router";
const blocker = useBlocker({
shouldBlockFn: (location, action) => {
// block if there are unsaved changes and the user is trying to navigate away
return formState.isDirty && action !== "replace";
},
withResolver: true,
enableBeforeUnload: true,
});
return (
<NavigationBlocker
status={blocker.status}
proceed={blocker.proceed}
reset={blocker.reset}
/>
);
// The `enableBeforeUnload` prop enables a native browser prompt on page reload/close.
const [dirty, setDirty] = useState(false);
return (
<>
<NavigationBlocker shouldBlock={dirty} enableBeforeUnload />
<form
onChange={() => setDirty(true)}
onSubmit={(e) => {
e.preventDefault();
// save...
setDirty(false);
}}
>
<input name="displayName" />
<button type="submit">Save</button>
</form>
</>
);