{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "confirm-dialog",
  "title": "Confirm Dialog",
  "author": "zyx1121 (https://github.com/zyx1121)",
  "description": "Asks before an action, locks both buttons while it runs and closes when it finishes.",
  "registryDependencies": [
    "https://ui.zyx.tw/r/alert-dialog.json",
    "https://ui.zyx.tw/r/button.json",
    "https://ui.zyx.tw/r/spinner.json"
  ],
  "files": [
    {
      "path": "registry/zyx/components/confirm-dialog.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport {\n  AlertDialog,\n  AlertDialogCancel,\n  AlertDialogContent,\n  AlertDialogFooter,\n  AlertDialogHeader,\n  AlertDialogTitle,\n  AlertDialogTrigger,\n} from \"./ui/alert-dialog\"\nimport { Button } from \"./ui/button\"\nimport { Spinner } from \"./ui/spinner\"\n\n/** \"Delete\" → \"Deleting…\", \"Revoke access\" → \"Revoking access…\". Pass\n * pendingLabel for a verb this gets wrong (\"Run\"). */\nfunction gerund(verb: string) {\n  const [first = verb, ...rest] = verb.split(\" \")\n  const base = /[^e]e$/i.test(first) ? first.slice(0, -1) : first\n  return [`${base}ing`, ...rest].join(\" \") + \"…\"\n}\n\ntype ConfirmDialogProps = {\n  /** The button that opens it. Leave it out and pass open / onOpenChange to\n   * open it from elsewhere, such as a dropdown-menu item. */\n  trigger?: React.ReactElement\n  open?: boolean\n  onOpenChange?: (open: boolean) => void\n  /** The question: \"Delete this project?\". There is no description. */\n  title: string\n  /** The verb on the confirm button: \"Delete\", \"Revoke\". Never \"OK\". */\n  confirmLabel: string\n  /** Shown while onConfirm runs; defaults to the verb + \"ing…\". */\n  pendingLabel?: string\n  /** destructive for what cannot be undone, default for the rest. */\n  variant?: \"destructive\" | \"default\"\n  /** Runs while both buttons are locked. Resolve to close; throw to stay\n   * open so the person can retry (report the error yourself, in a toast). */\n  onConfirm: () => void | Promise<void>\n}\n\n// Asks before an action runs, and holds the dialog open until it finishes so\n// a second click can never fire it twice.\nfunction ConfirmDialog({\n  trigger,\n  open: openProp,\n  onOpenChange,\n  title,\n  confirmLabel,\n  pendingLabel = gerund(confirmLabel),\n  variant = \"destructive\",\n  onConfirm,\n}: ConfirmDialogProps) {\n  const [openState, setOpenState] = React.useState(false)\n  const [pending, startTransition] = React.useTransition()\n  const open = openProp ?? openState\n\n  function setOpen(next: boolean) {\n    if (openProp === undefined) setOpenState(next)\n    onOpenChange?.(next)\n  }\n\n  function confirm() {\n    startTransition(async () => {\n      try {\n        await onConfirm()\n        setOpen(false)\n      } catch (error) {\n        console.error(error)\n      }\n    })\n  }\n\n  return (\n    <AlertDialog\n      open={open}\n      onOpenChange={(next) => {\n        if (!pending) setOpen(next)\n      }}\n    >\n      {trigger && <AlertDialogTrigger render={trigger} />}\n      <AlertDialogContent>\n        <AlertDialogHeader>\n          <AlertDialogTitle>{title}</AlertDialogTitle>\n        </AlertDialogHeader>\n        <AlertDialogFooter>\n          <AlertDialogCancel disabled={pending}>Cancel</AlertDialogCancel>\n          <Button variant={variant} disabled={pending} onClick={confirm}>\n            {pending && <Spinner />}\n            {pending ? pendingLabel : confirmLabel}\n          </Button>\n        </AlertDialogFooter>\n      </AlertDialogContent>\n    </AlertDialog>\n  )\n}\n\nexport { ConfirmDialog }\n",
      "type": "registry:component"
    }
  ],
  "meta": {
    "useWhen": "Asking before an action that cannot be undone, and running it.",
    "notFor": "Forms or long content; use dialog.",
    "related": [
      "alert-dialog",
      "spinner"
    ]
  },
  "docs": "Use when: Asking before an action that cannot be undone, and running it.\nNot for: Forms or long content; use dialog.\nGuide: https://ui.zyx.tw/confirm-dialog.md",
  "categories": [
    "block",
    "overlay",
    "form"
  ],
  "type": "registry:block"
}