Toast
Non-blocking status cards, stacked viewport notifications, and countdown undo toasts.
import { Toast, Toaster, createToaster } from '@uthereal/ui/toast'Toast playground
Overview
@uthereal/ui/toast unifies three notification patterns on one visual contract:
| Mode | API | Use for |
|---|---|---|
| Declarative | <Toast /> | One in-place status card controlled by React state or an agent composition. |
| Stacked viewport | <Toaster /> + createToaster() | Application-wide notifications pushed from event handlers that tuck behind the newest card. |
| Undo | <Toast undo /> or toaster.undo() | Reversible removals with a draining countdown ring, batch merging (mergeKey), and Cmd/Ctrl+Z. |
Use a toast for transient results that require no blocking decision. Use a dialog for blocking decisions or irreversible destructive confirmations.
Tones
| Tone | Visual cue | Default lifetime | When to use |
|---|---|---|---|
neutral | Info glyph (--ui-muted) | 4500 ms (<Toast>) / 5000 ms (stack) | General status updates and undo batches. |
info | Info glyph (--ui-accent) | 4500 ms / 5000 ms | Background updates and team activity. |
success | Drawn check (<Toast>) or circled check (--ui-success) | 4500 ms / 5000 ms | Completed actions ("Release 4.2 published"). |
warning | Alert triangle (--ui-warning) | 1.6x base in stack (8000 ms) | Limits or degraded states that keep working. |
danger | Circled X (--ui-danger) | 1.6x base in stack (8000 ms) | Failed actions (toaster.error()). |
loading | 800 ms spinner (--ui-muted) | Indefinite until updated | In-progress work replaced via toaster.update() or toaster.promise(). |
Usage
Declarative <Toast />
Render <Toast /> directly in React or in an agent composition. Control visibility with open and onOpenChange, or leave it uncontrolled with defaultOpen. Set duration={0} to keep the card visible until dismissed.
'use client'
import { useState } from 'react'
import { Toast } from '@uthereal/ui/toast'
export function ReleaseConfirmation() {
const [open, setOpen] = useState(true)
return (
<Toast
open={open}
onOpenChange={setOpen}
tone="success"
title="Release 4.2 published"
description="Live for 1,284 customers."
actionLabel="View"
action="view_release"
onAction={(action) => console.log(action)}
/>
)
}Stacked <Toaster /> + createToaster()
Create one toaster outside render, mount <Toaster toaster={toaster} /> once, and push toasts from event handlers. Pass contained on <Toaster /> to pin the stack inside the nearest positioned container instead of the viewport.
'use client'
import { Toaster, createToaster } from '@uthereal/ui/toast'
export const toaster = createToaster({
position: 'bottom-end',
visibleToasts: 3,
limit: 12,
})
export function App() {
return (
<>
<InvoiceForm
onSent={() =>
toaster.success({
title: 'Invoice sent',
description: 'Northwind will receive it by email.',
})
}
/>
<Toaster
toaster={toaster}
onAction={(action, toastId) => console.log(action, toastId)}
/>
</>
)
}Undo mode (toaster.undo and <Toast undo />)
Undo mode replaces the status icon with a draining countdown ring (6000 ms by default) and shows an Undo button with a ⌘Z / Ctrl Z cap on fine pointers. Host callbacks own all data effects:
onUndoruns when the user clicks Undo, clicks Retry, or pressesCmd/Ctrl+Z. Returning a promise transitions the toast throughcounting→restoring(disabled button + spinner) →restored(drawn check for2000ms) orerror(Retry button).onCommitruns when the countdown drains orEscapecommits the active undo window.- Repeat
toaster.undo(...)calls sharing amergeKeymerge into the active counting toast:countrolls up digit-by-digit, the countdown ring refills, and the card plays a subtle scale nudge.
'use client'
import { toaster } from './toaster'
export function archiveThread(id: string) {
hideThread(id)
toaster.undo(
{
mergeKey: 'archive-threads',
title: 'conversations archived',
undoLabel: 'Undo',
},
{
onUndo: () => restoreArchivedThreads(),
onCommit: () => commitArchivedThreads(),
},
)
}Examples
Tones and actions
Single declarative <Toast /> cards across info, warning, danger, and loading states with optional descriptions and action buttons (success is shown in the playground above).
Undo countdown and batch counts
Declarative <Toast undo /> cards with the live draining countdown ring, rolled batch count, and Cmd/Ctrl+Z shortcut cap.
Live stacked viewport
Trigger tones, promise updates, live undo batching (toaster.undo with mergeKey), morphing actions, placements, and overlap behavior in a live <Toaster />.
Create a toast.
Imperative queue methods
Every helper validates its input against toastSchema and returns the toast ID.
| Method | Behavior |
|---|---|
create(input) | Shows a toast (tone: 'info' by default). Reusing an active id updates that toast in place. |
success, info, warning, error | Shows a toast in that tone (error maps to danger). |
loading(input) | Shows a spinner toast with infinite lifetime until updated or dismissed. |
undo(input, { onUndo, onCommit? }) | Shows an undo countdown toast. Matching mergeKey while counting merges into the active toast, increments count, and refills the ring. |
update(id, patch) | Morphs icon, title, description, height, and action width in place and restarts the timer. Omitted fields keep their values; pass description: undefined or action: undefined to clear one. No-op if dismissed. |
promise(promise, { loading, success, error }) | Shows a loading toast, then morphs to success or danger when settled. Returns { id, unwrap }. |
dismiss(id?) | Dismisses one toast, or every toast when called without an ID. |
Stacking, gestures, and timing
- Stack geometry: Older toasts tuck behind the front card with
14px peeks and0.05scale steps on--ui-highlight. Hovering, keyboard-focusing, or tapping (on touch) fans the stack out withgap(12px) spacing. - Pause behavior: Timers and undo countdown rings pause on hover (
pauseOnHover), keyboard focus, pointer drag, and hidden browser tabs (visibilitychange), then resume from the exact remaining time. - RTL-aware swipe: Swiping toward the inline-end edge (right in LTR, left in RTL) follows
1:1; pulling toward inline-start rubber-bands. Releasing past80px or480px/s (<Toast>) or projected past40%width (<Toaster>) throws the card off with its release velocity. - Responsive layout: Viewport-fixed stacks sit
offset(24px) from the edges and center16px from the edge below480px. Contained stacks sit16px inside their ancestor. Below21rem (336px) card width, actions wrap under the copy; below360px, the shortcut cap hides.
API reference
Declarative <Toast />
| Argument | Type / default | Description |
|---|---|---|
titleRequired | string— | Required message: what happened, in sentence case. |
description | string— | Supporting detail under the title. |
tone | neutral | info | success | warning | danger | loadingDefault: "neutral" | Status: neutral or info for results, success with a drawn check, warning and danger longer-lived, loading a spinner until updated. |
defaultOpen | booleanDefault: true | Initial open state. Later argument edits do not reset an existing instance; use open and onOpenChange to control it. |
duration | integerDefault: 4500 | Time on screen, ms; 0 stays until dismissed. 4500 by default, or the 6000 undo window in undo mode. |
actionLabel | string— | Action button text: a verb, e.g. "View". |
action | string— | Action name sent to onAction; the toast closes after it. |
undo | booleanDefault: false | Undo mode: a draining countdown ring, an Undo button and the Cmd/Ctrl+Z shortcut replace the plain lifetime; undo emits onUndo and the drained window emits onCommit. |
count | integer— | Batch count rolled into the message, e.g. "3 items archived". |
undoShortcut | booleanDefault: true | Cmd+Z on Apple devices and Ctrl+Z elsewhere trigger the undo, unless focus is in a text field. |
undoLabel | stringDefault: "Undo" | Undo button text in undo mode. |
dismissible | booleanDefault: true | Show a dismiss button. |
dismissLabel | stringDefault: "Dismiss notification" | Accessible name of the dismiss button; {title} fills the toast title. |
label | stringDefault: "Notification" | Accessible name of the status. |
recipe | studio | softDefault: "studio" | Visual treatment. Both recipes use the same interaction behavior. |
theme | ToastTheme— | Color mode, accent, and tokens, including width. See Theming for all keys and defaults. |
motion | none | subtle | expressiveDefault: "subtle" | Rise and swap animation preset. System reduced-motion preferences take precedence. |
motionOptions | ToastMotionOptions— | Overrides for the stack spring and the press scale. |
dir | ltr | rtl— | Reading direction. Omit it to inherit the page direction. |
<Toaster />
| Argument | Type / default | Description |
|---|---|---|
label | stringDefault: "Notifications" | Accessible name of the notification region; " (Alt+T)" is appended while the hotkey is on. |
dismissLabel | stringDefault: "Dismiss notification" | Name of each dismiss button; {title} is replaced by the toast title, e.g. "Dismiss Invoice sent". |
statusLabels | ToastStatusLabels— | Status read before each toast by screen readers: info, success, warning, danger ("Error") and loading ("In progress"). Neutral toasts have none. |
contained | booleanDefault: false | Pin the stack inside the nearest positioned ancestor instead of the viewport. |
recipe | studio | softDefault: "studio" | Visual treatment. Both recipes use the same interaction behavior. |
theme | ToastTheme— | Color mode, accent, and tokens, including the stack width. See Theming. |
motion | none | subtle | expressiveDefault: "subtle" | Stack animation preset. System reduced-motion preferences take precedence. |
motionOptions | ToastMotionOptions— | transition for entering and following the stack (Arc smooth spring), expand for fanning out and tucking back (Arc morph spring), and pressScale for the buttons. |
dir | ltr | rtl— | Reading direction; start and end placements follow it. Omit it to use the document direction. |
createToaster options
| Argument | Type / default | Description |
|---|---|---|
position | top-start | top-center | top-end | bottom-start | bottom-center | bottom-endDefault: "bottom-end" | Viewport edge and alignment of the stack (Arc naming). start and end follow the reading direction. |
visibleToasts | integerDefault: 3 | Toasts shown at once, 1 to 10 (Arc: 3). Later ones wait behind the stack and return as the front ones close. |
placement | top-start | top | top-center | top-end | bottom-start | bottom | bottom-center | bottom-end— | Legacy spelling of position; top and bottom read as centered. |
maxVisible | integer— | Legacy spelling of visibleToasts. |
limit | integerDefault: 12 | Toasts kept in the queue, 1 to 50. The oldest beyond it are dropped. |
duration | integerDefault: 5000 | Base time on screen in milliseconds, 1000 to 60000. Warnings and errors stay 1.6 times longer. Hovering, focusing or tapping the stack, dragging a toast, or hiding the page pauses it. |
undoDuration | integerDefault: 6000 | Undo window in milliseconds, 1000 to 30000 (Arc: 6000). It refills whenever another removal joins the batch. |
overlap | booleanDefault: true | Tuck older toasts behind the newest; hovering or focusing the stack fans it out. false lists them all. |
pauseOnHover | booleanDefault: true | Hold the countdowns while the pointer rests on the stack. |
gap | integerDefault: 12 | Space between toasts in the expanded stack, px. |
offset | integerDefault: 24 | Distance from the viewport edges, px. Below 480px wide the stack centers 16px from its edge. |
hotkey | booleanDefault: true | Alt+T moves focus into the stack and fans it out. |
React integration
| Prop | Component | Purpose |
|---|---|---|
ref | Toast, Toaster | Forwarded to the root HTMLDivElement (Toast) or <section> (Toaster). |
open / onOpenChange(open) | Toast | Controlled visibility and dismissal callback. |
onAction(action) / onAction(action, toastId) | Toast, Toaster | Host action callback. In <Toaster>, calling toaster.update(toastId, ...) morphs the toast instead of dismissing it. |
onUndo / onCommit | Toast | Host undo and commit callbacks for <Toast undo />; return a promise to show restoring / restored / error. |
toaster | Toaster | Required queue instance from createToaster(). |
portalContainer | Toaster | Portal target when contained is false; defaults to the enclosing layer, provider, or document.body. |
renderIcon(name, { size, slot }) | Toast, Toaster | Custom icon renderer for slot: 'icon' or slot: 'dismiss'. Keep icons decorative. |
preset / tokens | Toast, Toaster | Application defaults and --ui-* CSS custom property overrides. |
className / style | Toast, Toaster | Appended to the root element or stack region. |
Keyboard & accessibility
| Key / gesture | Interaction |
|---|---|
Alt + T | Focus the <Toaster /> stack and fan it out (hotkey: true). |
Cmd + Z / Ctrl + Z | Trigger active undo toast (undoShortcut: true). Ignored while focus is inside input, textarea, select, or contenteditable. |
Tab / Shift+Tab | Move through toasts, action/undo buttons, and dismiss buttons. |
Enter / Space | Activate the focused action, undo, or dismiss button. |
Escape | Commit a focused counting undo toast, or dismiss any other focused toast. |
| Swipe inline-end | Throw and dismiss the toast (right in LTR, left in RTL). |
<Toast />renders withrole="status",aria-live="polite", andaria-atomic="true".<Toaster />mounts a dedicatedrole="status"live region before any toast appears, announcing status labels, merged batch counts, and undo lifecycle transitions (Restoring,Restored,Error).- Toasts waiting behind
visibleToastsareinert. Closing a focused toast hands focus to the next toast or restores focus to the element active before entering the stack. - Under
forced-colors: active, cards preserveCanvasTextborders, buttons preserveButtonTextborders, and the countdown ring usesCanvasTexttrack withHighlightfill.
Motion and theming
<Toast /> rises 16 px from scale 0.96 on the morph spring (0.42s, bounce 0.16), draws its success check over 240 ms, and springs height changes with the smooth spring (0.4s, bounce 0). <Toaster /> rises new toasts from their own height beyond the edge on the smooth spring, fans out on the morph spring, and folds removed action buttons horizontally. Copy updates crossfade with a 0.3em vertical shift and subtle blur.
Under prefers-reduced-motion: reduce, travel, scale, blur, and the loading spinner are disabled in favor of 160 ms opacity fades while the countdown ring still drains smoothly. Setting motion="none" makes every transition immediate (0 ms).
| Theme token | CSS variable | Default | Role |
|---|---|---|---|
width | --ui-toast-width | 360px | Preferred card and stack width, clamped to the viewport. |
surfaceRaised | --ui-surface-raised | theme | Front and expanded card background. |
highlight | --ui-highlight | theme | Tucked card background and control hover fill. |
popupRadius | --ui-popup-radius | theme | Card corner radius (26px in studio). |
shadow / shadowRaised | --ui-shadow / --ui-shadow-raised | theme | Floating front shadow and resting tucked shadow. |
border / borderStrong | --ui-border / --ui-border-strong | theme | Card edge and hovered action border. |
success / warning / danger / accent | --ui-success, --ui-warning, --ui-danger, --ui-accent | theme | Status icons and countdown ring stroke. |
shortcutSize | --ui-shortcut-size | 12px | Inline ⌘Z / Ctrl Z shortcut hint size. |
theme also accepts mode, accent, and core typography and color tokens. An application-wide UtherealProvider supplies shared defaults.