# Toast

A short message about something the user just did, in a corner or at the top or bottom center of the screen: Changes saved, Link copied, Upload failed. It closes on its own after a few seconds, or when swiped away. Several at once overlap in a stack, the newest in front, and spread out on hover or focus. Mount one Toaster at the root of the app and call toast({ title }) from an event handler; a Toast element shown with state works too.

**Dependencies:** npm: 3 direct, 5 total; registry: 3 direct, 3 total.

Native: iOS stable, Android stable.

## Usage

### When to use

- Confirm an action the user just took, without stopping them: Changes saved, Link copied.
- Offer a quick undo right after an action, when the same undo also exists elsewhere.

### When not to use

- An error that blocks work. Show it in the page, next to what failed, for example with Field.
- A question or a decision. Use AlertDialog.
- A state that lasts, such as a failed build. Use Badge.

### Native

The surface alone. The toaster draws it above the safe area and the bottom bar.

**Surface**

```text
WerewolfToast(title: AnyView(Text("Changes saved")), variant: .success, onDismiss: {})
```

```text
WerewolfToast(variant = WerewolfToastVariant.Success, onDismiss = {}) { Text("Changes saved") }
```

Call show from an event handler. The queue, timers, announcement and placement belong to the toaster.

**Shown through the toaster**

```text
@Environment(\.werewolfToaster) private var toaster
// ...
toaster.show(title: "Changes saved", variant: .success, duration: 4)
```

```text
val toaster = LocalWerewolfToaster.current
// ...
toaster.show(title = "Changes saved", variant = WerewolfToastKind.Success, durationMs = 4000)
```

One action. It also lives elsewhere in the app, since the toast closes on its own.

**With an action**

```text
toaster.show(title: "Project archived", actionLabel: "Undo", action: undo)
```

```text
toaster.show(title = "Project archived", actionLabel = "Undo", onAction = undo)
```

## Variants

### variant

default has no icon. success, danger, warning and info add their status icon; the surface stays neutral.

Values: `default`, `success`, `danger`, `warning`, `info`. Default: `default`.

### position

Where the toast shows. Set on the Toaster for all its toasts, or on one toast() call. Corners swipe sideways to dismiss; the centers swipe toward their edge.

Values: `bottom-right`, `bottom-left`, `bottom-center`, `top-right`, `top-left`, `top-center`. Default: `bottom-right`.

## Examples

### Saved

A button here; pressing it shows the toast on the screen, as toast() does in an app.

```tsx
<Button variant="secondary" onClick={() => toast({ variant: "success", title: "Changes saved" })}>Show toast</Button>
```

### With an action

A longer duration, since the user has to reach the button. The action also lives in the History panel. In an app: toast({ title: "Project archived", duration: 8000, action: <ToastAction altText="Undo from the History panel" onClick={undo}>Undo</ToastAction> }).

```tsx
<Button variant="secondary" onClick={archiveProject}>Archive project</Button>
```

### Failed

```tsx
<Button variant="secondary" onClick={() => toast({ variant: "danger", title: "Upload failed", description: "The file is larger than 25 MB." })}>Show toast</Button>
```

### Top center

Pick a corner or an edge center with position. At the top the stack hangs down, and a swipe up dismisses a toast.

```tsx
<Button variant="secondary" onClick={() => toast({ variant: "success", title: "Link copied", position: "top-center" })}>Show toast</Button>
```

### Info

```tsx
<Button variant="secondary" onClick={() => toast({ variant: "info", title: "Export started", description: "The file downloads when it is ready." })}>Show toast</Button>
```

### Stacked

Press the button several times. The toasts overlap, the newest in front; hover or focus the stack to spread it out. Only the 3 newest show at once.

```tsx
<Button variant="secondary" onClick={() => toast({ variant: "success", title: "Changes saved" })}>Show toast</Button>
```

## Do and don't

### Do

- Confirm an action the user just took: Changes saved, Link copied.
- Write the title in the past tense, and keep it to a few words.
- Mount one Toaster at the root of the app, and call toast() after the action is done.
- Keep the stack on and maxVisible low (3), so a burst of toasts never covers the page. Reuse an id, toast({ id: "saved" }), for an action the user repeats, so one toast updates instead of piling up.

### Don't

- Don't put an error that blocks work in a toast; show it in the page, next to what failed.
- Don't use it for a question or a decision; use AlertDialog.
- Don't put the only way to do something in a toast action; it closes on its own.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `variant` | `enum` | `default`, `success`, `danger`, `warning`, `info` | `default` | default has no icon. success, danger, warning and info add their status icon; the surface stays neutral. |
| `position` | `enum` | `bottom-right`, `bottom-left`, `bottom-center`, `top-right`, `top-left`, `top-center` | `bottom-right` | Where the toast shows. Set on the Toaster for all its toasts, or on one toast() call. Corners swipe sideways to dismiss; the centers swipe toward their edge. Only on web. Not generated for iOS and Android. |
| `title` (required) | `node` |  |  | What happened, in a few words, past tense: Changes saved. |
| `description` | `node` |  |  | One more sentence when the title is not enough. |
| `duration` | `number` |  | `4000` | How long it stays, in ms. Make it longer for a toast with an action or a long description. Only on web. Not generated for iOS and Android. |
| `action` | `node` |  |  | One ToastAction, such as Undo. Its altText says where else to do it. |
| `dismissible` | `boolean` |  | `true` | The Dismiss button. Keep it, so a toast never sits over content the user needs. |
| `open` | `boolean` |  |  | Set on Toast shown with state. Whether it is shown. Pair it with onOpenChange. Only on web. Not generated for iOS and Android. |
| `onOpenChange` | `function` |  |  | Set on Toast shown with state. Called with false when the time is up, or on Dismiss, Escape or a swipe. Only on web. Not generated for iOS and Android. |
| `icon` | `node` |  |  | Native only. The status icon beside the title. The toaster supplies the one for the variant; a free-standing surface passes its own, or none for the default variant. Only on iOS, Android. |
| `dismissLabel` | `string` |  | `Dismiss` | Native only. The accessibility label of the Dismiss button. On the web it is labels.dismiss on the Toaster. Only on iOS, Android. |
| `onDismiss` | `function` |  |  | Native only. Called when the Dismiss button is pressed. The toaster closes the toast; a free-standing surface removes itself. Only on iOS, Android. |

### Slots

| Name | Required | Description |
| --- | --- | --- |
| `toaster` | yes | One Toaster, mounted once at the root of the app. position (bottom-right by default) picks the corner or edge center; a toast() call can pick another, which adds a stack there. stack (on by default) overlaps the toasts; maxVisible (3 by default) sets how many show at once. labels ({ region, dismiss }) gives the words screen readers hear in the page's language; they default to Notifications and Dismiss. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | Toast: the raised surface with a border, radius-container and shadow-med, px-lg py-md, gap-md between its parts. The icon, text and buttons share its horizontal center line; with a description the icon stays level with the title. |
| `viewport` | Toaster: the stack in the chosen corner or edge center, 384px wide at most, p-lg from the edges. Stacked, the toasts overlap, newest in front, and each older one is 5% smaller and peeks out by gap-sm, away from the edge; spread out, gap-sm sits between them. With stack={false} they sit in a column, gap-sm apart. |
| `icon` | The status icon for success, danger, warning and info: Tabler icons, 16px, in the status text color, level with the title. |
| `title` | What happened: 14px medium primary text. |
| `description` | Optional second line: 14px secondary text. |
| `action` | ToastAction: one small secondary Button, such as Undo. |
| `close` | The Dismiss button: a 24px icon button with a Tabler X, 16px, secondary. |
| `hitArea` | Transparent ::before on the Dismiss button, never smaller than size-target-min. |
| `content` | The text column beside the icon: title, description and action, gap-xs apart. |
| `closeIcon` | The X inside the Dismiss button: 16px (size-icon-md), in the secondary icon color. |

## Accessibility

Role: `status`

| Keys | Action |
| --- | --- |
| `F8` | Moves focus to the toasts and spreads the stack out, so a keyboard user can reach an action or dismiss them. |
| `Tab` | Moves through the action and the Dismiss button of each toast. |
| `Escape` | Closes the focused toast. |

- Every toast is announced politely, after what a screen reader is reading. It never takes focus.
- The timer pauses while the pointer is over a toast or focus is inside it, and while the window is in the background.
- Stacked, the toasts spread out while the pointer is over them or focus is inside, so every action and Dismiss button can be read and reached. The toasts past maxVisible are hidden, from screen readers and Tab too, until a newer one closes; each was announced when it arrived.
- A ToastAction needs altText: how to do the same without the toast (Undo from the History panel). A toast can close before anyone reaches it, so the action must also exist elsewhere.
- The status icon is decorative; the title says what happened, so the state never relies on color alone.
- It rises in from its edge only when the user allows motion, and fades in otherwise. A swipe dismisses it: sideways in a corner, toward the edge at the top or bottom center. It follows the finger while swiped; let go too early, it springs back. Under reduced motion it fades out. A stack slides and shrinks into place only when the user allows motion; under reduced motion it changes without moving.

## Install

```bash
npx werewolf-ui add toast
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.

```bash
npx werewolf-ui add toast --platform ios
npx werewolf-ui add toast --platform android
```

For iOS and Android, run `npx werewolf-ui init --platform ios` or `npx werewolf-ui init --platform android` once, then add with `--platform`. See [Installation](https://www.werewolf-ui.dev/docs/installation.md).
