Skip to content

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

Components

Free

Toast for UI SDK

Toast notifications for React on UI SDK: success, error and promise toasts that leave on their own, read by screen readers without moving focus.

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 toast manager you call from anywhere. For a message that has to stay until someone deals with it, put an Alert in the page instead.

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

Toast anatomy: root, title, description, action trigger, close trigger

Usage

import { Button } from "@/components/ui/button";
import { Toaster, toast } from "@/components/ui/toast";
const ToastDemo = () => {
  const handleShowToast = () => {
    const id = toast.add({
      actionProps: {
        children: "Undo",
        onClick() {
          toast.close(id);
        },
      },
      description: "Sunday, December 3 at 9:00 AM",
      title: "Event created",
    });
  };

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

Accessibility

Toaster renders a viewport, 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 or a toast with priority set to high. Each toast is a focusable list item named by its title and described by its description, so focus stays put. Pointing at the viewport, 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.
Escape
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 adding a toast with a one-line description, and all but Default with the matching type, which picks the icon in ToastIcon. The error toast also sets priority to high, so it's announced at once instead of waiting its turn. Use a typed toast when the outcome matters, and keep the words specific, since the icon alone doesn't say what happened.

Preview width

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.

Preview width

FAQ

How do I show a toast in React?

Render Toaster once near the root of the app and call toast.add from any handler with a title and a description. The toast exported from components/ui/toast is a manager over UI SDK's shared toast queue, so a toast queued with UI SDK's own toast() shows in the same Toaster. Pass another manager, made with createToastManager, as toastManager.

Inside a component rendered under Toaster, useToastManager returns the same add, close, update and promise, along with the toasts on screen.

A second Toaster for the same manager doesn't show its toasts twice: the first one mounted shows them, and when it unmounts the next takes over, leaving the toasts on screen where they are.

How do I show a success or error toast with UI SDK?

Pass type to toast.add, one of success, error, warning, info or loading. ToastIcon in your file picks the icon from it, with the error one in the destructive color, as the Types demo shows.

An error toast is announced at once, where the other types wait their turn in the polite live region. Set priority to high to do the same for any other toast people must notice, or to low to keep an error polite.

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, where a string becomes the toast's title and an object can set a description and anything else toast.update takes. One toast shows the loading state without a timer, then turns into the result when the promise settles and starts a timer of its own, as the Promise demo does.

success can be a function that receives the resolved data. The call returns a promise of the same value, so you can still await the result.

How do I close a toast from code?

Keep the id that toast.add returns and pass it to toast.close, as the Undo button in the demo at the top of the page does. With no id, every toast closes.

The action button here doesn't close its toast by itself, which is why that Undo calls toast.close. toast.update changes a toast in place with new options, such as a new title or type, and toast.add with the id of a toast still on screen does the same.

How do I change where toasts appear?

Edit the classes on ToastViewport in components/ui/toast. It sits at the bottom, as a centered strip on narrow screens and in the end corner from the sm breakpoint up.

The toast's own classes place and animate it from the bottom, with bottom-0, origin-bottom and their translateY values, so moving the viewport to the top means flipping those too. limit on Toaster sets how many show at once, 3 by default.

How long does a toast stay on screen, and how do I change it?

Five seconds, unless you set timeout on Toaster for every toast or in toast.add for one. A timeout of 0 keeps a toast until it's closed, and a loading toast never times out.

The timers pause while the pointer is over the viewport, while focus is in it, 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?

Both are built on UI SDK's toast. Use Toast for every part in your own file, a toast manager with toast.add, F6 to reach the toasts and swipe to dismiss. Sonner keeps the sonner package's API, one toast() call with nothing to configure, and Alt+T to reach the toasts.

API Reference

Toast

<li> data-slot="toast"

PropType
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?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
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"
CSS variableType
--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

PropType
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"

PropType
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) → React.ReactNode

Renders each toast; ToastItem when unset.

Type
(toast: ToastObject) → React.ReactNode
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?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
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"
CSS variableType
--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"

PropType
className?string

Merged with the part's classes.

ToastTitle

<div>

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

ToastDescription

<div>

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

ToastAction

<button> data-slot="toast-action"

PropType
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 – Event

details – ToastActionDetails

Returns
unknown
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-pending?string

Present on an action or close button while its handler's Promise runs.

ToastClose

<button>

PropType
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 – Event

details – ToastActionDetails

Returns
unknown
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

ToastActionTrigger

<button> data-slot="toast-action"

PropType
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 – Event

details – ToastActionDetails

Returns
unknown
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-pending?string

Present on an action or close button while its handler's Promise runs.

ToastCloseTrigger

<button>

PropType
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 – Event

details – ToastActionDetails

Returns
unknown
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

ToastIcon

<span> data-slot="toast-icon"

PropType
type?string

The toast's type, which picks the icon: success, error, warning, info or loading.

className?string

Merged with the part's classes.

ToastItem

PropType
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 }
className?string

Merged with the part's classes.

Toaster

PropType
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

useToast