Skip to content

Overlays

Toast

tsx
"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.

tsx
"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.

tsx
"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.

tsx
"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.

tsx
"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.

tsx
"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

tsx
import { Toaster } from "@cherry-design-system/react";

<Toaster position="bottom" align="right" visibleToasts={5} expand />;

Properties

ToastProvider

childrenReactNoderequired

The part of the app that can show toasts. Place it once, near the root.

Toaster

align"left" | "right" | "center"

Horizontal placement. Defaults to "center".

position"top" | "bottom"

Anchored edge. Defaults to "top".

expandboolean

Keep every toast fanned out instead of collapsing them into a deck.

visibleToastsnumber

How many toasts show at once; older ones wait behind. Defaults to 3.

labelstring

Accessible name of the region. Defaults to "Notifications".

dismissLabelstring

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:

show(title: ReactNode, options?: ToastOptions) => number

Shows a toast and returns its id.

update(id: number, update: ToastUpdate) => void

Changes a toast in place. ToastUpdate takes title plus any ToastOptions except id.

dismiss(id: number) => void

Animated removal.

remove(id: number) => void

Immediate removal, no exit animation.

promise<T>(task: Promise<T> | (() => Promise<T>), messages: ToastPromiseMessages<T>, options?: ToastOptions) => Promise<T>

Loading toast that turns into success or error when the task settles. messages is { loading, success, error }.

toastsToastItem[]

The toasts currently in the stack.

ToastOptions

color"default" | "info" | "success" | "error" | "warning" | "loading"

Status color and icon tile. default has no tile. Defaults to "info".

autoHidenumber

Milliseconds before the toast hides itself. 0 (the default) is sticky.

descriptionReactNode

Optional second line under the title.

action{ label: string; onClick: () => void }

Optional button. Runs onClick, then dismisses the toast.

dismissibleboolean

Close badge and swipe to dismiss. Defaults to true.

idnumber

Reuse an existing toast's id to replace that toast in place.

Styling

HookValues
Classcherry-toaster
data-colordefault, info, success, error, warning, loading
data-aligncenter, left, right
data-positiontop, bottom
Statesdata-mounted, data-exiting, data-dragging, data-concealed, data-settled, data-expanded
Partscherry-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.