Skip to content
English

How it works

The timer keeps the time left whenever it pauses (pointer over the bar, focus inside it, page hidden or paused set) and the drain animation pauses at the same moment, so the bar always shows the real time left. The live region wrapper is always rendered, so each new message is announced.

Use it for

  • Reversible actions that should not ask first: archive, move, remove from a list.
  • Confirming a quick action without blocking the screen.

Look elsewhere when

  • Actions that cannot be undone: ask first with a confirmation.
  • Errors that need attention: use a callout or alert.

Usage

import { useState } from 'react'
import { UndoSnackbar } from './UndoSnackbar.jsx'

export function Inbox() {
  const [pending, setPending] = useState(null)
  return (
    <>
      <MessageList onArchive={(m) => setPending(m)} />
      <UndoSnackbar
        open={!!pending}
        message="Conversation archived"
        undoneMessage="Moved back to Inbox"
        onAction={() => restore(pending)}
        onTimeout={() => { commit(pending); setPending(null) }}
        onClose={() => setPending(null)}
      />
    </>
  )
}

Props

PropTypeDefaultWhat it does
openboolean—Shown or not; a new message while open restarts the countdown.
messageReactNode—What just happened.
onAction() => void—Undo was chosen (button or ⌘Z / Ctrl+Z).
onTimeout() => void—The countdown ended: make the action final and close.
onClose() => void—Dismissed, or finished confirming an undo.
durationnumber6000Countdown in ms.
undoneMessageReactNode—If set, shown with a check for 1.3s after Undo.
actionLabel / closeLabelstring'Undo' / 'Dismiss'Button text and the close button's accessible name.
iconReactNode—Small icon before the message.
shortcutbooleantrueListen for ⌘Z / Ctrl+Z while open and show the hint.
pausedbooleanfalseHold the countdown from outside.
containedbooleanfalseSit at the bottom of the nearest positioned ancestor instead of the viewport.

Accessibility

  • Wrapped in a polite role status region that stays mounted, so messages are announced.
  • The countdown pauses while the pointer is over the snackbar or focus is inside it, giving keyboard and screen reader users time to act (WCAG 2.2.1).
  • Undo is a real button with aria-keyshortcuts; ⌘Z or Ctrl+Z also undo while it is open, except in text fields.
  • Escape dismisses it when focus is inside; the close button has an accessible name; targets are 36px.
  • With reduced motion it fades in and out without sliding; the countdown line still drains as information.

Motion

Enter 420ms cubic-bezier(0.22, 1.25, 0.4, 1) rising 24px from 0.96 scale; leave 200ms; countdown linear over the duration; undo swaps text and icon with a 260ms slide and spring.

Add it to your project

Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.

UndoSnackbar.jsxundo-snackbar.csstokens.cssREADME.md

Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.

Feedback
Confetti burst comes with Pro. Its preview shows once you unlock it.