Toast
Web/MobileA succinct message that appears temporarily to provide feedback.
Installation
pnpm dlx cubix-ui@latest add toastUsage
import { Button } from "@/components/cubix/button"
import { toast } from "@/components/cubix/toast"
<Button
variant="outline"
size="sm"
onClick={() =>
toast.add({
title: "تغییرات ذخیره شد",
description: "پروفایل شما بهروزرسانی شد.",
})
}
>
ذخیره تغییرات
</Button>
Composition
Render Toaster once in your root layout, then call toast.add from any client component. The stack starts right-to-left at the bottom-left of the screen; pass dir="ltr" to move it to the bottom-right and flip the swipe direction.
import { Toaster } from "@/components/cubix/toast"
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="fa" dir="rtl">
<body>
<Toaster>{children}</Toaster>
</body>
</html>
)
}Accessibility
Toasts render inside a labelled region. Screen readers announce each toast when it appears: politely by default, and immediately with priority="high". Close timers pause while the pointer is over the stack or focus is inside it, and the close button is labelled بستن.
| Key | Action |
|---|---|
| F6 | Moves focus to the toast stack and pauses the close timers. |
| Tab / Shift+Tab | Moves focus between toasts and their buttons. |
| Enter / Space | Activates the focused action or close button. |
| Escape | Closes the focused toast. |
Examples
Basic
Pass a title and description to toast.add. The toast closes on its own after timeout milliseconds (5000 by default).
Types
Set type to render a status icon: success, info, warning, error, or loading.
Action
Pass actionProps to render an action button. Keep the id returned by toast.add to close the toast from the action.
Promise
Use toast.promise to show a loading toast that is replaced by a success or error toast when the task settles.
Custom styling
Pass data to change a single toast. icon replaces the status icon, actions renders buttons below the description, and className is merged into the toast root. Leave out actionProps when you use actions, and close the toast with toast.close(id).
API Reference
Note: Parts render data-slot attributes (toast-viewport, toast, toast-icon, toast-title, toast-description, toast-actions, toast-action, toast-close) for targeting in tests and parent selectors. Use the same props on Base UI, React Aria, and Radix.
toast
| Prop | Type | Default | Description |
|---|---|---|---|
| toast.add | (options) => string | - | Creates a toast from the options below and returns its id. |
| toast.promise | (promise, options) => Promise<T> | - | Shows a loading toast, then a success or error toast when the promise settles. Each state accepts a string, options, or a function of the result. |
| toast.close | (id: string) => void | - | Closes a toast by id. |
| title | React.ReactNode | - | Heading of the toast. |
| description | React.ReactNode | - | Supporting text below the title. |
| type | "success" | "info" | "warning" | "error" | "loading" | - | Renders a status icon. Loading toasts stay open until replaced or closed. |
| actionProps | { children?, onClick?, className? } | - | Renders an action button next to the content. |
| data | { icon?, actions?, className? } | - | Per-toast customization: a custom icon, extra buttons below the description, and extra classes for the toast root. |
| timeout | number | 5000 | Milliseconds before the toast closes on its own. |
| priority | "low" | "high" | "low" | Low priority toasts are announced politely. High priority toasts interrupt the screen reader; use them for errors. |
| onClose | () => void | - | Called when the toast closes. |
Toaster
| Prop | Type | Default | Description |
|---|---|---|---|
| dir | "ltr" | "rtl" | "rtl" | Direction of the toast stack. Sets dir and lang on the viewport and flips the swipe-to-dismiss side. |
| children | React.ReactNode | - | App content rendered before the toast viewport. |