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

3 files · Synced 2 min ago

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

toast-usage.tsx
"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.

  • Stripe

    Invoice 0412 is ready

    9:41
  • Linear

    3 issues assigned to you

    8:15
  • Vercel

    Your trial ends Friday

    Mon
  • Figma

    Noor left 4 comments

    Sun

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

Idle

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
durationnumberDefault4000Radix. Default time on screen in ms. Toasts with long text stay longer.
timeoutnumberDefault5000Base UI. Default time on screen in ms. Toasts with long text stay longer.

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
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.
keystringBase UI. Same merging as id on Radix.
durationnumberRadix. Time on screen in ms. When unset it grows with the length of the title and description.
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.
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.

toast.undo

toast.undo(message, options) on Radix, toast.undo(options) with a title on Base UI

Shows 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
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.
onUndo() => voidRuns when Undo is clicked. onCommit then never runs.
undoLabelReactNodeDefault"Undo"Text of the Undo button.
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.

toast.progress

toast.progress(message, options) on Radix, toast.progress(options) with a title on Base UI

Shows 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
valueRequirednumberProgress from 0 to 100. Values outside are clamped.

onToastPersist

onToastPersist(listener: (toast: PersistedToast) => void): () => void

Subscribes to toasts shown with persist. Each one arrives as { id, title, body?, createdAt } when it closes, which fits NotificationInbox items. Returns an unsubscribe function.

Source

components/ui/toast.tsx