Skip to content

Toast

Shows a short message that appears over the page, then disappears by itself, such as a confirmation after an action.

This page documents Sonner

On the React Aria base, shadcn has deprecated its own Toast and points to Sonner, an independent toast library by Emil Kowalski. Silkware follows: components/ui/sonner.tsx styles Sonner with the theme tokens and the root layout already mounts it, so you call toast() and nothing else. The page keeps the name Toast because that is the catalog entry.

Installation

pnpm dlx shadcn@latest add @silkware/sonner

Then mount the Toaster once, near the end of the root layout, after the page content:

import { Toaster } from "@/components/ui/sonner"
export default function RootLayout({ children }: { children: React.ReactNode }) {  return (    <html lang="en">      <body>        {children}        <Toaster />      </body>    </html>  )}

To copy the component by hand, install its dependencies:

pnpm add sonner next-themes lucide-react

Then copy the source into components/ui/sonner.tsx:

components/ui/sonner.tsx
"use client"
import { useTheme } from "next-themes"import { Toaster as Sonner, type ToasterProps } from "sonner"import { CircleCheckIcon, InfoIcon, TriangleAlertIcon, OctagonXIcon, Loader2Icon } from "lucide-react"
const Toaster = ({ ...props }: ToasterProps) => {  const { theme = "system" } = useTheme()
  return (    <Sonner      theme={theme as ToasterProps["theme"]}      className="toaster group"      icons={{        success: (          <CircleCheckIcon className="size-4" />        ),        info: (          <InfoIcon className="size-4" />        ),        warning: (          <TriangleAlertIcon className="size-4" />        ),        error: (          <OctagonXIcon className="size-4" />        ),        loading: (          <Loader2Icon className="size-4 animate-spin" />        ),      }}      style={        {          "--normal-bg": "var(--popover)",          "--normal-text": "var(--popover-foreground)",          "--normal-border": "var(--border)",          "--border-radius": "var(--radius)",        } as React.CSSProperties      }      toastOptions={{        classNames: {          toast: "cn-toast",        },      }}      {...props}    />  )}
export { Toaster }

Usage

import { toast } from "sonner"
toast("Order 4417 approved", {  description: "Sent to Lamivo Metalli at 09:14.",})

Import toast from sonner, not from @/components/ui/sonner. The component file exports only Toaster, the host that renders the toasts. You can call toast() from any client code, including event handlers and the callbacks of a mutation. The Toaster must be mounted once, anywhere in the tree; a second one shows every toast twice.

Examples

Types

Pick the type that matches the outcome. The default toast has no icon; success, info, warning and error add one from the set in the component, and the loading type shows a spinner. Every toast keeps the popover ground and text; the icon of a typed toast takes its status colour from the theme (--success, --info, --warning, --destructive), so the colour points at the outcome while the words carry it.

Success

An action the person took worked, and nothing else is needed. This is the most common toast.

Warning

The action worked, with a caveat the person should know about: a partial import, a value that was rounded. Say the caveat in the description.

Info

A neutral note about something the person started, such as when background work will finish. Use it rarely; a plain toast is usually enough.

Error

Background work failed and the person did not have to wait for it, such as a send that will be tried again. When the person must fix something, show it in the page as well.

Rich colours

Pass richColors to the Toaster to tint the whole toast with its status colour: bg-success/10 behind text-success in light, /20 in dark, the same pattern as the status badges. The tints read the same tokens, so they follow the theme and hold 4.5:1. The quiet default suits most products; turn tints on only when toasts must be told apart at a glance.

Description

A second line for the detail that makes the title useful, such as a count, a time or a file name. Keep the title complete without it.

Action

Add action for the one thing the reader may want to do next, most often Undo. action takes a label and an onClick; cancel takes the same and adds a quieter second button. Pressing either one closes the toast.

Promise

toast.promise follows an async task: it shows the loading message, then replaces it with success or error when the promise settles. success can be a function that reads the result.

Position

Each toast accepts a position. To change it for all toasts, pass position to Toaster instead. The default is the bottom end of the screen; keep one position across the product so people learn where to look.

Dismiss

A toast closes after four seconds by default, and pausing the pointer over the stack holds it open. Give a toast an id to refer to it later, set duration: Infinity to keep it until something dismisses it, and call toast.dismiss(id) to close it. Use this only for a task that has an end, such as a sync.

Accessibility

KeyAction
AltTMoves focus to the toast region and expands the stack, from anywhere on the page.
TabMoves focus through the toasts and the buttons inside them.
EnterPresses the focused action, cancel or close button.
SpacePresses the focused action, cancel or close button.
EscCollapses the expanded stack while focus is inside the toast region.

Sonner provides:

  • A landmark region labelled "Notifications" followed by the shortcut, which is a polite live region (aria-live="polite"). A new toast is announced when a screen reader is idle and does not interrupt the current sentence.
  • A focus stop on each toast, so keyboard users can reach its buttons.
  • Timers that pause while the pointer is over the stack and while the page is hidden, and swipe to dismiss on touch screens.
  • A close button, when you turn on closeButton, labelled "Close toast".

You provide:

  • A message that makes sense when read alone. Do not rely on the icon or the colour of the type.
  • A way to reach the same result without the toast. A toast leaves after a few seconds, so never put the only copy of an error, a number or a decision in one.
  • A longer duration, or Infinity, when a toast holds an action. Four seconds is not enough to find and press an Undo button for someone using a screen reader or a switch.
  • Short text. A screen reader reads the whole toast, and a long one blocks the next announcement.

Guidelines

When to use

  • To confirm that something worked and nothing else is needed: "Order 4417 approved", with toast.success.
  • To report a caveat after an action that still worked, with toast.warning.
  • To offer Undo for an action that was carried out at once and can be reversed.
  • To report the end of background work the person started, with toast.promise or a final success or error.

When not to use

  • For a message that must stay until it is dealt with, such as a supplier on hold. Use an Alert.
  • For a problem the person must fix before continuing, such as missing prices. Show it in the page with an Alert, and under the fields with the invalid state of a Field.
  • To ask for a decision. Use an Alert Dialog before the action, or a Dialog for a form.
  • To show that part of the page is loading. Use a Spinner, a Skeleton or a Progress bar in that place.
  • For a count that grows, such as every order that arrives. A stack of toasts hides what the person is doing.

Content

  • Write the title as a short past-tense statement of the result, in sentence case, with no full stop and no exclamation mark: "Order 4417 approved", "Supplier archived".
  • Name the object. "Saved" does not say what was saved.
  • Use the description for the detail, one line at most: a count, a time, a file name.
  • Label an action with a verb: "Undo", "View order". Do not write "OK" or "Close".
  • For an error, say what failed in the title, and put the fix in the page, not only in the toast.

Type and colour

  • toast.success when the action worked, toast.warning when it worked with a caveat, toast.info for a neutral note, toast.error when it failed.
  • A plain toast() when the result is ordinary and an action such as Undo matters more than the outcome.
  • Never pick a type for emphasis. The colour of the icon follows the outcome, and the title still says it in words.

Frequency

Show one toast for each action the person took. Do not show a toast for something the interface already shows: if the new row appears in the table, the table is the confirmation.

Do

Say what happened in a few words and add one line of detail.

Don’t

Don’t write a paragraph. It leaves before it can be read, and a screen reader reads all of it.

Do

Offer Undo for a reversible action that took effect at once.

Don’t

Don’t put a problem the person must fix in a toast. It disappears while they are still looking for rows 14 and 22.

API reference

Toaster

Renders the Sonner Toaster with the Silkware icons and the popover tokens for background, text, border and radius. It accepts every Sonner Toaster prop and the ones below are the ones you set most often. Mount it once.

PropTypeDefaultDescription
position?"top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right""bottom-right"Where the stack appears. A toast can override it.
duration?number4000How long a toast stays, in milliseconds, unless the toast sets its own.
closeButton?booleanfalseAdds a close button to every toast.
visibleToasts?number3How many toasts show at once before the stack collapses.
expand?booleanfalseKeeps the stack expanded instead of collapsing older toasts behind the newest.
richColors?booleanfalseTints each typed toast with its status colour from the theme tokens. Off by default: the icon alone takes the colour.
hotkey?string[]["altKey", "KeyT"]The keys that move focus to the toast region.
theme?"light" | "dark" | "system"NoneTaken from next-themes by the component. Override it only if the Toaster sits outside the theme provider.
toastOptions?ToastOptionsNoneDefaults for every toast, such as classNames, duration or closeButtonAriaLabel.

toast

The function you call to show a toast. It returns the toast id.

PropTypeDefaultDescription
toast(message, options)?(message: ReactNode, options?: ExternalToast) => string | numberNoneShows a toast with the default type.
toast.success, .info, .warning, .error?(message: ReactNode, options?: ExternalToast) => string | numberNoneShow a toast with the icon of that type, in the matching status colour.
toast.loading?(message: ReactNode, options?: ExternalToast) => string | numberNoneShows a toast with a spinner that stays until you update or dismiss it.
toast.promise?(promise, { loading, success, error }) => ...NoneFollows a promise and swaps the message when it settles. success and error accept a string or a function.
toast.dismiss?(id?: string | number) => voidNoneCloses one toast by id, or every toast when you pass no id.

The options object takes the props below.

PropTypeDefaultDescription
description?ReactNodeNoneA second line under the title.
action?{ label: ReactNode; onClick: (event) => void }NoneThe main button of the toast. Pressing it closes the toast.
cancel?{ label: ReactNode; onClick: (event) => void }NoneA quieter second button.
id?string | numberNoneA stable id. Showing a toast with an id that exists updates it instead of adding another.
duration?numberNoneMilliseconds before the toast closes. Use Infinity to keep it open.
position?PositionNoneWhere this toast appears.
dismissible?booleantrueWhether the toast can be dismissed by swiping or with the close button.
icon?ReactNodeNoneReplaces the icon of the type.
onDismiss?(toast) => voidNoneCalled when the reader dismisses the toast.
onAutoClose?(toast) => voidNoneCalled when the toast closes by itself after its duration.