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.
Here it's built from UI SDK's toast parts, with a toaster you call from anywhere. For a message that has to stay until someone deals with it, put an Alert in the page instead.
Installation
Anatomy

Usage
<script lang="ts">
import { Button } from "@/components/ui/button"
import { createToaster, Toaster } from "@/components/ui/toast"
const toast = createToaster({
overlap: true,
placement: "bottom-end",
})
const handleShowToast = () => {
toast.create({
action: {
label: "Undo",
// Restore the event here. The toast closes itself once this returns.
onClick: () => undefined,
},
description: "Sunday, December 3 at 9:00 AM",
title: "Event created",
});
}
</script>
<Toaster toaster={toast} /><Button onclick={handleShowToast} variant="outline">Show Toast</Button>Accessibility
Toaster renders a region named "Notifications" 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 named by its title and described by its description, so focus stays put. Pointing at the region, focus inside it, a hidden tab and leaving the window pause the timers.
A toast leaves on its own, so never make its button the only way to do something.
- F6
- Moves focus to the newest toast and pauses every toast. Pressed again, it sends focus back to where it was.
- 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 F6.
- Shift+Tab
- Moves focus to the previous toast or button, and from the newest toast back to where it was before F6.
- Enter
- Activates the focused action or close button.
- Space
- Activates the focused action or close button.
- EscEscape
- When focus is on a toast or inside it, closes that toast and moves focus to the next one, or back to where it was after the last.
Demos
Types
Five buttons, Default, Success, Info, Warning and Error, each calling toast.create with a one-line description, and all but Default with the matching type, which picks the icon. Each closes after 5 seconds, the duration the demo's toaster sets for every type. 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 "Creating event…" beside a spinner, then becomes "Event 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 Svelte?
Render Toaster once near the root of the app and call toast.create from any handler with a title and a description. The toast exported from components/ui/toast is a shared toaster that keeps up to 3 on screen at the bottom end, and Toaster shows it when you pass none.
For other settings, make your own toaster with createToaster and pass it to Toaster as toaster, as the demos on this page do. Keep it in a module of its own, so every file that calls it shares one instance.
A second Toaster for the same toaster doesn't show its toasts twice: the first one mounted shows them, and when it unmounts the next takes over.
How do I show a success or error toast with UI SDK?
Pass type to toast.create, one of success, error, warning, info or loading, or call the shortcut of the same name, such as toast.success, with the same options. The type picks the icon in ToastItem, with the error one in the destructive color, as the Types demo shows.
A loading toast stays until you update it, and the other types leave after 5 seconds. An error toast is also announced at once, where the other types wait their turn in the polite live region.
How do I show a loading toast that updates when a request finishes?
Call toast.promise with the promise and a loading, success and error option, each an object of toast options such as title and description, of which loading is required. One toast shows it and stays up, then turns into the success or error toast when the promise settles, as the Promise demo does.
success and error can be functions, of the resolved data and of the reason, and either one left out closes the toast when the promise settles that way. A fetch Response that isn't ok still resolves, so throw on it first. The call returns an object whose unwrap gives you the promise's result.
How do I close a toast from code?
Keep the id that toast.create returns and pass it to toast.dismiss, which plays the exit animation. With no id, every toast closes.
An action button dismisses its own toast once its onClick has run, which is why the Undo in the demo at the top of the page makes no dismiss call. toast.update changes a toast in place, as does toast.create with the id of one still on screen, and toast.remove here is the same as toast.dismiss. Set closable to false to hide the close button on a toast your code will dismiss.
How do I change where toasts appear?
Set placement when you create the toaster with createToaster: top-start, top, top-end, bottom-start, bottom or bottom-end. The shared toast in your file uses bottom-end.
The toast's own classes stack and animate it from the bottom, with bottom-0, origin-bottom and their translateY values, so a top placement means flipping those in components/ui/toast too. max caps how many show at once, 3 unless you set it, and the rest wait in a queue. overlap stacks the toasts, spread out while the pointer or focus is on them.
How long does a toast stay on screen, and how do I change it?
Five seconds, on the shared toast and on a toaster from createToaster, while a loading toast stays until you update or dismiss it. Pass duration in milliseconds to one toast, or to createToaster for all of them, and Infinity keeps a toast up until it's dismissed.
The timers pause while the pointer is over the toasts, while focus is inside them, while the tab is hidden and while the window is in the background.
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 F6 moves focus to the toasts and pauses them, so there's time to reach Undo. Escape closes the focused toast, and tabbing past the last one returns focus to where it was.
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?
Toast is the one to use on Svelte. Sonner exists only for React, while Toast is built on UI SDK's toast queue, with createToaster, typed toasts, placements and promise toasts. A toast queued with UI SDK's own toast() shows in the same Toaster as the shared toast, since that toaster queues into it.
API Reference
Toast
<li> data-slot="toast"
action?ToastAction<Content>
A button that does something, like Undo.
- Type
ToastAction<Content>
cancel?ToastCancel<Content>
A button that dismisses it, like Cancel.
- Type
ToastCancel<Content>
data?unknown
Anything else, for your own rendering, like an avatar.
description?Content
More detail, under the title.
dismissible?boolean
Whether the user can dismiss it: swiping, Escape and Close parts. Timers and dismiss() still close it.
- Default
true
duration?number
How long it shows, in ms, while nothing pauses it. Infinity keeps it until dismissed.
- Default
the toaster's duration, 5000, and `Infinity` for `"loading"`
id?string
Reuses a toast: a toast with this id that's still showing is updated instead of added again.
onDismiss?(details: ToastDismissDetails) → void
Runs when it closes, with why. Calling details.cancel() keeps it.
- Type
(details: ToastDismissDetails) → void- Parameters
details– ToastDismissDetails- Returns
void
priority?"assertive" | "polite"
How screen readers announce it: "polite" waits for a pause, "assertive" interrupts.
- Type
"assertive" | "polite"- Default
"assertive" for errors, "polite" otherwise
title?Content
The main message.
toast?object
Accepted for the other bases' markup: the region gives each toast its data.
- Type
{ actionProps?: React.ComponentPropsWithoutRef<"button">; data?: Data; description?: React.ReactNode; id: string; onClose?: () → void; onRemove?: () → void; priority?: "low" | "high"; timeout?: number; title?: React.ReactNode; type?: string }
type?ToastType
What it's about, for styling through data-type, and for how it's announced.
- Default
"default"
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
data-ending-style?string
Present while a toast animates out. Style the state you animate to.
data-expanded?string
Present while the stack is expanded: hovered, focused, or always with expanded.
data-front?string
Present on the newest toast, in front of a collapsed stack. A closing toast keeps it while it animates out.
data-starting-style?string
Present for a toast's first frame. Style the state you animate from.
data-type"default" | "success" | "error" | "info" | "warning" | "loading"
What the toast is about.
- Type
"default" | "success" | "error" | "info" | "warning" | "loading"
--gapstring--heightstring
The measured height, for animating open and closed.
--offset-ystring--peekstring--scalestring--shrinkstring--toast-heightstring
The toast's own height with its content laid out freely, whatever height it's given. Set it as the height to animate the stack.
--toast-indexstring
The toast's place in the stack, 0 for the newest. A closing toast keeps its place.
--toast-offsetstring
The height of the toasts in front of this one, in pixels. Add gaps with --toast-index to place an expanded stack.
--toast-swipe-xstring
How far a swipe moved the toast sideways, in pixels. Translate the toast by it.
--toast-swipe-ystring
How far a swipe moved the toast up or down, in pixels.
ToastProvider
limit?number
How many toasts show at once; the rest wait their turn.
- Default
3
timeout?number
How long toasts show, in ms, unless one says otherwise. 0 keeps them until closed.
- Default
5000
toastManager?object
The manager it shows: the shared toast when unset.
- Type
{ add: <T extends Data = Data>(options: ToastManagerAddOptions<T>) → string; close: (id?: string) → void; promise: <Value, T extends Data = Data>( promise: Promise<Value>, options: ToastManagerPromiseOptions<Value, T> ) → Promise<Value>; queue: ToastQueue<React.ReactNode>; update: <T extends Data = Data>( id: string, options: | ToastManagerUpdateOptions<T> | ((previous: ToastObject<T>) → ToastManagerUpdateOptions<T>) ) → void }- Default
toast
ToastPortal
ToastViewport
<section> data-slot="toast-region"
align?"center" | "end" | "start"
Where along that edge. "start" and "end" follow the writing direction, and swiping toward them dismisses too.
- Type
"center" | "end" | "start"- Default
"end"
children?(toast: ToastObject) → Snippet
Renders each toast; ToastItem when unset.
- Parameters
toast– { actionProps?: React.ComponentPropsWithoutRef<"button">; data?: Data; description?: React.ReactNode; id: string; onClose?: () → void; onRemove?: () → void; priority?: "low" | "high"; timeout?: number; title?: React.ReactNode; type?: string }- Returns
React.ReactNode
debug?boolean
Logs every state change with its reason to the console, in development builds.
expanded?boolean
Whether the stack is always expanded. Otherwise it expands while the pointer or focus is in it.
- Default
false
maxVisible?number
How many toasts show at once. Older ones wait, with their timers stopped, until there's room. UI SDK's default: 3.
- Default
context?.limit
pendingAnnouncement?string
What screen readers hear once a toast's action or close button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
- Default
"Loading"
shortcut?string | null
The key that moves focus into the region and back, like "F6" or "alt+t". null turns it off.
- Type
string | null- Default
"F6"
side?"bottom" | "top"
The edge of the viewport it sits on. Swiping toward it dismisses a toast.
- Type
"bottom" | "top"- Default
"bottom"
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
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"
--keyboard-insetstring
How many pixels of the page the on-screen keyboard covers, 0 without one.
--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.
ToastContent
<div> data-slot="toast-content"
class?string
Merged with the part's classes.
ToastTitle
<div>
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
ToastDescription
<div>
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
ToastAction
<button> data-slot="toast-action"
dismiss?boolean
Whether pressing it dismisses the toast.
- Default
true
onAction?(event: Event, details: ToastActionDetails) → unknown
Runs when it's pressed, instead of the toast's own action.onAction. A returned Promise marks it pending. details.source says how it was pressed, for changes made after an await.
- Type
(event: Event, details: ToastActionDetails) → unknown- Parameters
event– Eventdetails– ToastActionDetails- Returns
unknown
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
data-pending?string
Present on an action or close button while its handler's Promise runs.
ToastClose
<button>
action?(event: Event, details: ToastActionDetails) → unknown
Runs before the toast closes. A returned Promise keeps it open and the button pending until it settles. details.source says how it was pressed, for changes made after an await.
- Type
(event: Event, details: ToastActionDetails) → unknown- Parameters
event– Eventdetails– ToastActionDetails- Returns
unknown
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
ToastActionTrigger
<button> data-slot="toast-action"
dismiss?boolean
Whether pressing it dismisses the toast.
- Default
true
onAction?(event: Event, details: ToastActionDetails) → unknown
Runs when it's pressed, instead of the toast's own action.onAction. A returned Promise marks it pending. details.source says how it was pressed, for changes made after an await.
- Type
(event: Event, details: ToastActionDetails) → unknown- Parameters
event– Eventdetails– ToastActionDetails- Returns
unknown
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
data-pending?string
Present on an action or close button while its handler's Promise runs.
ToastCloseTrigger
<button>
action?(event: Event, details: ToastActionDetails) → unknown
Runs before the toast closes. A returned Promise keeps it open and the button pending until it settles. details.source says how it was pressed, for changes made after an await.
- Type
(event: Event, details: ToastActionDetails) → unknown- Parameters
event– Eventdetails– ToastActionDetails- Returns
unknown
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
ToastIcon
<span> data-slot="toast-icon"
type?string
The toast's type, which picks the icon: success, error, warning, info or loading.
class?string
Merged with the part's classes.
ToastItem
toastobject
The toast it renders, from the toast manager.
- Type
{ actionProps?: React.ComponentPropsWithoutRef<"button">; data?: Data; description?: React.ReactNode; id: string; onClose?: () → void; onRemove?: () → void; priority?: "low" | "high"; timeout?: number; title?: React.ReactNode; type?: string }
class?string
Merged with the part's classes.
Toaster
limit?number
How many toasts show at once; the rest wait their turn.
- Default
3
timeout?number
How long toasts show, in ms, unless one says otherwise. 0 keeps them until closed.
- Default
5000
toaster?object
The toast manager it shows, the same as toastManager: the shared toast
when unset.
- Type
{ add: <T extends Data = Data>(options: ToastManagerAddOptions<T>) → string; close: (id?: string) → void; promise: <Value, T extends Data = Data>( promise: Promise<Value>, options: ToastManagerPromiseOptions<Value, T> ) → Promise<Value>; queue: ToastQueue<React.ReactNode>; update: <T extends Data = Data>( id: string, options: | ToastManagerUpdateOptions<T> | ((previous: ToastObject<T>) → ToastManagerUpdateOptions<T>) ) → void }
toastManager?object
The manager it shows: the shared toast when unset.
- Type
{ add: <T extends Data = Data>(options: ToastManagerAddOptions<T>) → string; close: (id?: string) → void; promise: <Value, T extends Data = Data>( promise: Promise<Value>, options: ToastManagerPromiseOptions<Value, T> ) → Promise<Value>; queue: ToastQueue<React.ReactNode>; update: <T extends Data = Data>( id: string, options: | ToastManagerUpdateOptions<T> | ((previous: ToastObject<T>) → ToastManagerUpdateOptions<T>) ) → void }- Default
toaster ?? toast











