Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Sonner for UI SDK

Sonner toasts for a React app on UI SDK: mount one Toaster, then call toast() from any handler for saves, errors, promises and an Undo button.

On this page9 sections

Sonner shows a short notice in a corner of the screen, such as "Draft saved" or "Upload failed", and clears it after four seconds. You mount one Toaster near the root and call toast() from any handler, with no provider or state of your own. This file rebuilds the sonner API on UI SDK's toast, with your theme's colors and Lucide icons.

For a toast built from parts in your own file, use Toast. When the message must stay until someone acts on it, use an Alert Dialog.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Sonner anatomy: toaster, toast, title, description, action

Usage

import { Button } from "@/components/ui/button";
import { Toaster, toast } from "@/components/ui/sonner";
const SonnerDemo = () => {
  const handleShowToast = () => {
    toast("Event has been created", {
      action: {
        label: "Undo",
        onClick: () => undefined,
      },
      description: "Sunday, December 03, 2023 at 9:00 AM",
    });
  };

  return (
    <>
      <Toaster />
      <Button onClick={handleShowToast} variant="outline">
        Show Toast
      </Button>
    </>
  );
};

Accessibility

Toaster renders a section named "Notifications" that is a region while it holds toasts. It isn't a live region itself: UI SDK reads each new toast out through hidden live regions, politely, and at once for an error. Each toast is a focusable list item, and its timer, four seconds by default, pauses while the pointer or focus is in the stack, while the tab is hidden and while the window is in the background. Under reduced motion, this file's styles drop the slide and fade, and the loading toast's ring holds still.

A toast leaves on its own, so never make its button the only way to do something, and give a toast with an action a longer duration.

Alt+T
Moves focus to the newest toast, which expands the stack and pauses the timers. Pressed again, it sends focus back to where it was. It does nothing while focus is in a text field.
Tab
Moves focus through the toast's buttons and on to the toasts after it. Past the last one, focus goes back to where it was before Alt + T.
Shift+Tab
Moves focus to the previous toast or button, and from the newest toast back to where it was before Alt + T.
Enter
Activates the focused action, cancel or close button.
Space
Activates the focused action, cancel or close button.
Escape
When focus is on a toast or inside it, closes that toast, unless it sets dismissible to false. Focus moves to the next toast, or back to where it was after the last.

Demos

Types

Six buttons: Default, Success, Info, Warning, Error and Promise. The first five call toast, toast.success, toast.info, toast.warning and toast.error, and each typed one adds this file's icon for that outcome. Promise calls toast.promise, which shows Loading... for two seconds, then turns the same toast into "Event has been created". Pick the helper that matches what happened, so the icon agrees with the words.

Preview width

Description

One toast, "Event has been created", with description set to the date under it, Monday, January 3rd at 6:00pm. The title states the outcome, and the description carries the detail someone might want on a second look. Keep the title to a few words, since the description sits under it in quieter text and wraps on its own.

Preview width

Position

Six buttons, one per spot: top left, top center, top right, bottom left, bottom center and bottom right. Each passes position in that toast's own options, and Sonner opens a separate stack there while other toasts stay where Toaster puts them. Set position on Toaster when every toast should share one place, and per toast only for the odd one out.

Preview width

Action

Archive conversation shows "Conversation archived" with an Undo button, and pressing Undo closes that toast and confirms with "Conversation restored". The toast sets duration to 10 seconds, enough to read it and reach Undo with Alt+T and Tab. Give a toast that offers to reverse something a stay this long, and keep another way to undo it on the page.

Preview width

FAQ

How do I set up Sonner toasts in React?

Render Toaster once, near the root of the app, such as in the body of a Next.js root layout, then call toast("Saved") from any handler or module. There's no hook and no provider: toast writes to a store that every mounted Toaster reads. The file starts with "use client", so a server layout can render it.

The toaster follows the page's theme, a dark or light class on html or an inline color-scheme there, and the system's otherwise. Pass theme to fix it to one.

How do I show a success or error toast with Sonner?

Call toast.success, toast.error, toast.warning, toast.info or toast.loading with the message. Each one puts this file's Lucide icon for that outcome before the text, as the Types demo shows, and a second argument takes options such as description.

The toast's colors stay neutral. Add richColors to Toaster to tint each type green, blue, amber or red, with Sonner's own palette for light and dark rather than your theme's tokens.

How do I show a loading toast that updates when a request finishes?

Pass the promise, or a function that returns one, to toast.promise with loading, success and error messages. One toast shows the loading text with a spinner and doesn't time out, then turns into the success or error message when the promise settles, and only then starts its timer.

success can be a function that receives the resolved data, as in the Types demo, where it builds "Event has been created" from the result. Call unwrap on the returned value to get the promise's result in your own code.

How do I add an Undo button to a Sonner toast?

Pass action with a label and an onClick in the toast's options, as the Action demo does with Undo. Clicking it runs your handler and closes the toast, unless the handler calls preventDefault on the event. A cancel with the same shape adds a second, quieter button that runs its handler and then closes the toast.

Four seconds is short for deciding to undo something, which is why that demo sets duration to 10 seconds. Keep another way to reverse the change on the page too.

How do I close a Sonner toast from code?

Keep the id that toast() returns and pass it to toast.dismiss. Calling toast.dismiss() with no id closes every toast at once.

You can choose the id yourself with the id option. Calling toast again with the same id updates that toast in place instead of stacking a new one, which suits a "Saving…" notice that should become "Saved". Set duration to Infinity for a toast that stays until your code dismisses it.

How do I change where Sonner toasts appear?

Set position on Toaster: top-left, top-center, top-right, bottom-left, bottom-center or the default, bottom-right. offset sets the gap from the edges, 24 pixels by default, and on screens up to 600 pixels wide the stack spans the width.

A single toast can take its own position in its options, which opens a second stack at that spot, as the Position demo does. expand shows every toast at full height instead of a collapsed pile, and visibleToasts caps the pile at 3 by default.

Why does my Sonner toast show up twice?

It shouldn't with this file: a toast shows once however many Toasters are mounted, such as one in the root layout and another in a page. The oldest one on screen shows the toasts with its own settings, such as position and duration, and when it unmounts the next one takes over without moving the toasts already up.

Keep a single Toaster near the root anyway, so its settings are plainly the ones in use. This version has no option that sends a toast to one Toaster only, so for a second region with toasts of its own, use Toast, which can give each region a toast manager of its own.

Should I use Sonner or the Toast component?

Use Sonner when one toast() call from anywhere is all you need, with the sonner package's API. Toast is built on the same UI SDK toast, but you add toasts through a manager with toast.add, each toast is a set of parts in your own file, and F6 moves focus to the stack. Pick one, so the app has one place for its notices.

API Reference

Toaster

<section> data-slot="toast-region"

PropType
closeButton?boolean

Shows a close button on every toast.

Default
false
container?React.ComponentProps<typeof ToastRegion>["container"]

Where the toasts render: the docs preview, else the body, by default; null renders them in place.

Type
React.ComponentProps<typeof ToastRegion>["container"]
containerAriaLabel?string

The region's accessible name.

Default
"Notifications"
customAriaLabel?string

Replaces the region's accessible name, like containerAriaLabel.

dir?"rtl" | "ltr" | "auto"

The writing direction: the page's when unset.

Type
"rtl" | "ltr" | "auto"
duration?number

How long toasts show, in ms, unless one says otherwise.

Default
4000
expand?boolean

Keeps the stack expanded, not only while hovered or focused.

Default
false
gap?number

The space between expanded toasts, in px.

Default
14
hotkey?string[]

The keys that move focus to the toasts and back. The type's default: ["altKey", "KeyT"].

Default
["altKey"
icons?object

Replaces this file's icons, one type at a time.

Type
{ close?: React.ReactNode; error?: React.ReactNode; info?: React.ReactNode; loading?: React.ReactNode; success?: React.ReactNode; warning?: React.ReactNode }
invert?boolean

Whether the toasts take the other theme's colors.

mobileOffset?Offset

Mobile screens' distance from the edges.

offset?Offset

The distance from the edges of the screen.

position?Position

Where toasts show, unless one says otherwise.

Default
"bottom-right"
richColors?boolean

Whether success, info, warning and error toasts get their own colors.

style?React.CSSProperties

Inline styles for the list that holds the toasts.

theme?Theme

light or dark: the page's theme when unset.

toastOptions?{ actionButtonStyle?: React.CSSProperties; cancelButtonStyle?: React.CSSProperties; className?: string; classNames?: ToastClassnames; closeButton?: boolean; closeButtonAriaLabel?: string; descriptionClassName?: string; duration?: number; style?: React.CSSProperties; unstyled?: boolean; }

Defaults for every toast.

Type
{ actionButtonStyle?: React.CSSProperties; cancelButtonStyle?: React.CSSProperties; className?: string; classNames?: ToastClassnames; closeButton?: boolean; closeButtonAriaLabel?: string; descriptionClassName?: string; duration?: number; style?: React.CSSProperties; unstyled?: boolean; }
Default
{}
visibleToasts?number

How many toasts show at once; the rest wait their turn.

Default
3
className?string

Merged with the part's classes.

AttributeType
data-align"start" | "center" | "end"

Where along that edge.

Type
"start" | "center" | "end"
data-expanded?string

Present while the stack is expanded: hovered, focused, or always with expanded.

data-keyboard?string

Present on the region while the on-screen keyboard covers part of the page.

data-side"top" | "bottom"

The edge of the viewport the region sits on.

Type
"top" | "bottom"
data-theme?string

Mirrors resolvedTheme.

CSS variableType
--keyboard-insetstring

How many pixels of the page the on-screen keyboard covers, 0 without one.

--mobile-offset-bottomstring
--mobile-offset-leftstring
--mobile-offset-rightstring
--mobile-offset-topstring
--offset-bottomstring
--offset-leftstring
--offset-rightstring
--offset-topstring
--toast-countstring

How many toasts show.

--toast-front-heightstring

The newest toast's height. Clip the toasts behind it to it in a collapsed stack.

--toast-stack-heightstring

The toasts' heights added up, for the height of an expanded stack.

--visual-viewport-heightstring

The height of the part of the page you can see, above the keyboard.

--visual-viewport-topstring

How far the part of the page you can see is scrolled from the top of the layout, as when the keyboard pushes it up.

--visual-viewport-widthstring

The width of the part of the page you can see.

--widthstring

The measured width, for animating open and closed.