shadcn.io is an independent project built on shadcn/ui
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 wraps the sonner package with your theme's colors and Lucide icons.
For a toast built on Ark UI's own parts, use Toast. When the message must stay until someone acts on it, use an Alert Dialog.
Installation
Anatomy

Usage
Render Toaster once, near the root of the app, then call toast from any handler.
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Toaster } from "@/components/ui/sonner";<>
<Toaster />
<Button
variant="outline"
onClick={() => toast("Event has been created")}
>
Show Toast
</Button>
</>Accessibility
Toaster renders a section named "Notifications alt+T" that is a polite live region, so a screen reader reads each new toast without focus leaving what someone is doing. Each toast is a focusable list item, and its timer, four seconds by default, pauses while the pointer is over the stack, while the tab is hidden, and while the stack is expanded with Alt+T. Under reduced motion, Sonner'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
- Expands the stack of toasts, moves focus into it and pauses their timers.
- Tab
- Moves focus to the next toast or button in the stack. Tabbing in without Alt + T doesn't pause the timers, and once focus leaves the stack it goes back to the element that had it before.
- Shift+Tab
- Moves focus to the previous toast or button in the stack.
- Enter
- Activates the focused action, cancel or close button.
- Space
- Activates the focused action, cancel or close button.
- EscEscape
- While focus is in the stack, collapses it again. The toasts stay until their timers run out.
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.
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.
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.
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.
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?
Two Toasters are mounted, for example one in the root layout and another in a page or a component, and each one shows every toast. Sonner keeps one store for the whole app, so a toast appears in every Toaster that's on screen.
Keep a single Toaster near the root. If you need a second region on purpose, give that Toaster an id and pass the same value as toasterId in the options of the toasts meant for it, and the others will skip them.
Should I use Sonner or the Toast component?
Use Sonner when one toast() call from anywhere is all you need. Toast is Ark UI's toast: you call toast.create on a toaster made by createToaster, and every part of a toast sits in your own file to change. It's also the one that exists on Vue, Svelte and Solid.
API Reference
Toaster
data-slot="toaster"
closeButton?boolean
Whether each toast shows a close button.
containerAriaLabel?string
The toasts region's label for screen readers, before its hotkey: Notifications when unset.
customAriaLabel?string
The toasts region's whole label for screen readers, in place of containerAriaLabel and the hotkey.
dir?"rtl" | "ltr" | "auto"
The reading direction. Inherits the nearest DirectionProvider when unset.
- Type
"rtl" | "ltr" | "auto"
duration?number
How long each toast stays, in milliseconds.
expand?boolean
Whether the toasts show expanded instead of stacked.
gap?number
The space between expanded toasts, in px.
hotkey?string[]
The keys that move focus to the toasts: altKey and KeyT when unset.
icons?object
The icon for each kind of toast: success, info, warning, error, loading and close.
- Type
{ success?: React.ReactNode; info?: React.ReactNode; warning?: React.ReactNode; error?: React.ReactNode; loading?: React.ReactNode; close?: React.ReactNode }
id?string
The element's id.
invert?boolean
Whether the toasts take the other theme's colors.
mobileOffset?{ top?: string | number
The distance from the screen's edges on small screens: px, or one per side.
- Type
{ top?: string | number
offset?{ top?: string | number
The distance from the screen's edges: px, or one per side.
- Type
{ top?: string | number
position?"top-left" | "top-right" | "bottom-left" | "bottom-right" | "top-center" | "bottom-center"
Where the toasts show: top-left, top-center, top-right, bottom-left, bottom-center or bottom-right.
- Type
"top-left" | "top-right" | "bottom-left" | "bottom-right" | "top-center" | "bottom-center"
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.
swipeDirections?SwipeDirection[]
The directions a toast can be swiped away in.
theme?"light" | "dark" | "system"
The color theme: light, dark or system.
- Type
"light" | "dark" | "system"- Default
pageTheme
toastOptions?object
Defaults for every toast: its class names, styles, duration and more.
- Type
{ className?: string; closeButton?: boolean; descriptionClassName?: string; style?: React.CSSProperties; cancelButtonStyle?: React.CSSProperties; actionButtonStyle?: React.CSSProperties; duration?: number; unstyled?: boolean; classNames?: ToastClassnames; closeButtonAriaLabel?: string; toasterId?: string }
visibleToasts?number
How many toasts show at once.
className?string
Merged with the part's classes.












