Toast
"use client";
import type { ReactNode } from "react";
import { ToastProvider, Toaster } from "@cherry-design-system/react";
export function Providers({ children }: { children: ReactNode }) {
return (
<ToastProvider>
{children}
<Toaster />
</ToastProvider>
);
}Toasts are short, transient notifications announced without moving focus. Wrap the app in ToastProvider, place one Toaster inside it, and call useToast() anywhere below to show them. For a message that stays in the page use Callout or Banner.
"use client";
import { Button, useToast } from "@cherry-design-system/react";
export function SaveButton() {
const toast = useToast();
return (
<Button onClick={() => toast.show("Changes saved", { color: "success", autoHide: 4000 })}>
Save
</Button>
);
}Examples
Colors and descriptions
color sets the status icon tile: info (the default), success, warning, error, loading or default (no tile). description adds a second line.
"use client";
import { Button, useToast } from "@cherry-design-system/react";
export function UploadFailed() {
const toast = useToast();
return (
<Button
onClick={() =>
toast.show("Upload failed", {
color: "error",
description: "The file is larger than 10 MB.",
})
}
>
Upload
</Button>
);
}Toasts are sticky by default (autoHide: 0). Pass autoHide in milliseconds to hide one on its own. A loading toast never hides itself; update or dismiss it.
Action button
action adds a button that runs onClick and then dismisses the toast.
"use client";
import { Button, useToast } from "@cherry-design-system/react";
export function ArchiveButton({ onUndo }: { onUndo: () => void }) {
const toast = useToast();
return (
<Button
onClick={() =>
toast.show("Conversation archived", {
color: "default",
autoHide: 6000,
action: { label: "Undo", onClick: onUndo },
})
}
>
Archive
</Button>
);
}Promise
toast.promise shows a loading toast and swaps it in place for a success or error toast when the task settles. success and error can be functions of the result or the error. Settled toasts hide after 4 seconds unless you pass autoHide. It returns the task's promise.
"use client";
import { Button, useToast } from "@cherry-design-system/react";
export function DeployButton({ deploy }: { deploy: () => Promise<{ url: string }> }) {
const toast = useToast();
return (
<Button
onClick={() =>
toast.promise(deploy, {
loading: "Deploying",
success: (result) => `Deployed to ${result.url}`,
error: "Deploy failed",
})
}
>
Deploy
</Button>
);
}Update and dismiss
show returns the toast's id. Pass it to update to change the toast in place, to dismiss for an animated removal, or to remove to drop it immediately. Passing an existing id to show replaces that toast.
"use client";
import { Button, useToast } from "@cherry-design-system/react";
export function SyncButton({ sync }: { sync: () => Promise<void> }) {
const toast = useToast();
return (
<Button
onClick={async () => {
const id = toast.show("Syncing", { color: "loading" });
await sync();
toast.update(id, { title: "Synced", color: "success", autoHide: 3000 });
}}
>
Sync
</Button>
);
}Toaster placement
import { Toaster } from "@cherry-design-system/react";
<Toaster position="bottom" align="right" visibleToasts={5} expand />;Properties
ToastProvider
The part of the app that can show toasts. Place it once, near the root.
Toaster
Horizontal placement. Defaults to "center".
Anchored edge. Defaults to "top".
Keep every toast fanned out instead of collapsing them into a deck.
How many toasts show at once; older ones wait behind. Defaults to 3.
Accessible name of the region. Defaults to "Notifications".
Accessible name of each close badge. Defaults to "Dismiss notification".
Toaster renders the toasts of the nearest ToastProvider. Place it once. It takes no other attributes.
useToast
useToast() returns the toast API:
Shows a toast and returns its id.
Changes a toast in place. ToastUpdate takes title plus any ToastOptions except id.
Animated removal.
Immediate removal, no exit animation.
Loading toast that turns into success or error when the task settles. messages is { loading, success, error }.
The toasts currently in the stack.
ToastOptions
Status color and icon tile. default has no tile. Defaults to "info".
Milliseconds before the toast hides itself. 0 (the default) is sticky.
Optional second line under the title.
Optional button. Runs onClick, then dismisses the toast.
Close badge and swipe to dismiss. Defaults to true.
Reuse an existing toast's id to replace that toast in place.
Styling
| Hook | Values |
|---|---|
| Class | cherry-toaster |
data-color | default, info, success, error, warning, loading |
data-align | center, left, right |
data-position | top, bottom |
| States | data-mounted, data-exiting, data-dragging, data-concealed, data-settled, data-expanded |
| Parts | cherry-toast, cherry-toast__card, cherry-toast__content, cherry-toast__tile, cherry-toast__glyph, cherry-toast__stroke, cherry-toast__text, cherry-toast__title, cherry-toast__description, cherry-toast__action, cherry-toast__close |
Cards read --cherry-bg-elevated, --cherry-border-default and --cherry-elevation-lg. Status tiles use --cherry-info-solid, --cherry-success-solid, --cherry-warning-solid and --cherry-danger-solid.
Accessibility
Toasts are announced through a polite live region without moving focus. The close button is labelled with dismissLabel, and Escape dismisses. Hovering or focusing the stack pauses auto-hide. Do not put the only copy of an important action in a toast.