仕組み
A self-contained toast system: ToastProvider renders the region and useToast() returns toast(). Each toast is a floating surface card (18px corners, hairline ring and soft shadow, no border) with its status icon in a tinted rounded square, an optional soft pill action and a quiet round close button. Each toast is absolutely positioned and its transform is computed in JS from measured heights. Collapsed layers shrink 5%, step back 10px and fade 18%. When a layer behind is taller than the front one, clip-path trims the overflow instead of changing height. Timers pause on hover, focus, drag and in background tabs.
向いている場面
- Light feedback for an action: order placed, saved, copied.
- Messages that must not interrupt the current task, such as a new message or a status change.
- Results with a quick action, such as “Deleted · Undo” or “Order placed · View”.
向かない場面
- Information the user must confirm or choose from: use a dialog.
- Validation errors tied to an input: show them next to the field.
- Long content people need to revisit: toasts vanish in seconds, use a message center.
- A page that already uses another notification system: do not mix two.
使い方
import { ToastProvider, useToast } from './ToastProvider.jsx'
export function App() {
return (
<ToastProvider position="bottom-right" duration={4000}>
<OrderButton />
</ToastProvider>
)
}
function OrderButton() {
const toast = useToast()
return (
<button
onClick={() =>
toast.success('Order placed', {
description: 'Trail running shoes · Size 42',
action: { label: 'View', onClick: () => navigate('/orders') },
})
}
>
Order
</button>
)
}
// Inside one panel: give the parent position: relative, then use <ToastProvider contained>プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
ToastProvider.position | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | 'bottom-right' | Where toasts appear. On the left edge they are swiped left to dismiss, otherwise right. |
ToastProvider.duration | number | 4000 | Default time on screen, in ms. |
ToastProvider.visibleToasts | number | 3 | Maximum layers in the stack; older ones are hidden, also when expanded. |
ToastProvider.contained | boolean | false | Positions toasts inside the nearest positioned ancestor (it needs position: relative). Otherwise they are fixed and portaled to body. |
ToastProvider.offset | number | 16 | Distance from the container edge, in px. |
ToastProvider.gap | number | 12 | Space between toasts when expanded, in px. |
ToastProvider.label | string | 'Notifications' | Accessible name of the notification region. |
ToastProvider.labels | object | — | Replaces built-in text (close). Shallow-merged over the English defaults. |
useToast() | () => ToastFn | — | Returns toast(message, options) with success, error, info and dismiss. Must be used inside ToastProvider. |
toast(message, options) | (message: ReactNode, options?: ToastOptions) => string | — | Shows a toast and returns its id. toast.success, error and info take the same arguments with a fixed type. |
toast.dismiss(id?) | (id?: string) => void | — | Closes one toast, or all of them when no id is given. |
options.description | ReactNode | — | Second line of text. |
options.type | 'default' | 'success' | 'error' | 'info' | 'default' | Sets the icon. Errors are announced assertively. |
options.duration | number | — | Time on screen for this toast, in ms. Infinity never dismisses automatically. |
options.action | { label: ReactNode; onClick: () => void } | — | Action button on the right; the toast closes after a click. |
options.id | string | — | Custom id. Calling again with the same id updates the toast in place and restarts its timer. |
アクセシビリティ
- The region is a labelled landmark, with two always-present live regions: polite for normal toasts, assertive for errors.
- Each toast is focusable; focus expands the stack and pauses timers. Esc or Delete closes the toast and moves focus to a neighbour.
- The close button has an accessible name and focus ring, is always visible and has a 44px hit area.
- Toasts hidden behind the front one or exiting are inert and skipped by Tab.
- With reduced motion, toasts fade without sliding or moving.
モーション
Enter and re-stacking 360ms ease-out (a new toast rises from the edge while growing from 94%); exit 200ms ease-in (40% toward the edge while fading); swipe snap-back 240ms ease-out. The action pill presses to 0.96.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
ToastProvider.jsxtoast-stack.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

