# Toast

> A dismissible status notification with animated entry and exit, a paused timeout, and repeatable show events.

- Kind: ui · Category: Feedback · Interactivity: script
- Install: `npx shadcn@latest add @haze/toast`
- File: `src/components/haze/ui/toast.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/button, @haze/utils
- Docs page: https://hazeui.com/ui/toast

## Use when
- A completed action needs a brief confirmation without moving focus.
- A small screen needs a single notification with an optional inline action.

## Avoid when
- Several notices must stay available in a queue. Use notification-stack.

## Alternatives
- [notification-stack](https://hazeui.com/ui/notification-stack.md)
- [alert](https://hazeui.com/ui/alert.md)
- [undo-toast](https://hazeui.com/ui/undo-toast.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Root ID used by the show trigger. |
| message | `string` |  | no | Notification heading. |
| description | `string` |  | no | Supporting status text. |
| variant | `"info" \| "success" \| "warning" \| "destructive"` |  | no | Semantic icon and tone. |
| duration | `number` |  | no | Visible time in milliseconds. Defaults to 6000, zero disables automatic dismissal. |
| open | `boolean` |  | no | Whether the initial static notification is visible. |
| default slot | `slot` |  | no | Optional action content. |
| class | `string` |  | no | Additional classes on the component wrapper. |

## Accessibility
- New content is copied into a polite atomic live region. The timer pauses while hovered, focused or in a hidden tab.
- Dispatch haze:toast-show on the root with message and description, or target its id with a data-toast-trigger button. A zero duration stays open.
- Key Tab / Enter / Space: Reach the dismiss control and close the notification.

## Agent prompts

### Install and use

Install the Haze UI component "Toast" with `npx shadcn@latest add @haze/toast`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Toast from "@/components/haze/ui/toast.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/toast.md

### Customize with real content

Adapt the Haze toast ui for our product. Replace the demo values of these props with our real content: id, message, description, variant, duration, open, default slot, class. Keep the layout, the heading level rules and these accessibility details: New content is copied into a polite atomic live region. The timer pauses while hovered, focused or in a hidden tab. Dispatch haze:toast-show on the root with message and description, or target its id with a data-toast-trigger button. A zero duration stays open. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using toast, check it fits: use it when a completed action needs a brief confirmation without moving focus.; a small screen needs a single notification with an optional inline action.. Avoid it when several notices must stay available in a queue. use notification-stack.. Alternatives: @haze/notification-stack, @haze/alert, @haze/undo-toast.

## Example requests that should lead here
- Show a reusable saved toast that pauses its timeout when someone reads its action.

## Source

```astro
---
import { Check, CircleAlert, Info, X } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
import Button from "./button.astro";
interface Props {
  id: string;
  message?: string;
  description?: string;
  variant?: "info" | "success" | "warning" | "destructive";
  duration?: number;
  open?: boolean;
  class?: string;
}
const { id, message = "Changes saved", description = "Your review notes are up to date.", variant = "success", duration = 6000, open = true, class: className } = Astro.props;
const tones = { info: "text-brand bg-brand/14", success: "text-success bg-success/14", warning: "text-warning bg-warning/14", destructive: "text-destructive bg-destructive/14" };
const Icon = variant === "success" ? Check : variant === "info" ? Info : CircleAlert;
---
<div id={id} data-haze-toast data-duration={Math.max(0, duration)} data-open={open || undefined} class={cn("group/toast hidden min-w-0 max-w-full items-start gap-3 rounded-xl border border-border bg-popover py-3.5 pr-3.5 pl-4 text-popover-foreground opacity-0 shadow-float transition-[opacity,translate,scale,display] transition-discrete duration-200 ease-snappy data-open:flex data-open:translate-y-0 data-open:scale-100 data-open:opacity-100 starting:data-open:translate-y-2 starting:data-open:scale-[0.97] starting:data-open:opacity-0", className)}>
  <span class={cn("grid size-7 shrink-0 place-items-center rounded-full", tones[variant])}><Icon aria-hidden="true" class="size-4" /></span>
  <div class="grid min-w-0 flex-1 gap-1">
    <p data-toast-message class="text-[0.8125rem]/5 font-medium tracking-[-0.01em] wrap-anywhere">{message}</p>
    <p data-toast-description hidden={!description} class="text-xs/5 wrap-anywhere text-muted-foreground [&[hidden]]:hidden">{description}</p>
    <slot />
  </div>
  <Button data-toast-dismiss variant="ghost" size="icon" aria-label="Dismiss notification" class="hidden size-7 rounded-full text-muted-foreground group-data-[enhanced]/toast:inline-flex"><X aria-hidden="true" /></Button>
  <span data-toast-status role="status" aria-atomic="true" class="sr-only"></span>
</div>
<script>
  function enhance(root: HTMLElement) {
    if (root.hasAttribute("data-enhanced")) return;
    const message = root.querySelector<HTMLElement>("[data-toast-message]");
    const description = root.querySelector<HTMLElement>("[data-toast-description]");
    const status = root.querySelector<HTMLElement>("[data-toast-status]");
    const close = root.querySelector<HTMLButtonElement>("[data-toast-dismiss]");
    if (!message || !description || !status || !close) return;
    let timer = 0, start = 0, left = 0;
    const duration = Number(root.dataset.duration) || 0;
    const pause = () => { if (timer) { clearTimeout(timer); timer = 0; left = Math.max(0, left - (performance.now() - start)); } };
    const hide = () => {
      pause();
      if (root.contains(document.activeElement)) {
        const trigger = document.querySelector<HTMLElement>(`[data-toast-trigger="${CSS.escape(root.id)}"]`);
        trigger?.focus();
      }
      root.removeAttribute("data-open");
      root.dispatchEvent(new CustomEvent("haze:toast-dismiss", { bubbles: true }));
    };
    const resume = () => {
      if (!left || timer || !root.hasAttribute("data-open") || document.hidden || root.matches(":hover") || root.contains(document.activeElement)) return;
      start = performance.now();
      timer = window.setTimeout(hide, left);
    };
    const show = (event?: Event) => {
      pause();
      const detail = (event as CustomEvent<{ message?: string; description?: string }> | undefined)?.detail;
      if (typeof detail?.message === "string") message.textContent = detail.message;
      if (typeof detail?.description === "string") { description.textContent = detail.description; description.hidden = !detail.description; }
      root.setAttribute("data-open", "");
      status.textContent = `${message.textContent} ${description.textContent}`;
      left = duration;
      resume();
    };
    root.addEventListener("haze:toast-show", show);
    root.addEventListener("pointerenter", pause);
    root.addEventListener("pointerleave", resume);
    root.addEventListener("focusin", pause);
    root.addEventListener("focusout", () => queueMicrotask(resume));
    root.addEventListener("haze:toast-visibility", () => document.hidden ? pause() : resume());
    close.addEventListener("click", hide);
    root.setAttribute("data-enhanced", "");
    if (root.hasAttribute("data-open")) show();
  }
  const init = () => document.querySelectorAll<HTMLElement>("[data-haze-toast]").forEach(enhance);
  init();
  if (!document.documentElement.hasAttribute("data-haze-toast-listener")) {
    document.documentElement.setAttribute("data-haze-toast-listener", "");
    document.addEventListener("astro:page-load", init);
    document.addEventListener("visibilitychange", () => document.querySelectorAll<HTMLElement>("[data-haze-toast]").forEach(root => root.dispatchEvent(new Event("haze:toast-visibility"))));
    document.addEventListener("click", event => {
      const trigger = (event.target as Element).closest<HTMLElement>("[data-toast-trigger]");
      if (trigger) document.getElementById(trigger.dataset.toastTrigger ?? "")?.dispatchEvent(new CustomEvent("haze:toast-show"));
    });
  }
</script>
```
