Toast
Toasts that keep shadcn's API and add undo with a working shortcut, progress, merging of repeated toasts, reading-time durations and a handoff to the notification inbox.
Marketing / Q3
Shared with 4 people
q3-roadmap.png
1.2 MB · 2h ago
brand-guidelines.pdf
4.8 MB · Yesterday
invoice-0412.pdf
310 KB · Mon
Keyboard
- AltT
- Focus the toasts (Radix)
- F6
- Focus the toasts (Base UI)
- CtrlZ
- Undo the newest undo toast that sets it
- Tab
- Move to Undo and other actions
- Escape
- Collapse the stack (Radix) or close the toast (Base UI)
On this page
Installation
The CLI also installs the npm packages and registry items it builds on, and reads the style in your components.json to pick the Base UI or Radix build.
Usage
"use client"
import { Button } from "@/components/ui/button"
import { toast } from "@/components/ui/toast"
export function DeleteFiles({ ids, setHidden }: { ids: string[]; setHidden: (hidden: boolean) => void }) {
function remove() {
setHidden(true)
toast.undo({
title: `Deleted ${ids.length} files`,
shortcut: "Mod+Z",
onCommit: () =>
fetch("/api/files/delete", { method: "POST", body: JSON.stringify(ids), keepalive: true }),
onUndo: () => setHidden(false),
})
}
return <Button variant="destructive" onClick={remove}>Delete</Button>
}Examples
Undo instead of confirm
Archive or delete a conversation and it leaves the list at once. The real action waits in onCommit, which runs when the toast times out, is swiped away or closed, or the page unloads. Send that request with fetch keepalive: true or navigator.sendBeacon so it still arrives when the tab is closing. Undo, or ⌘Z on a Mac and Ctrl+Z elsewhere, restores it, and onCommit never runs. Hover or focus the toasts to pause the countdown.
- 9:41
Stripe
Invoice 0412 is ready
- 8:15
Linear
3 issues assigned to you
- Mon
Vercel
Your trial ends Friday
- Sun
Figma
Noor left 4 comments
Nothing committed yet
Types and actions
Each type gets its own icon color from the theme. Errors are announced assertively, and an action gives the reader something to do next.
Follow a promise
One toast moves from loading to success or error as the promise settles. The production deploy fails on purpose.
acme-web
main · a1f9c2e
Upload progress
toast.progress shows a determinate bar and stays up until you replace it. Call it again with the same id to move the bar, then swap in a success toast with that id.
- q3-roadmap.png1.2 MB
- brand-guidelines.pdf4.8 MB
- invoice-0412.pdf310 KB
Merge repeated toasts
Toasts that share an id (a key on Base UI) merge into one with a count, and the timer restarts on every repeat.
Invite your team
Click either button a few times. Each repeat bumps the count and restarts the timer instead of stacking another toast.
Time to read
Without a duration, a toast stays up longer the more text it has and never less than the Toaster default, so a long error does not vanish mid-sentence.
Saved
Stays 5.0s
Could not publish the post
Stays 9.3s
Keep it in the inbox
Toasts with persist land in the notification inbox when they close. onToastPersist hands you an InboxNotification to add to the list.
API reference
Toaster
Render once near the root of the app. On Radix it takes every prop of shadcn's Sonner Toaster, and on Base UI every prop of shadcn's toast Toaster. The default duration it sets is the shortest a toast stays up.
| Prop | Type | Default |
|---|---|---|
durationnumberDefault4000Radix. Default time on screen in ms. Toasts with long text stay longer. | number | 4000 |
timeoutnumberDefault5000Base UI. Default time on screen in ms. Toasts with long text stay longer. | number | 5000 |
Other props go to the Sonner Toaster (Radix) or Toast.Provider (Base UI).
toast
toast(message, options?) and toast.success, info, warning, error, loading, promise, dismiss (Radix); toast.add(options), close, update, promise (Base UI)The same calls as shadcn. Import it from the toast file, not from sonner, to get merging, reading-time durations and toast.undo. Error toasts are announced assertively.
| Prop | Type | Default |
|---|---|---|
idstring | numberRadix. A toast of the same type with an id that is already showing merges into it: the newest content wins, a count badge appears and the timer restarts. Reusing the id with another type, such as loading then success, updates the toast without a count. | string | number | |
keystringBase UI. Same merging as id on Radix. | string | |
durationnumberRadix. Time on screen in ms. When unset it grows with the length of the title and description. | number | |
timeoutnumberBase UI. Time on screen in ms, 0 to stay until closed. When unset it grows with the length of the title and description, unless the Toaster timeout is 0, which keeps every toast up until closed. | number | |
persistbooleanHands the toast to onToastPersist listeners when it closes, ready for the notification inbox. Only text titles and descriptions are passed on, so a toast whose title is a React element is not persisted. | boolean |
toast.undo
toast.undo(message, options) on Radix, toast.undo(options) with a title on Base UIShows a toast with an Undo button and a countdown ring that pauses while the toasts are hovered or focused. The action is delayed until the toast goes away.
| Prop | Type | Default |
|---|---|---|
onCommitRequired() => voidRuns once when the toast times out, is swiped away or closed, or the page unloads. Because it can run during pagehide, send its request with fetch(url, { keepalive: true }) or navigator.sendBeacon, which the browser finishes after the tab closes. | () => void | |
onUndo() => voidRuns when Undo is clicked. onCommit then never runs. | () => void | |
undoLabelReactNodeDefault"Undo"Text of the Undo button. | ReactNode | "Undo" |
shortcutstringShortcut such as "Mod+Z" that triggers Undo on the newest undo toast while it is open. Shown as ⌘Z on a Mac and Ctrl+Z elsewhere, and ignored while focus is in a text field. | string |
toast.progress
toast.progress(message, options) on Radix, toast.progress(options) with a title on Base UIShows a determinate progress bar that stays up until replaced. Call it again with the same id to move the bar, then show a success or error toast with that id.
| Prop | Type | Default |
|---|---|---|
valueRequirednumberProgress from 0 to 100. Values outside are clamped. | number |
onToastPersist
onToastPersist(listener: (toast: PersistedToast) => void): () => voidSubscribes to toasts shown with persist. Each one arrives as { id, title, body?, createdAt } when it closes, which fits NotificationInbox items. Returns an unsubscribe function.