Toast
A stacked, auto-dismissing notification queue
Workspace
Installation
npx shadcn@latest add @sudokw/components-toastimport { ToastProvider } from "@gpuixui/components";Usage
import { Duration } from "effect";
import { Button } from "@gpuixui/primitives";
import {
ToastProvider,
ToastViewport,
useToast,
} from "@gpuixui/components/toast";
const PublishBar = () => {
const toast = useToast();
return (
<Button
onActivate={() =>
toast.push(
{
title: "Changes published",
description: "The site rebuilds in about a minute.",
tone: "success",
action: { label: "Undo" },
duration: Duration.seconds(8),
},
{ onAction: () => restore() },
)
}
>
Publish changes
</Button>
);
};
export const App = () => (
<ToastProvider>
<PublishBar />
{/* Render the viewport last. */}
<ToastViewport />
</ToastProvider>
);const rows = await toast.promise(importRows(file), {
loading: { title: "Importing rows", description: "Reading the file…" },
success: (count) => ({ title: `Imported ${count} rows`, tone: "success" }),
error: (cause) => ({ title: "Import failed", description: String(cause), tone: "danger" }),
});Composition
ToastProvider
└── ToastViewportAPI reference
| Part | Prop | Type | Default |
|---|---|---|---|
ToastProvider | max | number — toasts kept before the oldest is dropped | 3 |
ToastProvider | defaultDuration | Duration.Input | 5 seconds |
ToastProvider | children | the tree that may push | — |
ToastViewport | swipe | boolean — sideways drag to dismiss | true |
ToastViewport | children | an optional control before the stack | — |
ToastViewport | className | string — joined after the recipe | — |
useToast | toasts | ReadonlyArray<Toast> — oldest first | — |
useToast | push | (draft, options?) => void — title required; description, tone, duration, action, phase optional | — |
useToast | promise | (p, { loading, success, error }) => Promise — settles one row | — |
useToast | dismiss | (id: string) => void | — |
useToast | dismissAll | () => void | — |
useToast | pause | (id: string) => void — stop one clock | — |
useToast | resume | (id: string) => void — restart with the remaining time | — |
useToast | hold | (held: boolean) => void — the stack's clock | — |
useToast | actionFor | (id: string) => Option<() => void> — the queued handler | — |
ToastInput | title | string — non-empty; empty input is dropped | required |
ToastInput | description | string | — |
ToastInput | tone | "info" | "success" | "warning" | "danger" | "info" |
ToastInput | phase | "loading" | "settled" — a loading row has no clock | "settled" |
ToastInput | duration | Duration.Input — 0 keeps it until dismissed | the provider's default |
ToastInput | action | { label } — the handler travels beside the draft | — |
Edit this page on GitHubapps/docs/content/docs/components/toast.mdx