React Alert Dialog
A glass React alert dialog that asks before destructive actions, focuses the safe choice first and stays open with the error if the action fails.
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/alert-dialog.jsonAurora redesign
42 files, 6 collaborators.
AlertDialogAlertDialogIconAlertDialogCloseAlertDialogTitleAlertDialogAction- +14 more
- +11 more
Destructive confirmation
onConfirm returns a promise: the action shows a spinner, Cancel and Escape wait, and the dialog closes when it resolves. Turn on the failure to see the inline error.
import { ConfirmDialog } from "@/components/sheenui/alert-dialog/alert-dialog";
import { Button } from "@/components/sheenui/button/button";
import { useToast } from "@/components/sheenui/toast/toast";
// Package build (after the first npm release, or a packed tarball): import { Button, ConfirmDialog, useToast } from "@sheenui/theme";
// This example also uses toast: npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/toast.json
// Your API client.
declare const api: { deleteProject(id: string): Promise<void> };
export function Example() {
const toast = useToast();
return (
<ConfirmDialog
trigger={<Button variant="danger">Delete project</Button>}
title="Delete “Aurora”?"
description="The project, its 24 deployments and every environment variable are removed for the whole team. This can’t be undone."
confirmLabel="Delete project"
labels={{ pending: "Deleting…" }}
onConfirm={async () => {
await api.deleteProject("aurora"); // throw to keep the dialog open with the message
toast.success("Project deleted");
}}
/>
);
}Files to copy
8 folders · 14 files · 4 packages
- From
theme/src/ components/alert-dialog/components/button/components/input/components/spinner/hooks/{use-controllable.ts, use-latest.ts, use-magnetic.ts, use-media-query.ts, use-preferences.ts}motion/{gate.ts, tokens.ts}lib/{cn.ts, field-size.ts, form.ts, modal-tab-trap.ts, popup-motion.ts, state-light.tsx, warn-once.ts}styles/
- shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/alert-dialog.jsonRegistry item: alert-dialog. The styles need the Tailwind v4 glass UI setup.- The examples also use
components/toast/hooks/use-direction.ts
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/toast.json- Packages
pnpm add @base-ui/react clsx motion tailwind-merge
Type to confirm
For the irreversible: the action stays disabled until the name is typed exactly. Enter submits.
import { ConfirmDialog } from "@/components/sheenui/alert-dialog/alert-dialog";
import { Button } from "@/components/sheenui/button/button";
// Package build (after the first npm release, or a packed tarball): import { Button, ConfirmDialog } from "@sheenui/theme";
// Your API client.
declare const api: { deleteRepo(name: string): Promise<void> };
export function Example() {
return (
<ConfirmDialog
trigger={<Button variant="glass">Delete repository</Button>}
title="Delete sheenui/theme?"
description="All issues, pull requests and 1,284 stars go with it. Forks are not deleted."
confirmationText="sheenui/theme"
confirmLabel="Delete this repository"
onConfirm={() => api.deleteRepo("sheenui/theme")}
/>
);
}useConfirm()
Ask from any event handler and await the answer: true once the action has finished, false if dismissed. One dialog serves the whole list. After a key is revoked, focus moves to the next row's Revoke button (or to Restore once the list is empty), never to the page; pass finalFocus to choose another target.
- Production deploysk_live_…4f2a · 2 min ago
- Stagingsk_test_…9c1e · Yesterday
- CI pipelinesk_test_…07bd · 3 days ago
- Local dev (Mira)sk_test_…e6a0 · Last month
import { useState } from "react";
import { useConfirm } from "@/components/sheenui/alert-dialog/alert-dialog";
// Package build (after the first npm release, or a packed tarball): import { useConfirm } from "@sheenui/theme";
type ApiKey = { id: string; name: string; hint: string };
// Your API client.
declare const api: { revoke(id: string): Promise<void> };
export function Example() {
const [confirm, confirmDialog] = useConfirm();
const [keys, setKeys] = useState<ApiKey[]>([{ id: "k1", name: "CI deploy", hint: "sk_live_…a41f" }]);
async function revoke(key: ApiKey) {
const ok = await confirm({
tone: "warning",
title: `Revoke “${key.name}”?`,
description: `Apps using ${key.hint} stop working immediately.`,
confirmLabel: "Revoke key",
onConfirm: () => api.revoke(key.id),
// Optional: focus goes to the same spot in the list by default once the row is removed.
// finalFocus: newKeyButtonRef,
});
if (ok) setKeys((list) => list.filter((k) => k.id !== key.id));
}
return (
<>
{keys.map((key) => (
<button key={key.id} type="button" onClick={() => revoke(key)}>
Revoke {key.name}
</button>
))}
{confirmDialog}
</>
);
}Composed from parts
Build your own layout from the pieces. tone sets the icon and the action's color (danger → danger button).
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from "@/components/sheenui/alert-dialog/parts/icons-content";
import { Button } from "@/components/sheenui/button/button";
// Package build (after the first npm release, or a packed tarball): import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, Button } from "@sheenui/theme";
const discard = () => console.log("discarded");
export function Example() {
return (
<AlertDialog>
<AlertDialogTrigger render={<Button variant="glass" />}>Close editor</AlertDialogTrigger>
<AlertDialogContent tone="warning">
<AlertDialogHeader>
<AlertDialogTitle>Discard unsaved changes?</AlertDialogTitle>
<AlertDialogDescription>You edited 3 fields in “Billing details”. Leaving now loses those edits.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Keep editing</AlertDialogCancel>
<AlertDialogAction onClick={discard}>Discard changes</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}ConfirmDialog
ConfirmDialogLabels keys and English defaults (5)
Parts and hook
AlertDialogAction props
AlertDialogCancel props
AlertDialogContent props
AlertDialogIcon props
Keyboard
Accessibility
- role="alertdialog" named by the title and described by the description; the page behind is inert.
- Initial focus lands on Cancel for danger and warning tones (the safe choice), on the action for info, or on the type-to-confirm field.
- While pending, aria-busy and a polite status announce it; errors use role="alert". The tone icon is decorative, so the title carries the meaning.
- If the trigger was removed (a deleted row), focus lands on the same spot in the list.
All exports on this page
AlertDialog— Root of an alert dialog: holds the open state and wires the trigger, popup and buttons together.AlertDialogAction— The confirming action.AlertDialogCancel— Closes the dialog without doing anything.AlertDialogClose— Any element inside the dialog that closes it when pressed (Base UI AlertDialog.Close).AlertDialogContent— The dialog panel: portal, dimmed scrim and a glass popup.AlertDialogDescription— Supporting text under the title (Base UI AlertDialog.Description).AlertDialogFooter— Buttons row: stacked full-width on phones (action on top, in thumb reach), a right-aligned row from sm.AlertDialogHeader— Icon + title + description.AlertDialogIcon— The tone icon in a soft tinted disc.AlertDialogTitle— The dialog's name (Base UI AlertDialog.Title, an <h2>).AlertDialogTrigger— The button that opens an AlertDialog (Base UI AlertDialog.Trigger).ConfirmDialog— A complete confirmation dialog in one component: tone icon, title, description, Cancel and the action, a loading state while an async onConfirm runs.createAlertDialogHandle— Connects an AlertDialog to triggers rendered elsewhere (e.g.defaultAlertLabels— The English AlertLabels used when labels is omitted; a partial labels prop is merged over it.defaultConfirmDialogLabels— The English strings of ConfirmDialog (Confirm, Cancel, Working…, a generic error, the type-to-confirm prompt).useConfirm— Ask from any event handler and await the answer: const [confirm, confirmDialog] = useConfirm().
When to use the alert dialog
- Use it as a delete confirmation modal before deleting a project, revoking an API key or discarding unsaved changes, where the person must choose because an outside click will not dismiss it, only nudge the panel.
- Use ConfirmDialog when you want the whole thing prebuilt: icon, title, description, Cancel and action, a loading state while an async onConfirm runs, and inline errors if it throws.
- Use useConfirm in an event handler when awaiting the answer reads best, as in if (await confirm({ title: "Delete?" })), or add type-to-confirm for actions as serious as deleting an organization.
- Skip it when the content is informational or a form that people may dismiss freely; Dialog closes on an outside press, and Toast fits a quiet notice better than a blocking prompt.
- Pair it with Button for the trigger through the render prop, Toast to confirm that the action happened, and DataTable rows, where ConfirmDialog and useConfirm move focus to the matching control in the neighboring row after a delete.
Frequently asked questions
Related pages
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .