# Toast

A brief message that appears in a corner and leaves by itself.

A brief message that arrives in a corner and leaves by itself: "Link copied", "Deploying…", "Deploy failed" with Retry. <Toast /> is the region (mount it once, near the root of the app); toast({ title, … }) raises one from anywhere. Each toast is a raised card (the selection toolbar's floating surface: surface, shadow.overlay) with the tone's icon (the alert's, in the status-pill tones), a title, a description, an optional action Button and a close Button. Hovering or focusing the region pauses every timer. Behaviour, timing, stacking and announcements come from the Zag toast machine through Ark UI.

Kind: component · Category: Feedback · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `toast` machine, through Ark UI

## Add

```bash
npx fndn add toast
```

It writes the folder `toast/` into the project's ui folder (`index.ts`, `toast.tsx`, `Toast.vue`, `Toast.svelte`, `toast.recipe.ts`), and what it is built from: [Button](/components/button.md), [Tooltip](/components/tooltip.md), [Spinner](/components/spinner.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Toast } from "@/components/ui/toast";
```

```vue
import { Toast } from "@/components/ui/toast";
```

```svelte
import { Toast } from "$lib/components/ui/toast";
```

## Parts

In the DOM the root carries `data-slot="toast"` and each part `data-slot="toast-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `toast` |  | The region (Ark's toaster group): role=region, aria-live=polite, labelled "Notifications" with its hotkey; fixed in its corner, 16px from the edges (and above the safe-area insets), min(356px, 100vw − 32px) wide. Toasts stack in it 8px apart, newest nearest the edge. |
| `item` | `div` | `toast-item` |  | One toast: role=status, aria-atomic; data-state=open\|closed, data-type. Absolutely placed by the engine through --y, --scale, --opacity and --z-index; a card: radius.card, padding 12px, gap 10px, surface, shadow.overlay. Focusable (tabIndex 0): Escape dismisses it. Carries kbd-instant. |
| `icon` | `span` | `toast-icon` | optional | The alert's tone icon (16px, bold, the tone's colour) on the title's first line; a Spinner (sm) while loading. None when neutral. aria-hidden. |
| `content` | `div` | `toast-content` |  | Takes the free space: title and description, wrapping (never cut). |
| `title` | `div` | `toast-title` |  | text.sm, weight 500, ink, line-height 20px. |
| `description` | `div` | `toast-description` | optional | text.sm, ink-2. |
| `action` | `button` | `toast-action` | optional | A Button (outline, sm) after the text, marked data-toast="action". Calls its handler, then dismisses the toast. |
| `close` | `button` | `toast-close` | optional | A Button (ghost, xs, square, rounded, muted, X) with a Tooltip, aria-label "Dismiss notification"; marked data-toast="close". On a coarse pointer its hit area reaches 40px. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `placement` | `"top-start" \| "top" \| "top-end" \| "bottom-start" \| "bottom" \| "bottom-end"` | `"bottom-end"` | The corner (or edge centre) the toasts arrive in. start and end follow the writing direction. |
| `max` | `number` | `3` | Toasts shown at once; the rest wait in a queue. |
| `duration` | `number` | `5000` | Default time on screen, in ms (each toast may set its own). Paused while the region is hovered or focused, or the page is hidden. |
| `overlap` | `boolean` | `false` | false: every toast shows in full, stacked. true: older toasts tuck behind the newest one and fan out while the region is hovered or focused. |
| `toasts` | `{ id?: string; title: string; description?: string; tone?: 'neutral' \| 'accent' \| 'green' \| 'orange' \| 'red'; loading: boolean? (a Spinner, and it stays until updated or dismissed); action: { label: string, value?: string, onClick?: () => void }? (one Button: onClick runs when it is pressed, an Undo; the action event also reports it, with value or the label); closable: boolean? (default true); duration: number? (ms; 0 = stays until dismissed) }[]` |  | Toasts raised when the region mounts (the same options toast() takes). Later ones come from toast(). |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `action` | (value: string, id: string) an action Button was pressed (its own action.onClick runs as well) |
| `dismiss` | (id: string) a toast left: timed out, closed, Escape, or toast.dismiss() |

## States

`open`, `paused`, `hover`, `press`, `focus-visible`

## Keyboard and accessibility

Role: region (status per toast).

Keyboard:

- Alt+T moves focus to the region (the engine's hotkey); Tab then walks the toasts and their Buttons
- Escape on a focused toast dismisses it
- Enter / Space on the action or the close Button press it

Notes:

- The region is aria-live=polite: a new toast is read once, title then description, without moving focus.
- Timers pause while the pointer is over the region or focus is inside it, so a toast never leaves while it is being read or reached.
- A toast never holds the only way to do something: its action repeats something reachable elsewhere (Undo, Retry, View).
- Leaving from the keyboard (Escape, the close Button pressed with a key) is instant.

## Motion

- arrive: slides in from its edge (--y from ±100% to its place) and fades in, over duration.slow with easing.out-strong; nothing plays for toasts present on first paint
- the others make room: they glide to their new offset on the same timing; after a dismissal they start as the exit ends (removeDelay = duration.base), with no empty slot in between
- leave: shorter, over duration.base with easing.out-strong: it slides back towards its edge and fades
- keyboard: leaving after Escape or a key press is instant (kbd-instant)
- reduced motion: no travel: a toast fades in place (opacity only, duration.slow in, duration.base out); position and stacking still apply
- overlap: the toasts behind the newest show only their edge, their content hidden (opacity 0) so no text shows past the front toast; it fades back in when the stack expands (hover, focus), with the same timing

## Tokens

`color.surface`, `color.ink`, `color.ink-2`, `color.accent`, `color.green`, `color.orange`, `color.red`, `shadow.overlay`, `radius.card`, `text.sm`, `duration.base`, `duration.slow`, `easing.out-strong`

## Dependencies

- [Button](/components/button.md)
- [Tooltip](/components/tooltip.md)
- [Spinner](/components/spinner.md)

## Examples

### Changes saved

React · `save-settings.tsx`

```tsx
import { Button } from "@/components/ui/button";
import { Toast, toast } from "@/components/ui/toast";

export default function SaveSettings() {
  const save = () => {
    toast({ title: "Changes saved", description: "web-app now builds with pnpm build." });
  };
  return (
    <>
      <Button onClick={save}>Save changes</Button>
      {/* mount the region once, near the root of the app */}
      <Toast />
    </>
  );
}
```

Vue · `save-settings.vue`

```vue
<script setup lang="ts">
import { Button } from "@/components/ui/button";
import { Toast, toast } from "@/components/ui/toast";

function save() {
  toast({ title: "Changes saved", description: "web-app now builds with pnpm build." });
}
</script>

<template>
  <Button @click="save">Save changes</Button>
  <!-- mount the region once, near the root of the app -->
  <Toast />
</template>
```

Svelte · `save-settings.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
  import { Toast, toast } from "$lib/components/ui/toast";

  const save = () => {
    toast({ title: "Changes saved", description: "web-app now builds with pnpm build." });
  };
</script>

<Button onclick={save}>Save changes</Button>
<!-- mount the region once, near the root of the app -->
<Toast />
```

### Deploy started, then done

React · `deploy-progress.tsx`

```tsx
import { Button } from "@/components/ui/button";
import { Toast, toast } from "@/components/ui/toast";

export default function DeployProgress() {
  const deploy = () => {
    const id = toast({ title: "Deploying web-app to Production…", loading: true, closable: false });
    setTimeout(() => toast.update(id, { title: "Deployed to Production", description: "dpl_7f3a9c is live at app.northwind.example.", tone: "green", loading: false, duration: 4000, closable: true }), 2000);
  };
  return (
    <>
      <Button variant="primary" onClick={deploy}>Deploy to Production</Button>
      {/* mount the region once, near the root of the app */}
      <Toast />
    </>
  );
}
```

Vue · `deploy-progress.vue`

```vue
<script setup lang="ts">
import { Button } from "@/components/ui/button";
import { Toast, toast } from "@/components/ui/toast";

function deploy() {
  const id = toast({ title: "Deploying web-app to Production…", loading: true, closable: false });
  setTimeout(() => toast.update(id, { title: "Deployed to Production", description: "dpl_7f3a9c is live at app.northwind.example.", tone: "green", loading: false, duration: 4000, closable: true }), 2000);
}
</script>

<template>
  <Button variant="primary" @click="deploy">Deploy to Production</Button>
  <!-- mount the region once, near the root of the app -->
  <Toast />
</template>
```

Svelte · `deploy-progress.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
  import { Toast, toast } from "$lib/components/ui/toast";

  const deploy = () => {
    const id = toast({ title: "Deploying web-app to Production…", loading: true, closable: false });
    setTimeout(() => toast.update(id, { title: "Deployed to Production", description: "dpl_7f3a9c is live at app.northwind.example.", tone: "green", loading: false, duration: 4000, closable: true }), 2000);
  };
</script>

<Button variant="primary" onclick={deploy}>Deploy to Production</Button>
<!-- mount the region once, near the root of the app -->
<Toast />
```

### Deploy failed, with Retry

React · `deploy-failed.tsx`

```tsx
import { Button } from "@/components/ui/button";
import { Toast, toast } from "@/components/ui/toast";

export default function DeployFailed() {
  const retry = () => {
    const id = toast({ title: "Redeploying billing-api to Staging…", loading: true, closable: false });
    setTimeout(() => toast.update(id, { title: "Deployed billing-api to Staging", tone: "green", loading: false, duration: 4000, closable: true }), 2000);
  };
  const deploy = () => {
    toast({ title: "Deploy to Staging failed", description: "The build of billing-api exited with code 1.", tone: "red", duration: 0, action: { label: "Retry", onClick: retry } });
  };
  return (
    <>
      <Button onClick={deploy}>Deploy to Staging</Button>
      {/* mount the region once, near the root of the app */}
      <Toast />
    </>
  );
}
```

Vue · `deploy-failed.vue`

```vue
<script setup lang="ts">
import { Button } from "@/components/ui/button";
import { Toast, toast } from "@/components/ui/toast";

function retry() {
  const id = toast({ title: "Redeploying billing-api to Staging…", loading: true, closable: false });
  setTimeout(() => toast.update(id, { title: "Deployed billing-api to Staging", tone: "green", loading: false, duration: 4000, closable: true }), 2000);
}
function deploy() {
  toast({ title: "Deploy to Staging failed", description: "The build of billing-api exited with code 1.", tone: "red", duration: 0, action: { label: "Retry", onClick: retry } });
}
</script>

<template>
  <Button @click="deploy">Deploy to Staging</Button>
  <!-- mount the region once, near the root of the app -->
  <Toast />
</template>
```

Svelte · `deploy-failed.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
  import { Toast, toast } from "$lib/components/ui/toast";

  const retry = () => {
    const id = toast({ title: "Redeploying billing-api to Staging…", loading: true, closable: false });
    setTimeout(() => toast.update(id, { title: "Deployed billing-api to Staging", tone: "green", loading: false, duration: 4000, closable: true }), 2000);
  };
  const deploy = () => {
    toast({ title: "Deploy to Staging failed", description: "The build of billing-api exited with code 1.", tone: "red", duration: 0, action: { label: "Retry", onClick: retry } });
  };
</script>

<Button onclick={deploy}>Deploy to Staging</Button>
<!-- mount the region once, near the root of the app -->
<Toast />
```

### Link copied

React · `copy-invite-link.tsx`

```tsx
import { Link } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";
import { Toast, toast } from "@/components/ui/toast";

export default function CopyInviteLink() {
  const copy = () => {
    void navigator.clipboard.writeText("https://northwind.example/join/8f2k1q");
    toast({ title: "Invite link copied", description: "Anyone with the link can join Northwind until Nov 24.", tone: "green" });
  };
  return (
    <>
      <Button icon={<Link data-slot="icon" weight="bold" />} onClick={copy}>Copy invite link</Button>
      {/* mount the region once, near the root of the app */}
      <Toast />
    </>
  );
}
```

Vue · `copy-invite-link.vue`

```vue
<script setup lang="ts">
import { PhLink } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { Toast, toast } from "@/components/ui/toast";

function copy() {
  void navigator.clipboard.writeText("https://northwind.example/join/8f2k1q");
  toast({ title: "Invite link copied", description: "Anyone with the link can join Northwind until Nov 24.", tone: "green" });
}
</script>

<template>
  <Button @click="copy"><template #icon><PhLink data-slot="icon" weight="bold" /></template>Copy invite link</Button>
  <!-- mount the region once, near the root of the app -->
  <Toast />
</template>
```

Svelte · `copy-invite-link.svelte`

```svelte
<script lang="ts">
  import Link from "phosphor-svelte/lib/LinkIcon";
  import { Button } from "$lib/components/ui/button";
  import { Toast, toast } from "$lib/components/ui/toast";

  const copy = () => {
    void navigator.clipboard.writeText("https://northwind.example/join/8f2k1q");
    toast({ title: "Invite link copied", description: "Anyone with the link can join Northwind until Nov 24.", tone: "green" });
  };
</script>

<Button onclick={copy}>{#snippet icon()}<Link data-slot="icon" weight="bold" />{/snippet}Copy invite link</Button>
<!-- mount the region once, near the root of the app -->
<Toast />
```

### Undo a removal

React · `remove-member.tsx`

```tsx
import { Button } from "@/components/ui/button";
import { Toast, toast } from "@/components/ui/toast";

export default function RemoveMember() {
  const remove = () => {
    toast({ title: "Lucas Moreau was removed from Northwind", action: { label: "Undo", onClick: () => toast({ title: "Lucas Moreau is back in Northwind", tone: "green" }) } });
  };
  return (
    <>
      <Button variant="danger" onClick={remove}>Remove Lucas Moreau</Button>
      {/* mount the region once, near the root of the app */}
      <Toast />
    </>
  );
}
```

Vue · `remove-member.vue`

```vue
<script setup lang="ts">
import { Button } from "@/components/ui/button";
import { Toast, toast } from "@/components/ui/toast";

function remove() {
  toast({ title: "Lucas Moreau was removed from Northwind", action: { label: "Undo", onClick: () => toast({ title: "Lucas Moreau is back in Northwind", tone: "green" }) } });
}
</script>

<template>
  <Button variant="danger" @click="remove">Remove Lucas Moreau</Button>
  <!-- mount the region once, near the root of the app -->
  <Toast />
</template>
```

Svelte · `remove-member.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
  import { Toast, toast } from "$lib/components/ui/toast";

  const remove = () => {
    toast({ title: "Lucas Moreau was removed from Northwind", action: { label: "Undo", onClick: () => toast({ title: "Lucas Moreau is back in Northwind", tone: "green" }) } });
  };
</script>

<Button variant="danger" onclick={remove}>Remove Lucas Moreau</Button>
<!-- mount the region once, near the root of the app -->
<Toast />
```

## Recipe

`toast.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* toast — the region (Ark's toaster group) and its toasts. A toast is a raised card: the floating
 * surface of the selection toolbar (surface, shadow.overlay) at the card radius, with the alert's
 * tone icon on the title's first line, then the text, an optional action and the close Button.
 *
 * Placement and stacking are the Zag toast machine's: the group is fixed in its corner (inline
 * styles), each toast is absolutely placed in it through custom properties the engine writes and
 * updates: --y (its offset, ±100% before it arrives and after it leaves), --opacity, --index,
 * --z-index and --height. The recipe turns them into `translate`, `opacity` and `scale`, and
 * transitions those: in over duration.slow, out over duration.base (shorter), both out-strong.
 * Toasts that exist on first paint do not slide in: the adapter sets `ready` after the first
 * paint, and until then nothing transitions. A dismissal from the keyboard is instant
 * (kbd-instant). overlap: the older toasts tuck behind the newest, 5% smaller per step, their
 * content hidden until the stack expands. Reduced motion: opacity only, no travel. */

const ICON = "inline-flex shrink-0 items-center justify-center h-5 text-[16px] [&>[data-slot=icon]]:size-4 [&>svg]:size-4";

export const toast = tv({
  slots: {
    /* mx-auto centres it at top and bottom, where the engine sets both insets */
    /* the engine resolves start / end to a physical side, then writes it as a logical inset on an
     * element that carries the page's dir, which flips it a second time under rtl: the region is
     * laid out ltr so the inset lands where the engine meant, and each toast takes the page's
     * direction back (rtl: matches the dir attribute on the region) */
    base: "w-[min(356px,calc(100vw-32px))] mx-auto outline-none [direction:ltr]",
    item: [
      "kbd-instant",
      "absolute inset-x-0 flex items-start gap-2.5 p-3 rounded-card bg-surface text-ink shadow-overlay",
      "text-sm leading-5 outline-offset-2 rtl:[direction:rtl]",
      "[translate:var(--x,0)_var(--y,0)] [opacity:var(--opacity,1)] [z-index:var(--z-index)] h-(--height)",
      "[scale:1] data-overlap:data-sibling:[scale:calc(1-var(--index,0)*0.05)]",
      "motion-also-[translate_var(--fndn-duration-slow)_var(--fndn-easing-out-strong),scale_var(--fndn-duration-slow)_var(--fndn-easing-out-strong),opacity_var(--fndn-duration-slow)_var(--fndn-easing-out-strong)]",
      /* it leaves faster than it came */
      "data-[state=closed]:motion-also-[translate_var(--fndn-duration-base)_var(--fndn-easing-out-strong),scale_var(--fndn-duration-base)_var(--fndn-easing-out-strong),opacity_var(--fndn-duration-base)_var(--fndn-easing-out-strong)]",
      /* reduced motion: no travel, the toasts fade in place (position and stacking still apply) */
      "motion-reduce:motion-also-[opacity_var(--fndn-duration-slow)_var(--fndn-easing-out-strong)]",
      "motion-reduce:data-[state=closed]:motion-also-[opacity_var(--fndn-duration-base)_var(--fndn-easing-out-strong)]",
      /* collapsed behind the newest, older toasts show only their edge: their content is hidden
       * (Sonner's stack) so no text peeks past the front toast; it fades back in when the stack
       * expands (hover, focus), with the toast's own timing */
      "data-overlap:data-sibling:overflow-hidden",
      "[&>*]:opacity-100 data-overlap:data-sibling:[&>*]:opacity-0 [&>*]:motion-opacity-slow-out [&>*]:kbd-instant",
    ],
    icon: ICON,
    content: "flex flex-col flex-1 min-w-0",
    title: "font-medium text-ink wrap-anywhere [unicode-bidi:plaintext]",
    description: "text-ink-2 wrap-anywhere [unicode-bidi:plaintext]",
    /* a 28px Button on the 20px title line */
    action: "shrink-0 -my-1",
    close: [
      "shrink-0 -my-0.5 -me-1",
      "pointer-coarse:relative pointer-coarse:after:absolute pointer-coarse:after:-inset-2 pointer-coarse:after:content-['']",
    ],
  },
  variants: {
    tone: {
      neutral: {},
      accent: { icon: "text-accent" },
      green: { icon: "text-green" },
      orange: { icon: "text-orange" },
      red: { icon: "text-red" },
    },
    /* false until the region has been painted once: toasts present at mount do not slide in */
    ready: { false: { item: "transition-none! [&>*]:transition-none!" }, true: {} },
  },
  defaultVariants: { tone: "neutral", ready: true },
});

export type ToastRecipeProps = VariantProps<typeof toast>;
```
