Overlays & Dialogs
@hacline/ui/toastToast
Present temporary dismissible notifications with optional actions.
Usage
Trigger multiple notifications to preview stacked cards, hover or focus the region to expand the stack, or click an action like Undo. Semantic helpers (success, warning, error, info) style automatically via data-type.
"use client";
import { useMemo, useRef, useState } from "react";
import { Button } from "@/components/ui/hacline/components/button";
import {
createToaster,
ToastRegion,
} from "@/components/ui/hacline/components/toast";
const SAMPLE_EVENTS = [
"Friday, June 12 at 10:00 AM",
"Monday, June 15 at 2:30 PM",
"Wednesday, June 17 at 4:00 PM",
] as const;
export const Example = () => {
const notify = useMemo(() => createToaster(), []);
const counterRef = useRef(0);
const [lastAction, setLastAction] = useState(
"Click repeatedly to stack toasts"
);
const triggerEventToast = () => {
const index = counterRef.current;
counterRef.current += 1;
const schedule = SAMPLE_EVENTS[index % SAMPLE_EVENTS.length];
const eventNumber = index + 1;
setLastAction(`Created event #${eventNumber}`);
notify("Event created", {
action: {
label: "Undo",
onAction: () => {
setLastAction(`Undid event #${eventNumber}`);
},
},
description: schedule,
});
};
return (
<div className="grid justify-items-center gap-3">
<div className="flex flex-wrap items-center justify-center gap-2">
<Button onClick={triggerEventToast} variant="primary">
Create event
</Button>
<Button
onClick={() => {
notify.success("Changes saved", {
description: "Your workspace settings are up to date.",
});
}}
variant="outline"
>
Success
</Button>
<Button
onClick={() => {
notify.warning("Storage almost full", {
description: "90% of your workspace quota is used.",
});
}}
variant="outline"
>
Warning
</Button>
<Button
onClick={() => {
notify.error("Sync failed", {
action: {
label: "Retry",
onAction: () => {
setLastAction("Retried sync");
},
},
description: "Check your connection and try again.",
});
}}
variant="outline"
>
Error
</Button>
<Button
onClick={() => {
notify.info("New version available", {
description: "Refresh to load the latest updates.",
});
}}
variant="outline"
>
Info
</Button>
</div>
<output className="text-hl-fg-muted text-xs">{lastAction}</output>
<ToastRegion aria-label="Notifications" toaster={notify} />
</div>
);
};Promise-Driven Async Status
Use notify.promise to show a pending loading toast first and automatically update the same notification upon completion.
"use client";
import { useMemo } from "react";
import { Button } from "@/components/ui/hacline/components/button";
import {
createToaster,
ToastRegion,
} from "@/components/ui/hacline/components/toast";
const EXPORT_DELAY = 900;
export const Example = () => {
const notify = useMemo(() => createToaster(), []);
return (
<>
<Button
onClick={async () => {
// oxlint-disable-next-line promise/avoid-new -- Browser timers use a callback.
const job = new Promise<void>((resolve) => {
setTimeout(resolve, EXPORT_DELAY);
});
notify.promise(job, {
error: "Export failed",
loading: "Creating archive…",
success: "Project archive ready",
});
await job;
}}
variant="outline"
>
Create project archive
</Button>
<ToastRegion
aria-label="Project archive notifications"
toaster={notify}
/>
</>
);
};Parts
| Part | Description | Extended Props |
|---|---|---|
Toast | One toast. Swipe it toward the region's side, or press Escape on it, to dismiss it. | variant |
ToastAction | The toast's action, like Undo. Renders the toast's action label when it has no children, as a link when the action has an href. A Promise from its handler keeps the toast open and the button pending. | — |
ToastClose | Closes the toast. With an action, runs it first, and a returned Promise keeps the toast open until it settles. | — |
ToastDescription | The toast's description. Renders the toast's description when it has no children, and nothing when it has neither. | — |
ToastTitle | The toast's title. Renders the toast's title when it has no children, and nothing when it has neither. | — |
Props
Prop
Type