{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tool-approval",
  "title": "Tool approval",
  "description": "Inline card that asks the user before an AI agent runs a tool with side effects: the question, the tool name, its arguments as rows, and Approve / Deny buttons that settle into an approved or denied line.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "registry/components/tool-approval.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport { Check, ShieldAlert, X } from \"lucide-react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\nexport type ToolApprovalStatus = \"pending\" | \"approved\" | \"denied\";\n\nexport type ToolApprovalProps = {\n  /** The question in plain words, e.g. \"Send this email to 3 people?\" */\n  title?: React.ReactNode;\n  /** One line on what happens if the user approves. */\n  description?: React.ReactNode;\n  /** Tool name shown as a chip, e.g. \"send_email\". */\n  toolName?: string;\n  /** The arguments the agent wants to run with, shown as label/value rows. */\n  details?: Record<string, React.ReactNode>;\n  /** Controlled status. Leave unset to let the buttons drive it. */\n  status?: ToolApprovalStatus;\n  /** Called when the user approves. With the Vercel AI SDK, answer the approval here. */\n  onApprove?: () => void;\n  /** Called when the user denies. */\n  onDeny?: () => void;\n  approveLabel?: string;\n  denyLabel?: string;\n  /** Loop a scripted approval while on screen, for demos. */\n  autoPlay?: boolean;\n  className?: string;\n};\n\nconst DEMO = {\n  title: \"Send this email to 3 people?\",\n  description: \"The agent drafted a reply and wants to send it from your account.\",\n  toolName: \"send_email\",\n  details: {\n    To: \"mara@studio.co, +2\",\n    Subject: \"Re: Q4 launch timeline\",\n    Attachment: \"launch-plan.pdf\",\n  },\n};\n\n// Demo loop, in ms: wait, press Approve, hold the approved state, reset.\nconst PRESS = 2600;\nconst SETTLE = 3000;\nconst LOOP = PRESS + SETTLE + 1600;\nconst TICK = 100;\n\n/** An inline card that asks the user before an agent runs a tool with side effects. */\nexport function ToolApproval({\n  title,\n  description,\n  toolName,\n  details,\n  status: statusProp,\n  onApprove,\n  onDeny,\n  approveLabel = \"Approve\",\n  denyLabel = \"Deny\",\n  autoPlay = false,\n  className,\n}: ToolApprovalProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const reduce = useReducedMotion();\n  const [inner, setInner] = useState<ToolApprovalStatus>(\"pending\");\n  const [visible, setVisible] = useState(false);\n  const [tick, setTick] = useState(0);\n\n  useEffect(() => {\n    const el = ref.current;\n    if (!el || !autoPlay) return;\n    const io = new IntersectionObserver(([entry]) => setVisible(entry.isIntersecting));\n    io.observe(el);\n    return () => io.disconnect();\n  }, [autoPlay]);\n\n  useEffect(() => {\n    if (!autoPlay || !visible || reduce) return;\n    let t = 0;\n    const id = setInterval(() => {\n      t = (t + TICK) % LOOP;\n      setTick(t);\n      // A click by the viewer holds until the loop starts over.\n      if (t === 0) setInner(\"pending\");\n    }, TICK);\n    return () => clearInterval(id);\n  }, [autoPlay, visible, reduce]);\n\n  const demo = autoPlay;\n  const t = reduce ? 0 : tick;\n  const scripted: ToolApprovalStatus = t >= PRESS ? \"approved\" : \"pending\";\n  const status = statusProp ?? (demo && inner === \"pending\" ? scripted : inner);\n  const pressing = demo && !reduce && inner === \"pending\" && t >= PRESS - 450 && t < PRESS;\n\n  const shownTitle = title ?? DEMO.title;\n  const shownDescription = description ?? DEMO.description;\n  const shownTool = toolName ?? DEMO.toolName;\n  const shownDetails = details ?? DEMO.details;\n\n  function decide(next: ToolApprovalStatus) {\n    if (statusProp === undefined) setInner(next);\n    if (next === \"approved\") onApprove?.();\n    else onDeny?.();\n  }\n\n  return (\n    <div ref={ref} className={cn(\"w-full max-w-md rounded-xl bg-muted p-1.5 text-foreground\", className)}>\n      <div className=\"rounded-lg border bg-card p-3.5 shadow-xs\">\n        <div className=\"flex items-start gap-3\">\n          <span className=\"grid size-8 shrink-0 place-items-center rounded-lg border bg-background text-foreground\" aria-hidden>\n            <ShieldAlert className=\"size-4\" />\n          </span>\n          <div className=\"min-w-0 flex-1\">\n            <p className=\"text-sm font-medium\">{shownTitle}</p>\n            {shownDescription && <p className=\"mt-0.5 text-xs leading-relaxed text-muted-foreground\">{shownDescription}</p>}\n          </div>\n          {shownTool && (\n            <span className=\"shrink-0 rounded-md bg-muted px-1.5 py-0.5 font-mono text-[11px] text-muted-foreground\">{shownTool}</span>\n          )}\n        </div>\n\n        {Object.keys(shownDetails).length > 0 && (\n          <dl className=\"mt-3 divide-y rounded-md border text-xs\">\n            {Object.entries(shownDetails).map(([label, value]) => (\n              <div key={label} className=\"flex gap-3 px-3 py-2\">\n                <dt className=\"w-20 shrink-0 text-muted-foreground\">{label}</dt>\n                <dd className=\"min-w-0 flex-1 truncate font-medium\">{value}</dd>\n              </div>\n            ))}\n          </dl>\n        )}\n\n        <div className=\"mt-3 h-8\">\n          <AnimatePresence mode=\"wait\" initial={false}>\n            {status === \"pending\" ? (\n              <motion.div\n                key=\"ask\"\n                initial={reduce ? false : { opacity: 0, y: 4 }}\n                animate={{ opacity: 1, y: 0 }}\n                exit={reduce ? undefined : { opacity: 0, y: -4, filter: \"blur(3px)\" }}\n                transition={{ duration: 0.25, ease: [0.22, 1, 0.36, 1] }}\n                className=\"flex justify-end gap-2\"\n              >\n                <button\n                  type=\"button\"\n                  onClick={() => decide(\"denied\")}\n                  className=\"h-8 rounded-md border bg-background px-3 text-xs font-medium text-foreground transition-colors outline-none hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring\"\n                >\n                  {denyLabel}\n                </button>\n                <motion.button\n                  type=\"button\"\n                  onClick={() => decide(\"approved\")}\n                  animate={pressing ? { scale: [1, 0.94, 1] } : { scale: 1 }}\n                  transition={{ duration: 0.45, ease: \"easeOut\" }}\n                  className=\"h-8 rounded-md bg-foreground px-3 text-xs font-medium text-background transition-opacity outline-none hover:opacity-90 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2\"\n                >\n                  {approveLabel}\n                </motion.button>\n              </motion.div>\n            ) : (\n              <motion.div\n                key={status}\n                initial={reduce ? false : { opacity: 0, y: 6, filter: \"blur(3px)\" }}\n                animate={{ opacity: 1, y: 0, filter: \"blur(0px)\" }}\n                exit={reduce ? undefined : { opacity: 0 }}\n                transition={{ duration: 0.3, ease: [0.22, 1, 0.36, 1] }}\n                className=\"flex h-8 items-center gap-2 text-xs\"\n                role=\"status\"\n              >\n                <motion.span\n                  initial={reduce ? false : { scale: 0.4 }}\n                  animate={{ scale: 1 }}\n                  transition={{ type: \"spring\", stiffness: 520, damping: 20 }}\n                  className={cn(\n                    \"grid size-5 place-items-center rounded-full\",\n                    status === \"approved\" ? \"bg-foreground text-background\" : \"bg-muted text-muted-foreground\",\n                  )}\n                  aria-hidden\n                >\n                  {status === \"approved\" ? <Check className=\"size-3\" strokeWidth={3} /> : <X className=\"size-3\" strokeWidth={3} />}\n                </motion.span>\n                <span className={cn(\"font-medium\", status === \"denied\" && \"text-muted-foreground\")}>\n                  {status === \"approved\" ? \"Approved. The agent is continuing.\" : \"Denied. The agent won't run this.\"}\n                </span>\n              </motion.div>\n            )}\n          </AnimatePresence>\n        </div>\n      </div>\n    </div>\n  );\n}\n\nexport default function ToolApprovalDemo() {\n  return <ToolApproval autoPlay />;\n}\n",
      "type": "registry:component",
      "target": "components/nextui/tool-approval.tsx"
    }
  ],
  "meta": {
    "kind": "component",
    "category": "ai",
    "previewHeight": 380,
    "new": true,
    "free": true,
    "openSource": true,
    "added": "2026-10-31"
  },
  "categories": [
    "ai"
  ],
  "type": "registry:component"
}