Toast
A dismissible status notification with animated entry and exit, a paused timeout, and repeatable show events.
pnpm dlx shadcn@latest add @haze/toast---
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>When to use
- A completed action needs a brief confirmation without moving focus.
- A small screen needs a single notification with an optional inline action.
When not to use
- Several notices must stay available in a queue. Use notification-stack.
Installation
Add the Haze registry to components.json once, then install Toast with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/toastnpx shadcn@latest add @haze/toastyarn dlx shadcn@latest add @haze/toastbunx --bun shadcn@latest add @haze/toastFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Root ID used by the show trigger. |
| message | string | – | Notification heading. |
| description | string | – | Supporting status text. |
| variant | "info" | "success" | "warning" | "destructive" | – | Semantic icon and tone. |
| duration | number | – | Visible time in milliseconds. Defaults to 6000, zero disables automatic dismissal. |
| open | boolean | – | Whether the initial static notification is visible. |
| default slot | slot | – | Optional action content. |
| class | string | – | Additional classes on the component wrapper. |
Keyboard interactions
- TabEnterSpace
- Reach the dismiss control and close the notification.
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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- npm: @lucide/astro
- @haze/button
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.