shadcn.io is an independent project built on shadcn/ui
On this page9 sections
A toast is a short notice at the edge of the screen after something happens, such as "Event created" with an Undo button, and it leaves on its own a few seconds later. Focus stays where it was, so people keep working while a screen reader reads it out.
On Radix UI it's the Sonner file under another name. For a message that has to stay until someone deals with it, put an Alert in the page instead.
Installation
Anatomy

Usage
import { Button } from "@/components/ui/button";
import { Toaster, toast } from "@/components/ui/toast";const ToastDemo = () => {
const handleShowToast = () => {
toast("Event has been created", {
action: {
label: "Undo",
// Restore the event here. The toast closes itself once this returns.
onClick: () => undefined,
},
description: "Sunday, December 03, 2023 at 9:00 AM",
});
};
return (
<>
<Toaster />
<Button onClick={handleShowToast} variant="outline">
Show Toast
</Button>
</>
);
};Accessibility
This is the Sonner file, so Toaster renders a section named "Notifications alt+T" that is a polite live region, and a screen reader reads each new toast while focus stays put. Each toast is a focusable list item, and its timer pauses while the pointer is over the stack, while the tab is hidden and while the stack is expanded with Alt+T.
A toast leaves on its own, so never make its button the only way to do something.
- 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
Five buttons, Default, Success, Info, Warning and Error, calling toast, toast.success, toast.info, toast.warning and toast.error with one line about an event. The typed ones put the matching icon before the text, and each toast closes after 4 seconds. Use a typed toast when the outcome matters, and keep the words specific, since the icon alone doesn't say what happened.
Promise
Create Event calls toast.promise with a request that resolves after two seconds. One toast reads "Loading..." beside a spinner, then becomes "Event has been created" in place, and the error message would show only if the promise rejected, which this one never does. Use it for a save or an upload someone is waiting on, so the same toast carries the result.
FAQ
How do I show a toast in React?
Render Toaster once near the root of the app and call toast("Event has been created") from any handler, with options such as description as the second argument. There's no provider or hook to set up.
On Radix UI this component is the Sonner file under another name, so everything on the Sonner page applies here, from toast.promise to an action button.
How do I show a success or error toast with Radix UI?
Call toast.success, toast.error, toast.warning or toast.info with the message, as the Types demo does, and each one puts the matching Lucide icon before the text. A second argument takes options such as description.
The colors stay neutral by default. Add richColors to Toaster to tint each type green, blue, amber or red with Sonner's own palette.
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 result when the promise settles, as the Promise demo does.
success can be a function that receives the resolved data, and calling unwrap on the returned value gives you the promise's result.
How do I close a toast from code?
Keep the id that toast() returns and pass it to toast.dismiss, or call toast.dismiss() with no id to close them all. Calling toast again with the same id option updates that toast in place.
An action button closes its toast on its own after running your handler, which is why the Undo in the demo at the top of the page makes no dismiss call.
How do I change where toasts appear?
Set position on Toaster: top-left, top-center, top-right, bottom-left, bottom-center or the default, bottom-right.
A single toast can take its own position in its options, which opens a second stack at that spot. visibleToasts caps the pile at 3 by default.
How long does a toast stay on screen, and how do I change it?
Four seconds, unless you set duration on Toaster for every toast or in a toast's options for one, and Infinity keeps it until dismissed. A loading toast stays until you update it or its promise settles.
The timers pause while the pointer is over the stack, while the tab is hidden and after Alt+T.
Give a toast with an Undo button longer than the default, since reading it and reaching the button both take time.
Can keyboard and screen reader users reach a toast?
Yes. A new toast is read out from a polite live region without moving focus, and Alt+T moves focus to the stack and pauses it, so there's time to reach Undo. Escape only collapses the stack again and doesn't close a toast.
Keep the title short, since it's read first, and keep anything a toast offers available somewhere else on the page too. The Accessibility section lists each key.
Should I use Toast or Sonner?
On Radix UI they're the same file: Toast is Sonner under another name, with the same Toaster and the same toast. Pick one import and keep to it, so the app doesn't mount two toasters by accident.
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.











