Back to all examples

Confirm before navigating away

ReactAngularWeb Components
const [open, setOpen] = useState(false);

  const handleChangeRoute = () => {
    setOpen(false);
    // In a real app, you would use your router's navigate function
    // setTimeout(() => navigate("/some-path"), 300);
    console.log("Navigating to new route...");
  };
<GoabButton onClick={() => setOpen(true)}>Open</GoabButton>
      <GoabModal
        heading="Are you sure you want to change route?"
        open={open}
        onClose={() => setOpen(false)}
        actions={
          <GoabButtonGroup alignment="end">
            <GoabButton type="secondary" size="compact" onClick={() => setOpen(false)}>
              Cancel
            </GoabButton>
            <GoabButton type="primary" size="compact" onClick={handleChangeRoute}>
              Change route
            </GoabButton>
          </GoabButtonGroup>
        }
      />

Prompt the user in a modal before navigating to a new route to preserve context.

When to use

Use this pattern when:

  • The user has unsaved changes that would be lost
  • Navigation would interrupt an important workflow
  • You need to confirm the user’s intent to leave the page
  • Context or data would be lost on navigation

Considerations

  • Allow users to cancel and stay on the current page
  • Use setTimeout for route changes after modal closes for smooth transitions
  • The secondary button should cancel the navigation
  • The primary button confirms the route change