# Toast

> Cherry Toast component - provider, hook, and animated toast stack with semantic colors and auto-hide.

Source: https://cherry.al/code/toast

> For the complete documentation index, see [llms.txt](https://cherry.al/llms.txt).

# Toast

<iframe className="light-only" src="https://demo.cherry.al/preview/toast?theme=light" title="Toast notifications" loading="lazy" style={{ width: "100%", height: "320px", border: "1px solid var(--color-grayLight)", borderRadius: "12px" }} />
<iframe className="dark-only" src="https://demo.cherry.al/preview/toast?theme=dark" title="Toast notifications" loading="lazy" style={{ width: "100%", height: "320px", border: "1px solid var(--color-grayLight)", borderRadius: "12px" }} />

Cherry's toast system shows short, animated notifications in a fixed 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.

For design guidelines and all interactive states, see the [Overlays](/overlays) design page.

Wrap your app with the provider and render the stack once. The example assumes the Cherry theme provider from [Installation](/code/installation) is already set up; rendering a toast without a theme throws:

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

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

Fire notifications with the `useToastNotifications` hook:

```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 successfully.", {
          color: "success",
          autoHide: 4000,
        })
      }
    >
      Save
    </Button>
  );
}
```

A toast without `autoHide` stays until the user dismisses it with the built-in close button. Either way, once a toast has animated out it is removed from the stack entirely. The stack is an `aria-live="polite"` region, so screen readers announce new toasts as they appear.

## Properties

`useToastNotifications` returns `{ notifications, addNotification, removeNotification }`. The `notifications` array holds the toasts currently in the stack; each entry carries a unique numeric `id` plus its `text`, resolved `color`, and `autoHide` value.

### addNotification

<Field value="text" type="string" required>
  The message to display.
</Field>

<Field value="config.color" type='"info" | "success" | "error" | "warning"'>
  Semantic color for the status icon. Defaults to `"info"`.
</Field>

<Field value="config.autoHide" type="number">
  Milliseconds before the toast hides itself. Omit to keep it until dismissed.
</Field>

### removeNotification

Removes a toast from state immediately, skipping the exit animation. You rarely need this yourself: the close button and `autoHide` already remove toasts after their exit animation.

<Field value="id" type="number" required>
  The `id` of the toast to remove, as found on the entries of the
  `notifications` array.
</Field>

### ToastNotifications

<Field value="$align" type='"center" | "left" | "right"'>
  Horizontal position of the stack. Defaults to `"center"`.
</Field>

<Field value="$bottom" type="boolean">
  Anchors the stack to the bottom of the viewport instead of the top.
</Field>

## Exports

The library also exports the types `ToastColor` (`"info" | "success" | "error" | "warning"`), `ToastAlign` (`"center" | "left" | "right"`), and `ToastConfig` (`{ color?, autoHide? }`; `addNotification` defaults `color` to `"info"` and `autoHide` to `0`, which keeps the toast until dismissed). For advanced use it exposes `ToastNotificationsContext`, the context behind the hook, and the styled primitives `StyledNotifications` and `StyledNotificationItem`.

<Button href="https://github.com/cherry-design-system/styled-components/blob/main/src/lib/toast.tsx" icon="code" iconPosition="left">
  View Source
</Button>
