components / feedback
Toast
Stacked notifications that dismiss themselves, with swipe and keyboard support.
1.3 kB gzip client componentbase ui · toast
toast-default.tsx
"use client";
import { Button } from "@/components/ui/button";
import { ToastProvider, useToast } from "@/components/ui/toast";
function Buttons() {
const toast = useToast();
return (
<div className="flex flex-wrap justify-center gap-3">
<Button onClick={() => toast.add({ title: "Changes saved", description: "Your profile is up to date.", type: "success" })} variant="outline">
show toast
</Button>
<Button onClick={() => toast.add({ title: "Upload failed", description: "Check your connection and try again.", type: "error" })} variant="outline">
show error
</Button>
</div>
);
}
export default function ToastDefault() {
return (
<ToastProvider>
<Buttons />
</ToastProvider>
);
}Installation
$ npx shadcn@latest add https://cd-ui.vercel.app/r/toast.jsonfirst time? set up components.json and the theme first: see installation.
Usage
import { ToastProvider, useToast } from "@/components/ui/toast"
// once, at the root of your app
<ToastProvider>{children}</ToastProvider>
// anywhere below it
const toast = useToast()
toast.add({ title: "Saved", description: "Your changes are live.", type: "success" })API
ToastProvider
Base UI · Toast.ProviderWrap your app once. It renders the stack of notifications in the bottom-right corner.
| prop | type | default |
|---|---|---|
timeoutTime (ms) before a toast dismisses itself. Use | number | 5000 |
limitMaximum number of visible toasts. | number | 3 |
closeLabelLabel of the X for screen readers. | string | "Close" |
useToast
Base UI · Toast.useToastManagerHook that returns the manager. Call add({ title, description, type }) to show a toast; it also has close and update.
| prop | type | default |
|---|---|---|
title · descriptionText shown in the toast. | ReactNode | - |
typeFree text. | string | - |
createToastManager
Base UI · Toast.createToastManagerCreates a manager you can use outside React (pass it to ToastProvider as toastManager).
| prop | type | default |
|---|
Keyboard
| key | action |
|---|---|
| F6 | Moves focus to the toast area. |
| Tab | Moves between toasts and their close buttons. |
| EnterSpace | Dismisses the toast when its X is focused. |
Accessibility
- Toasts are announced by screen readers without taking focus. Don't put the only copy of important information in a toast.
- The timer pauses on hover and while the window is out of focus, so there is time to read.