Toast
"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:
"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:
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:
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$expandto 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: falseto remove the badge and swipe. - Persistence. A toast without
autoHidestays until it is dismissed;loadingtoasts 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 underprefers-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.
The title of the toast.
The status tile. default has no tile and loading shows a spinner.
Defaults to "info".
A second, smaller line under the title.
Renders a small button that runs onClick, then dismisses the toast.
Milliseconds before the toast hides itself. Omit to keep it until
dismissed. Ignored for loading.
Shows the close badge and allows swiping the toast away. Defaults to true.
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.
The id returned by addNotification.
The fields to change. Changing autoHide restarts its timer.
dismissNotification
Removes a toast with its exit animation, the same way the close badge does.
The id of the toast to dismiss.
promiseNotification
The work to track.
Titles for each phase. success may be a function of the result and
error a function of the error.
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.
The id of the toast to remove.
ToastNotifications
Horizontal position of the stack. Defaults to "center".
Anchors the stack to the bottom of the viewport instead of the top.
Keeps every toast fanned out instead of collapsing them into a deck.
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.