HextaUI

Alert dialog: Detached triggers

Create a handle with createAlertDialogHandle() to share one dialog between many triggers. Each trigger passes its own payload, which the dialog reads through a render function.

"use client"

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
  createAlertDialogHandle,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"

const removeMember = createAlertDialogHandle<{ name: string }>()

export function AlertDialogDetachedTriggers() {
  return (
    <div className="flex flex-wrap gap-2">
      {["Olivia", "Liam"].map((name) => (
        <AlertDialogTrigger
          key={name}
          handle={removeMember}
          payload={{ name }}
          render={<Button variant="outline" />}
        >
          Remove {name}
        </AlertDialogTrigger>
      ))}
      <AlertDialog handle={removeMember}>
        {({ payload }) => (
          <AlertDialogContent size="sm">
            <AlertDialogHeader>
              <AlertDialogTitle>Remove {payload?.name}?</AlertDialogTitle>
              <AlertDialogDescription>
                They’ll lose access to this workspace.
              </AlertDialogDescription>
            </AlertDialogHeader>
            <AlertDialogFooter>
              <AlertDialogCancel>Cancel</AlertDialogCancel>
              <AlertDialogAction variant="destructive">
                Remove
              </AlertDialogAction>
            </AlertDialogFooter>
          </AlertDialogContent>
        )}
      </AlertDialog>
    </div>
  )
}

This adds the example as components/examples/alert-dialog/detached-triggers.tsx, together with the Alert dialog component and the HextaUI theme tokens it needs.

pnpm dlx shadcn@latest add https://hextaui.com/r/alert-dialog-detached-triggers.json

To add only the component, follow the Alert dialog installation.

Props in this example

The Alert dialog props this example sets, from the API reference.

PropTypeDefault
handleConnects detached triggers.
AlertDialogHandle<Payload>–
PropTypeDefault
handle
AlertDialogHandle<Payload>–
payloadPassed to the dialog’s render function.
Payload–
render
ReactElement | (props, state) => ReactElement<button>
PropTypeDefault
size
"default" | "sm""default"
PropTypeDefault
variant
ButtonVariant"default"

Every prop is in the Alert dialog API reference.

See them all, with keyboard support and accessibility notes, on the Alert dialog page.