Skip to content

Components

Toast

jsx
"use client";
import React from "react";
import {
  ToastNotifications,
  ToastNotificationsProvider,
} from "cherry-styled-components";

export default function App({ children }) {
  return (
    <ToastNotificationsProvider>
      <ToastNotifications />
      {children}
    </ToastNotificationsProvider>
  );
}

Cherry's toast system shows short notifications as cards in a stack. It consists of three parts: a provider that holds the state, a component that renders the stack, and a hook to fire notifications from anywhere in the tree.

Each toast is a card with a colored icon tile, a bold title, an optional description line, an optional action button, and a close badge on the corner. Several toasts collapse into a deck: the newest sits in front and older ones peek out behind it. Hovering, focusing, or tapping the stack fans it out.

For design guidelines and all interactive states, see the Overlays design page.

Wrap your app with the provider and render the stack once, as in the example above. It assumes the Cherry theme provider from Installation is already set up; rendering a toast without a theme throws.

Fire notifications with the useToastNotifications hook. The first argument is the title; everything else is optional:

jsx
"use client";
import React from "react";
import { Button, useToastNotifications } from "cherry-styled-components";

export default function SaveButton() {
  const { addNotification } = useToastNotifications();

  return (
    <Button
      onClick={() =>
        addNotification("Changes saved", {
          color: "success",
          description: "Your profile is up to date.",
          autoHide: 4000,
        })
      }
    >
      Save
    </Button>
  );
}

Actions

Give a toast an action to render a small button next to the text. Clicking it runs onClick, then dismisses the toast:

jsx
addNotification("Message archived", {
  color: "default",
  autoHide: 5000,
  action: { label: "Undo", onClick: () => restoreMessage(id) },
});

The default color has no icon tile, which suits plain confirmations like this one.

Promise toasts

promiseNotification shows a loading toast with a spinner, then turns the same toast into a success or error toast when the promise settles. The icon swaps in place, so the stack does not jump:

jsx
const { promiseNotification } = useToastNotifications();

promiseNotification(publishPost(), {
  loading: "Publishing your post",
  success: (post) => `Published "${post.title}"`,
  error: "Couldn't publish",
}).catch(() => {});

success and error can be plain values or functions of the result or the error. The settled toast hides itself after 4 seconds unless you pass autoHide. The returned promise is your task's own promise, so handle its rejection as you would without the toast.

Behavior

  • Deck. The newest toast sits in front; up to $visibleToasts (default 3) are shown, the older ones peeking out behind it, smaller, with their content hidden. Pass $expand to keep every toast fanned out instead.
  • Fan out. Hovering, focusing, or tapping the stack spreads the toasts out. While the stack is in use, and while the browser tab is hidden, auto-hide timers pause and pick up where they left off.
  • Dismiss. The close badge appears on hover or focus (always on touch screens). A toast can also be swiped toward the edge it is anchored to, or sideways, and Escape dismisses the focused toast. Pass dismissible: false to remove the badge and swipe.
  • Persistence. A toast without autoHide stays until it is dismissed; loading toasts always stay until updated or dismissed. Once a toast has animated out it is removed from the stack entirely.
  • Accessibility. The stack is an aria-live="polite" region labeled "Notifications", so screen readers announce new toasts and in-place updates. All motion turns off under prefers-reduced-motion.

Properties

useToastNotifications returns { notifications, addNotification, updateNotification, dismissNotification, removeNotification, promiseNotification }. The notifications array holds the toasts currently in the stack; each entry carries a unique numeric id plus its text, color, autoHide, description, action, dismissible, and an exiting flag while it animates out.

addNotification

Adds a toast and returns its id.

textReactNoderequired

The title of the toast.

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

The status tile. default has no tile and loading shows a spinner. Defaults to "info".

config.descriptionReactNode

A second, smaller line under the title.

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

Renders a small button that runs onClick, then dismisses the toast.

config.autoHidenumber

Milliseconds before the toast hides itself. Omit to keep it until dismissed. Ignored for loading.

config.dismissibleboolean

Shows the close badge and allows swiping the toast away. Defaults to true.

config.idnumber

Reuse the id of a toast in the stack to replace it in place instead of adding a new one.

updateNotification

Changes a toast that is still on screen, keeping its place in the stack. Toasts that were dismissed are left alone.

idnumberrequired

The id returned by addNotification.

update{ text?, color?, description?, action?, autoHide?, dismissible? }required

The fields to change. Changing autoHide restarts its timer.

dismissNotification

Removes a toast with its exit animation, the same way the close badge does.

idnumberrequired

The id of the toast to dismiss.

promiseNotification

taskPromise<T> | (() => Promise<T>)required

The work to track.

messages{ loading, success, error }required

Titles for each phase. success may be a function of the result and error a function of the error.

configToastConfig

Any other toast options, such as description or autoHide for the settled toast (default 4000).

removeNotification

Removes a toast from state immediately, skipping the exit animation. Prefer dismissNotification; this is for clearing state without motion.

idnumberrequired

The id of the toast to remove.

ToastNotifications

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

Horizontal position of the stack. Defaults to "center".

$bottomboolean

Anchors the stack to the bottom of the viewport instead of the top.

$expandboolean

Keeps 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.

Exports

The library also exports the types ToastColor, ToastAlign ("center" | "left" | "right"), ToastConfig, ToastUpdate, ToastAction, ToastPromiseMessages, and ToastNotification (an entry of the notifications array). For advanced use it exposes ToastNotificationsContext, the context behind the hook, and the styled primitives StyledNotifications and StyledNotificationItem.