Overlays & Dialogs
@hacline/ui/alert-dialogAlertDialog
Collect user confirmation before executing a critical or irreversible action.
Usage
"Keep draft" and "Confirm clear" each update the demo state. A close button's value is passed as returnValue to the open state callback.
"use client";
import { useState } from "react";
import {
AlertDialog,
AlertDialogBackdrop,
AlertDialogClose,
AlertDialogContent,
AlertDialogDescription,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/hacline/components/alert-dialog";
export const Example = () => {
const [status, setStatus] = useState("Draft saved");
return (
<div className="flex flex-col items-center gap-3">
<AlertDialog>
<AlertDialogTrigger>Discard draft</AlertDialogTrigger>
<AlertDialogBackdrop />
<AlertDialogContent>
<AlertDialogTitle>Clear current draft?</AlertDialogTitle>
<AlertDialogDescription>
The draft in this example will be cleared.
</AlertDialogDescription>
<div className="flex justify-end gap-2">
<AlertDialogClose
action={() => setStatus("Draft kept")}
value="cancel"
>
Keep draft
</AlertDialogClose>
<AlertDialogClose
action={() => setStatus("Draft cleared")}
data-agent-confirm="Clear sample draft"
data-variant="danger"
value="confirm"
>
Confirm clear
</AlertDialogClose>
</div>
</AlertDialogContent>
</AlertDialog>
<output className="text-hl-fg-muted text-sm">{status}</output>
</div>
);
};Async Confirmation
Return a Promise from the confirm button's action. The button displays a pending state while archiving, and the dialog closes once the operation completes.
"use client";
import { useState } from "react";
import {
AlertDialog,
AlertDialogBackdrop,
AlertDialogClose,
AlertDialogContent,
AlertDialogDescription,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/hacline/components/alert-dialog";
const ARCHIVE_DELAY = 700;
export const Example = () => {
const [archived, setArchived] = useState(false);
return (
<AlertDialog pendingAnnouncement="Archiving record">
<AlertDialogTrigger disabled={archived}>
{archived ? "Archived" : "Archive sample record"}
</AlertDialogTrigger>
<AlertDialogBackdrop />
<AlertDialogContent>
<AlertDialogTitle>Archive this record?</AlertDialogTitle>
<AlertDialogDescription>
You can still find it in history after archiving.
</AlertDialogDescription>
<div className="flex justify-end gap-2">
<AlertDialogClose>Not now</AlertDialogClose>
<AlertDialogClose
action={async () => {
// oxlint-disable-next-line promise/avoid-new -- Browser timers use a callback.
await new Promise<void>((resolve) => {
setTimeout(resolve, ARCHIVE_DELAY);
});
setArchived(true);
}}
value="confirm"
>
{({ pending }) => (pending ? "Archiving…" : "Confirm archive")}
</AlertDialogClose>
</div>
</AlertDialogContent>
</AlertDialog>
);
};Parts
| Part | Description | Extended Props |
|---|---|---|
AlertDialogBackdrop | A dim behind the alert dialog. Pressing it does nothing. | variant |
AlertDialogClose | A button that closes the alert dialog, like Cancel or Delete. An action runs first, and a Promise it returns keeps the alert dialog open until it settles. | — |
AlertDialogContent | The alert dialog itself, with the alertdialog role, named by its title. Put the title, the description and the buttons that answer it inside. | size |
AlertDialogDescription | Describes the alert dialog while it's rendered. A paragraph by default. | — |
AlertDialogShortcut | Shows a trigger's shortcut inside the trigger. | — |
AlertDialogTitle | The alert dialog's title, which names it. A level 2 heading by default. | — |
AlertDialogTrigger | The button that opens the alert dialog. A trigger outside the alert dialog connects through controller. | — |
Props
Prop
Type