Alert Dialog
A wrapper for Base UI Alert Dialog with flexible trigger composition, async loading state, media slot, and detached trigger support.
Installation
npx shadcn@latest add @glrk-ui/alert-dialogIf you haven't set up the prerequisites yet, check out registry setup.
Usage
Basic
import { AlertDialogWrapper } from "@/components/ui/alert-dialog"
export function Basic() {
return (
<AlertDialogWrapper
trigger="Delete"
triggerCls={buttonVariants({ variant: 'outline' })}
/>
)
}Icon trigger
Pass any ReactNode as trigger — icon only, icon + text, or any element:
<AlertDialogWrapper
trigger={<><Trash2 /> Delete</>}
triggerCls={buttonVariants({ variant: 'destructive' })}
action="Delete"
/>Custom element trigger
Use triggerProps.render to replace the default <button> with any element. Set nativeButton={false} for non-button elements:
// Anchor link trigger
<AlertDialogWrapper
trigger="Delete file"
triggerProps={{
render: <a href="#" />,
nativeButton: false,
}}
triggerCls="text-sm text-destructive underline underline-offset-4"
/>
// Custom forwardRef component trigger
const MyTrigger = React.forwardRef<HTMLDivElement, React.ComponentProps<'div'>>(
({ children, ...props }, ref) => <div ref={ref} {...props}>{children}</div>
)
<AlertDialogWrapper
trigger="Open"
triggerProps={{ render: <MyTrigger />, nativeButton: false }}
/>With media
Pass an icon or image to the media prop to render it in the header:
<AlertDialogWrapper
trigger="Delete account"
media={<Trash2 />}
title="Delete account?"
description="All your data will be permanently deleted."
action="Delete"
/>Controlled
export function Controlled() {
const [open, setOpen] = useState(false)
return (
<>
<button onClick={() => setOpen(true)}>Open</button>
<AlertDialogWrapper
open={open}
onOpenChange={setOpen}
title="Leave page?"
description="You have unsaved changes."
action="Leave"
onAction={() => setOpen(false)}
/>
</>
)
}Async action with loading state
Pass loading to disable buttons and show a spinner on the action button. Dialog blocks Escape and backdrop close while loading:
export function AsyncDelete() {
const [open, setOpen] = useState(false)
const [loading, setLoading] = useState(false)
async function handleDelete() {
setLoading(true)
await deleteRecord()
setLoading(false)
setOpen(false)
}
return (
<AlertDialogWrapper
open={open}
onOpenChange={setOpen}
trigger="Delete"
title="Delete this record?"
description="This will permanently delete the record."
loading={loading}
onAction={handleDelete}
/>
)
}Detached trigger
Place the trigger outside the wrapper using AlertDialog.createHandle():
import { AlertDialog as AlertDialogPrimitive } from "@base-ui/react/alert-dialog"
import { AlertDialogTrigger, AlertDialogWrapper } from "@/components/ui/alert-dialog"
const handle = AlertDialogPrimitive.createHandle()
<AlertDialogTrigger handle={handle}>Open</AlertDialogTrigger>
<AlertDialogWrapper
handle={handle}
title="Are you sure?"
description="This cannot be undone."
/>Multiple detached triggers
Multiple triggers can share one dialog via the same handle:
const handle = AlertDialogPrimitive.createHandle()
<AlertDialogTrigger handle={handle}>Trigger 1</AlertDialogTrigger>
<AlertDialogTrigger handle={handle}>Trigger 2</AlertDialogTrigger>
<AlertDialogWrapper handle={handle} title="Shared dialog" />Custom footer content
Set action="" and cancel="" to hide the default footer, then pass custom footer via children:
<AlertDialogWrapper
trigger="Open"
action=""
cancel=""
>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction onClick={handleAction}>Confirm</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogWrapper>Reference
AlertDialogFooterWrapper
AlertDialogWrapper
Prop
Type