Ir al contenido
Español

Cómo funciona

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.

Úsalo para

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

Busca otra opción cuando

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

Uso

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)}
      />
    </>
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
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.

Accesibilidad

  • 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.

Movimiento

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.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

UndoSnackbar.jsxundo-snackbar.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Avisos
Lluvia de confeti viene con Pro. La vista previa aparece al desbloquearlo.