本文へ移動
日本語

仕組み

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.

向いている場面

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

向かない場面

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

使い方

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

プロパティ

プロパティ型既定値説明
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.

アクセシビリティ

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

モーション

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.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

UndoSnackbar.jsxundo-snackbar.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フィードバック
「紙吹雪」は Pro に含まれます。解除するとプレビューが表示されます。