Toast
A brief message about something that just happened, announced without taking focus.
Installation
npx shadcn@latest add @oakoss/toastThis also installs Button, for the action and close buttons, and Spinner, for loading toasts.
Usage
Mount one Toaster at the root of the app, then call toast() from anywhere,
including outside React:
import { Toaster } from '#/components/ui/feedback/toast/toast';
export function App() {
return (
<>
<Routes />
<Toaster />
</>
);
}import { toast } from '#/components/ui/feedback/toast/toast';
toast.success('Changes saved', { description: 'All files are up to date.' });toast() returns the toast's id. Showing a toast again with the same id
updates it in place and restarts its timer, and toast.dismiss(id) closes
it. One Toaster per store: a second one on the same store renders every
toast twice, and limit, duration and visible apply to the whole store.
toast() uses the app's store; a Toaster with its own ToastStore, as in
the placement example, keeps its toasts apart.
When to use a toast
A toast reports something that just happened and needs nothing more from the user. Don't use one for:
- Errors the user can fix, such as validation: show them next to the field, where they stay until fixed.
- Results the user must see: a toast closes on its own and can be missed.
- The only path to an action or fact: whatever a toast says or offers has to be reachable elsewhere, such as an undo in the item's own menu.
Keep it to one sentence, two lines at most, and don't narrate an async flow
step by step; toast.promise shows the loading and the result in one toast.
Text and timing
A toast stays at least 5 seconds, longer with duration, and pauses while
it's hovered or focused and while the page is hidden. A toast with an action,
and a loading toast, stays until it's closed or updated. Its title and
description must render on the first pass, since that's the text the
Toaster announces; text that loads later is never read out.
The stack
Toasts stack at the screen's edge, newest in front, with the next two peeking
behind it. The stack opens while it's hovered or has focus, or after a tap on
a touch screen until a tap outside. Escape on the region closes it again.
Timers pause while the stack is hovered, focused or tapped open.
visible(default 3) sets how many toasts the stack shows. Older ones sit behind it, hidden from sight and screen readers, and move up as the ones in front close. A toast's timer starts only once it's shown, so none times out unseen. While keyboard or screen-reader focus is inside the stack, all of them show.limit(default 10) caps how many toasts are kept; past it the oldest closes, with the reason'evicted'.isExpandedkeeps the stack open, for toasts whose actions shouldn't hide behind another. Their timers still run.
A toast can be swiped away by touch or pen, toward the stack's side and the
edge it's aligned to; a centered stack swipes only toward its side. A mouse
selects the toast's text instead, and the close button and Escape work for
everyone. A touch that starts on a toast doesn't scroll the page.
On a phone the stack spans the screen, clears the notch and home bar, and sits above the on-screen keyboard. A stack taller than its room scrolls, starting at the newest toast; a vertical drag on a toast then scrolls rather than swipes.
After a redirect
A toast shown just before a server action or redirect is lost with the page. Pass the message across instead, such as in a short-lived cookie the server sets, and show it once the next page loads:
useEffect(() => {
const message = readFlashCookie();
if (message) {
clearFlashCookie();
toast.success(message);
}
}, []);Coming from Sonner
The calls follow shadcn's Sonner toast. These are the differences that change code:
toast.erroristoast.destructive.- An action is an object with
onPressrather thanonClick, never an element, and there's nocancel; every toast has a close button. onDismissandonAutoCloseare oneonClose, which gets the reason:'user','programmatic','timeout'or'evicted'.toast.customtakes atitleoption, which names the toast for screen readers.<Toaster position="bottom-right">isplacement="bottom end", which follows the reading direction.visibleToastsisvisible, andexpandisisExpanded.limitis new: the most toasts kept before the oldest closes.- Durations under 5 seconds are raised to 5.
Examples
Intents
toast.info, toast.success, toast.warning and toast.destructive add an
icon and color. A destructive toast interrupts screen readers; the others
wait their turn:
Actions
action adds a button and keeps the toast open until it's closed. A promise
from onPress keeps the button pending until it settles, then the toast
closes; pass closeOnAction: false to keep it. There's no timeout, so the
promise has to settle. If onPress throws or its promise rejects, the toast
stays and onError receives the error, or reportError without it. Updating
a toast in place readies its action again, even while the previous action's
work is still running, so an action that reports progress in its own toast
should use another id or guard against running twice:
Promises
toast.promise shows a loading toast, then updates it with the result.
success and error can be text, a function of the result or error, or an
object of title and options:
Custom content
toast.custom draws the whole toast beside its close button. Its title
names the toast and is what screen readers announce, so it should say what
the content shows:
Stack
Add a few toasts to see the stack: hover, focus or tap it to open it.
isExpanded keeps it open:
Placement
placement puts the stack at a corner or the middle of the top or bottom
edge, following the reading direction. Swipes go toward the side and the
edge the stack is aligned to:
Accessibility
Each toast is a non-modal alertdialog inside a landmark region named for
the number of toasts, so it doesn't take focus or trap it. Announcing is the
Toaster's job rather than the toast's:
- Announcements: two live regions, mounted with the
Toasterso screen readers already watch them, read each toast's title and description once when it appears and again when its text changes. Destructive toasts andpriority: 'high'go to the assertive region, the rest to the polite one. - Reaching a toast: F6 moves focus to the toasts, cycling through the
landmarks React Aria knows about; plain
<main>and<nav>elements aren't among them. Each toast is in the tab order with its buttons. When the last toast closes, focus returns to where it was before the toasts. - Timing: a toast stays at least 5 seconds and pauses while hovered, focused or hidden, and a toast with an action never times out, so its action stays reachable.
- Modals: the
Toastersits on top of an open React Aria modal and stays outside its inert background, so a toast shown over a dialog is still reachable. A native<dialog>opened withshowModal()is different: the browser draws it above everything else and makes the rest of the page inert, toasts included, so show the message inside such a dialog instead. - The stack: toasts waiting behind the stack, past
visible, are inert and hidden from screen readers, but each is announced when it appears. While keyboard or screen-reader focus is in the stack, every toast shows, so focus never lands on a hidden one; a click doesn't reveal them. A stack taller than the screen becomes a tab stop, so the arrow keys scroll it, and Tab to a toast scrolls it into view. - Labels: React Aria translates the close button's label and the
region's name;
messagesreplaces them.
| Key | Action |
|---|---|
F6 | Moves focus to the toasts, or the next React Aria landmark |
Shift + F6 | Moves focus to the previous React Aria landmark |
Tab | Moves between toasts and their buttons |
Escape | Closes the focused toast; on the region, closes the stack |
| Arrow keys | Scroll the region, when the stack is taller than the screen |
Enter/Space | Presses the focused button |
Toasts follow React Aria's Toast pattern; WAI-ARIA has no toast pattern of its own.
API
Toaster
Renders the toasts in a portal on document.body. Its data-slots are
toast-portal (the wrapper with the live regions) and toast-viewport (the
region).
Props
Prop
Type
Data attributes
| Attribute | Present when |
|---|---|
data-slot | toast-viewport on the region |
data-placement | Always, on the region; the placement |
data-expanded | On the region, while the stack is open |
data-scrollable | On the region, while the stack is taller than it |
CSS variables
| Variable | Sets |
|---|---|
--z-toast | The region's stacking order, above modals |
--toaster-height | The stack's height, set on the region |
--toast-front-height | The front toast's height, which toasts behind it take |
--toaster-viewport, --toaster-top, --toaster-bottom | The visible viewport's height and its offsets, which keep the stack above an on-screen keyboard |
toast
| Call | Shows |
|---|---|
toast(title, options?) | A neutral toast; returns its id |
toast.info, .success, .warning | A toast with that intent's icon and color |
toast.destructive | An error toast, announced assertively |
toast.loading(title, options?) | A spinner toast that stays until updated |
toast.promise(promise, states) | Loading, then success or error; returns the promise |
toast.custom(render, { title }) | Your own content, named by title |
toast.dismiss(id?) | Closes one toast, or every toast without an id |
toast.dismissAll() | Closes every toast |
Options
Prop
Type
A toast also takes duration, in milliseconds, or an action of label,
onPress and an optional onError, but not both: a toast with an action
stays until it's closed.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | toast, and toast-title, toast-description, toast-icon, toast-action, toast-close and toast-inner on its parts |
data-intent | Always; the intent |
data-entering | During its first frame |
data-exiting | While it plays its exit |
data-behind | While the stack is closed, on every toast but the front one |
data-swiping | While it follows a finger or pen |
aria-hidden | While it waits behind the stack, past visible |