Skip to content

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

Components

Free

Toast for React

Toast notifications for React on Base UI: 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 Base UI'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 labeled "Notifications" that is a polite live region. Each toast is a non-modal dialog named by its title and described by its description, so a screen reader reads it while focus stays put. One with priority set to high is an alertdialog instead, announced at once through a hidden alert copy. Pointing at the viewport, keyboard focus inside it 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 toast viewport and pauses every toast.
Tab
From the viewport, moves focus to the newest toast, then through its buttons and 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 viewport 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.

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 made with createToastManager, and Toaster shows it unless you pass another as toastManager.

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

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

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.

For a failure people must notice, also set priority to high. Base UI then announces it at once through a hidden alert, where a normal toast waits its 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, where a string becomes the toast's description and an object can set a title and anything else toast.add takes. One toast shows the loading state without a timer, then turns into the result when the promise settles, 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.

An action button in Base UI 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.

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 keyboard focus is in it 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?

Use Toast for a toast built from Base UI's parts, with every part in your own file, F6 to reach the toasts and swipe to dismiss. Sonner is one toast() call with nothing to configure, styled and stacked by the sonner package.

API Reference

Toast

<div> data-slot="toast"

PropType
toastToastRootToastObject<any>

The toast to render.

Type
ToastRootToastObject<any>
swipeDirection?"up" | "down" | "left" | "right" | ("up" | "down" | "left" | "right")[]

Direction(s) in which the toast can be swiped to dismiss.

Type
"up" | "down" | "left" | "right" | ("up" | "down" | "left" | "right")[]
Default
['down', 'right']
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"

How the content aligns to its trigger.

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

Present when frontmost is true.

data-mounted?string

Present when mounted is true.

data-overlap?string

Present when stacked is false.

data-paused?string

Present when paused is true.

data-placementstring

The content's placement on its trigger, such as bottom-start.

data-sibling?string

Present when frontmost is false.

data-side"top" | "right" | "bottom" | "left"

The side of the trigger the content sits on.

Type
"top" | "right" | "bottom" | "left"
data-stack?string

Present when stacked is true.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"
data-typestring

Mirrors type.

CSS variableType
--gapstring
--heightstring

The measured height, for animating open and closed.

--offset-ystring
--peekstring
--scalestring
--shrinkstring
--toast-heightstring
--toast-indexstring
--toast-offset-ystring
--toast-swipe-movement-xstring
--toast-swipe-movement-ystring

ToastTitle

<h2> data-slot="toast-title"

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

<p> data-slot="toast-description"

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.

ToastPortal

<div> data-slot="toast-portal"

PropType
container?HTMLElement | ShadowRoot | React.RefObject<HTMLElement | ShadowRoot | null> | null

A parent element to render the portal element into.

Type
HTMLElement | ShadowRoot | React.RefObject<HTMLElement | ShadowRoot | null> | null
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

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

ToastViewport

<div> data-slot="toast-viewport"

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.

ToastContent

<div> data-slot="toast-content"

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

PropType
nativeButton?boolean

Whether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).

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

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
Default
<Button size="sm" variant="outline" />
className?string

Merged with the part's classes.

ToastClose

<button> data-slot="toast-close-trigger"

PropType
nativeButton?boolean

Whether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).

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

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
Default
<Button size="icon-sm" variant="ghost" />
className?string

Merged with the part's classes.

ToastActionTrigger

PropType
nativeButton?boolean

Whether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).

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

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
AttributeType
data-align"start" | "center" | "end"

How the content aligns to its trigger.

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

Present when frontmost is true.

data-mounted?string

Present when mounted is true.

data-overlap?string

Present when stacked is false.

data-paused?string

Present when paused is true.

data-placementstring

The content's placement on its trigger, such as bottom-start.

data-sibling?string

Present when frontmost is false.

data-side"top" | "right" | "bottom" | "left"

The side of the trigger the content sits on.

Type
"top" | "right" | "bottom" | "left"
data-stack?string

Present when stacked is true.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"
data-typestring

Mirrors type.

ToastCloseTrigger

PropType
nativeButton?boolean

Whether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).

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

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
AttributeType
data-align"start" | "center" | "end"

How the content aligns to its trigger.

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

Present when frontmost is true.

data-mounted?string

Present when mounted is true.

data-overlap?string

Present when stacked is false.

data-paused?string

Present when paused is true.

data-placementstring

The content's placement on its trigger, such as bottom-start.

data-sibling?string

Present when frontmost is false.

data-side"top" | "right" | "bottom" | "left"

The side of the trigger the content sits on.

Type
"top" | "right" | "bottom" | "left"
data-stack?string

Present when stacked is true.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"
data-typestring

Mirrors type.

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
toastToastPrimitive.Root.Props["toast"]

The toast it renders, from the toast manager.

Type
ToastPrimitive.Root.Props["toast"]
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"

How the content aligns to its trigger.

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

Present when frontmost is true.

data-mounted?string

Present when mounted is true.

data-overlap?string

Present when stacked is false.

data-paused?string

Present when paused is true.

data-placementstring

The content's placement on its trigger, such as bottom-start.

data-sibling?string

Present when frontmost is false.

data-side"top" | "right" | "bottom" | "left"

The side of the trigger the content sits on.

Type
"top" | "right" | "bottom" | "left"
data-stack?string

Present when stacked is true.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"
data-typestring

Mirrors type.

Toaster

PropType
toaster?ToastPrimitive.Provider.Props["toastManager"]

The toast manager it shows, the same as toastManager: the shared toast when unset.

Type
ToastPrimitive.Provider.Props["toastManager"]
AttributeType
data-align"start" | "center" | "end"

How the content aligns to its trigger.

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

Present when frontmost is true.

data-mounted?string

Present when mounted is true.

data-overlap?string

Present when stacked is false.

data-paused?string

Present when paused is true.

data-placementstring

The content's placement on its trigger, such as bottom-start.

data-sibling?string

Present when frontmost is false.

data-side"top" | "right" | "bottom" | "left"

The side of the trigger the content sits on.

Type
"top" | "right" | "bottom" | "left"
data-stack?string

Present when stacked is true.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"
data-typestring

Mirrors type.

ToastProvider

PropType
limit?number

The maximum number of toasts that can be displayed at once. When the limit is exceeded, the oldest toasts are marked as limited (via the data-limited attribute) rather than removed, so they can be hidden or animated out.

Default
3
timeout?number

The default amount of time (in ms) before a toast is auto dismissed. A value of 0 will prevent the toast from being dismissed automatically.

Default
5000
toastManager?object

A global manager for toasts to use outside of a React component.

Type
{ listener: (data: ToastManagerEvent) → void) → () → void; add: <T extends Data = Data>(options: ToastManagerAddOptions<T>) → string; close: (id?: string) → void; update: <T extends Data = Data>(id: string, updates: ToastManagerUpdateOptions<T> | ((prevToast: ToastObject<T>) → ToastManagerUpdateOptions<T>)) → void; promise: <Value, T extends Data = Data>(promiseValue: Promise<Value>, options: ToastManagerPromiseOptions<Value, T>) → Promise<Value> }

useToast