Confirm action
An alert dialog's confirm button that shows a loader and stays focusable while its action runs.
Use AlertDialogPendingAction in place of AlertDialogAction when confirming starts a request. It does not close the dialog: close it when the request succeeds. variant is destructive unless you set default, which is the primary button.
Delete that asks first
Drafts deleted: 0
"use client"
import * as React from "react"
import {
AlertDialog,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"
import { AlertDialogPendingAction } from "@/components/ui/confirm-action"
// Stands in for a request.
const request = () => new Promise((resolve) => setTimeout(resolve, 1500))
export default function ConfirmActionDeleteThatAsksFirst() {
const [open, setOpen] = React.useState(false)
const [pending, setPending] = React.useState(false)
const [deleted, setDeleted] = React.useState(0)
async function deleteDraft() {
setPending(true)
await request()
setDeleted((count) => count + 1)
setPending(false)
setOpen(false)
}
return (
<div className="flex flex-col items-center gap-3">
{/* Stays open until the request ends, so its outcome is seen. */}
<AlertDialog open={open} onOpenChange={(next) => !pending && setOpen(next)}>
<AlertDialogTrigger render={<Button variant="destructive" />}>Delete a draft</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete this draft?</AlertDialogTitle>
<AlertDialogDescription>
The draft and its comments are removed for everyone. This cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={pending}>Cancel</AlertDialogCancel>
<AlertDialogPendingAction pending={pending} onClick={deleteDraft}>
{pending ? "Deleting" : "Delete draft"}
</AlertDialogPendingAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<p role="status" className="text-sm text-muted-foreground tabular-nums">
Drafts deleted: {deleted}
</p>
</div>
)
}
Not destructive
Reminders sent: 0
"use client"
import * as React from "react"
import {
AlertDialog,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"
import { AlertDialogPendingAction } from "@/components/ui/confirm-action"
// Stands in for a request.
const request = () => new Promise((resolve) => setTimeout(resolve, 1500))
export default function ConfirmActionNotDestructive() {
const [open, setOpen] = React.useState(false)
const [pending, setPending] = React.useState(false)
const [sent, setSent] = React.useState(0)
async function sendReminders() {
setPending(true)
await request()
setSent((count) => count + 4)
setPending(false)
setOpen(false)
}
return (
<div className="flex flex-col items-center gap-3">
{/* Stays open until the request ends, so its outcome is seen. */}
<AlertDialog open={open} onOpenChange={(next) => !pending && setOpen(next)}>
<AlertDialogTrigger render={<Button variant="outline" />}>Send reminders</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Remind 4 reviewers?</AlertDialogTitle>
<AlertDialogDescription>
Everyone who has not reviewed release 2.4 gets an email now.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={pending}>Cancel</AlertDialogCancel>
<AlertDialogPendingAction variant="default" pending={pending} onClick={sendReminders}>
{pending ? "Sending" : "Send reminders"}
</AlertDialogPendingAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<p role="status" className="text-sm text-muted-foreground tabular-nums">
Reminders sent: {sent}
</p>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/confirm-actionbunx --bun shadcn@latest add @khaeli/confirm-actionInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
Keyboard
EnterSpace- On the trigger, opens the dialog and moves focus to Cancel. On the confirm button, starts the action. While
pendingthe confirm button keeps focus asaria-disabledand further presses do nothing. TabShift+Tab- Move between Cancel and the confirm button. Focus stays inside the dialog.
Escape- Closes the dialog and returns focus to the trigger. A request that is already running is not cancelled: keep the dialog open while
pending, as these examples do.
Accessible name
- The confirm button's name is its text. Name the action, not the answer (Delete draft, not Yes), and change it while
pendingto say what is happening, such as Deleting. The loader is hidden from assistive technology. - The dialog takes its name from
AlertDialogTitleand its description fromAlertDialogDescription.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/confirm-action.json