Skip to content
boing

Guide

Undo toasts: act first, offer a way back

An undo toast does the action straight away and offers a way back, instead of asking “Are you sure?” first. In Boing, add an action labelled Undo whose onClick reverses the change and returns a patch, and the same toast turns into the confirmation.

Try it

Why undo beats a confirm dialog

A confirm dialog interrupts every time, and people learn to click through it without reading. An undo toast costs nothing when the action was intended, and still rescues the person who clicked the wrong row. It only works when the action can really be reversed, so the data has to stay recoverable for a while: a soft delete, an archive flag or a copy kept in memory.

How do I add an undo button to a toast?

TypeScript
import { toast } from 'react-boing';

/** Archives a conversation and offers to bring it back. */
function archiveConversation(conversation: Conversation) {
  archive(conversation.id);

  toast.success('Conversation archived', {
    action: {
      label: 'Undo',
      onClick: () => {
        unarchive(conversation.id);
        return { type: 'info', title: 'Conversation restored' };
      },
    },
  });
}

When onClick returns a patch, the toast morphs into it and its buttons go away. Here it becomes an info toast, which closes after the usual 5 seconds. If onClick returns nothing, the toast closes. Add a secondary button for a second choice; it sits before the main one.

When undo needs a network request

onClick returns its patch right away, so for a slow undo return a loading state and update the toast when the request finishes:

TypeScript
const id = toast.success('File deleted', {
  action: {
    label: 'Undo',
    onClick: () => {
      restoreFile(fileId).then(
        () => toast.update(id, { type: 'success', title: 'File restored' }),
        () => toast.update(id, { type: 'error', title: 'Could not restore the file' }),
      );
      return { type: 'loading', title: 'Restoring…' };
    },
  },
});

How long should an undo toast stay?

By default a toast with a button stays until it is used or dismissed. That is the safe choice: a toast that vanishes after a few seconds takes the undo with it, which is hard on people who read slowly, use a screen reader or reach the button by keyboard. WCAG 2.2.1 (Timing Adjustable) asks that time limits like this can be turned off, adjusted or extended.

If you do set a duration, a slim bar along the bottom counts down the remaining time. Hovering the toast or focusing it with the keyboard pauses the timer, and Alt+T moves focus straight to the Undo button.

TypeScript
toast.success('Label removed', {
  duration: 8000,
  action: {
    label: 'Undo',
    onClick: () => {
      restoreLabel();
    },
  },
});

In this version onClick returns nothing, so the toast simply closes after the undo.

When a confirm dialog is still right

  • The action cannot be reversed, such as sending an email, charging a card or deleting data for good.
  • Other people see the effect immediately, like a message posted to a channel or a notification sent to a team.
  • The action is large, such as removing a whole project, where a mistake would be costly to notice late.

For everything else (archiving, moving, hiding, removing a tag) act first and offer undo.

Frequently asked questions

When does an undo toast go away?

Not by default. A toast with a button stays until it is used or dismissed. Pass a duration to give it a timer, and a countdown line appears.

Can keyboard users reach the Undo button?

Yes. Pressing Alt+T moves focus to the first button in the toast, and focusing it pauses the timer.

What happens after the user clicks Undo?

If onClick returns a patch, the toast morphs into it and the buttons disappear. If it returns nothing, the toast closes.

Does pressing Escape trigger undo?

No. Escape dismisses the toast without running its action, so the original change stays in place.