Toast
A short message that appears at the edge of the screen and goes by itself (DGA: Notification Toast). Five kinds, an action, a close button, and announced to screen readers.
Version 0.4.1 ·
Installation
bunx --bun shadcn@latest add @dga/toastUsage
import { Button } from "@/components/ui/button";
import { Toaster, toast } from "@/components/ui/toast";
// Once, at the root of the app (for example in layout.tsx).
export function Providers({ children }: { children: React.ReactNode }) {
return (
<>
{children}
<Toaster closeLabel="Close" regionLabel="Notifications" />
</>
);
}
export function SubmitButton() {
return (
<Button
onClick={() =>
toast.add({
type: "success",
title: "Request submitted",
description: "Your reference number is 24810.",
})
}
>
Submit
</Button>
);
}Use a Toast to confirm something that just happened and needs no reply: a request was sent, a draft was saved. Do not use it for an error that blocks the person (show it next to the field, or in an Inline Alert), or for anything they must act on before it disappears. Mount one Toaster at the root and call toast.add({ title, description, type }) from anywhere, even outside React. type is neutral, info, success, warning or error. A toast goes after 6 seconds; pass timeout: 0 for one that stays until it is closed, and use that for errors people need to read. Keep the message to one short sentence; an actionProps button gives one quick action (such as Undo).
Anatomy
- Toaster (
role="region", a live region): the list at the bottom end of the screen. It holds up to 3 toasts. - Toast: 484px wide (full width on phones), radius-md,
shadow-3xl, onbackground-notification-white. - Bar: 8px on the start side, in the kind's 400 colour.
- Icon, title, description, an optional action and a close button.
Examples
Kinds
Press a button to add a toast. error and warning are announced at once (priority: "high"), the others politely. Success here has an action.
Timeout, priority and actions
A toast closes by itself after six seconds. Pass timeout: 0 to toast.add to keep it until the person closes it, and actionProps to add a button. Warnings and errors use priority: "high", so a screen reader announces them at once; the others wait their turn. Only three toasts show at once (limit on Toaster), and the rest queue behind them.
RTL
Toasts appear at the bottom end of the screen: the bottom right in English and the bottom left in Arabic. The bar is on the start side, and a swipe toward the edge they came from dismisses them.
Accessibility
| Key | Action |
|---|---|
| F6 | Moves focus into the toast region (when there are toasts). Escape or a click elsewhere returns it. |
| Tab | Moves between the action and close buttons of the toasts. |
| Enter | Presses the focused action or close button. |
| Escape | Closes the focused toast. |
- Built on Base UI Toast. The viewport is a live region: new toasts are announced without moving focus (
polite, orassertiveforpriority: "high"). Announce short, complete sentences. - A toast does not go while the pointer is over it or focus is inside it, and the timer restarts after (WCAG 2.2.1). Use
timeout: 0for anything people may need more time to read. - The kind is shown by an icon and the title as well as the bar, never by colour alone. Text is 4.5:1 and icons 3:1 or better, in light and dark. The close button has an accessible name (
closeLabel). - With reduced motion a toast appears and goes without sliding.
API Reference
Toaster
Put one Toaster at the root of the app (DGA: Notification Toast). It lists the toasts at the bottom end of the screen: bottom right on English pages, bottom left on Arabic pages. A toast is announced to screen readers when it appears (a polite live region, assertive for errors and warnings), stays while it is hovered or focused, and can be swiped away.
| Prop | Type | Default |
|---|---|---|
numberThe most toasts shown at once. | 3 | |
| ||
numberHow long a toast stays, in milliseconds. `0` keeps it until it is closed. | 6000 | |
| ||
stringAccessible name of the close button. Translate it on Arabic pages. | "Close" | |
| ||
stringAccessible name of the region that holds the toasts. Translate it on Arabic pages. | "Notifications" | |
| ||
Also accepts every prop of the Base UI Toast (opens in a new tab).
Deviations from DGA
- The neutral bar is
border-neutral-primary: DGA draws it white, which is invisible on the white card. The bar is logical (the start side). - DGA specifies no timing or announcement. Toasts pause while hovered or focused, can be swiped away, are announced, and the Toaster sits on the bottom end of the screen.
| Change | Reason |
|---|---|
Toast: neutral bar, timing and live region | A message that disappears must be announced and must not vanish while someone is reading it (WCAG 4.1.3, 2.2.1). |
Contrast
24 pairs checked · all pass WCAG 2.1 AAShow 12 statesHide
| Variant / state | Light | Dark |
|---|---|---|
| title | 14.53:1 AAA | 14.42:1 AAA |
| description | 10.17:1 AAA | 10.95:1 AAA |
| icon / neutral | 18.09:1 3:1 | 14.42:1 3:1 |
| icon / info | 5.98:1 3:1 | 8.98:1 3:1 |
| icon / success | 5.69:1 3:1 | 9.82:1 3:1 |
| icon / warning | 5.42:1 3:1 | 10.27:1 3:1 |
| icon / error | 6.57:1 3:1 | 8.18:1 3:1 |
| action text | 16.44:1 AAA | 12.71:1 AAA |
| action text, hovered | 14.61:1 AAA | 6.95:1 AA |
| close icon | 18.09:1 3:1 | 14.42:1 3:1 |
| close icon, hovered | 16.44:1 3:1 | 9.59:1 3:1 |
| focus ring | 18.09:1 3:1 | 15.89:1 3:1 |
Text needs 4.5:1, icons and focus rings 3:1. Disabled states are exempt. Full contrast report