{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "reasoning",
  "title": "Reasoning",
  "description": "Collapsible block for model reasoning: open with a live timer and shimmering label while the model thinks, then folded into \"Thought for Ns\". Takes AI SDK reasoning parts.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "registry/components/reasoning.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport { Brain, ChevronDown } from \"lucide-react\";\nimport { useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\n/** The fields this block reads from an AI SDK reasoning part (`{ type: \"reasoning\", text, state }`). */\nexport type ReasoningPartLike = {\n  type: string;\n  text: string;\n  state?: \"streaming\" | \"done\";\n};\n\nexport type ReasoningProps = {\n  /** The model's reasoning text. */\n  text?: string;\n  /** True while reasoning tokens are still arriving. */\n  streaming?: boolean;\n  /** How long the model thought, in seconds. Counted live while streaming when left unset. */\n  duration?: number;\n  /** An AI SDK reasoning part; fills text and streaming. */\n  part?: ReasoningPartLike;\n  /** Controlled open state. Left unset, the block opens while streaming and folds away when done. */\n  open?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  /** Loop a scripted think while on screen, for demos. */\n  autoPlay?: boolean;\n  className?: string;\n};\n\nconst DEMO_TEXT =\n  \"The user wants the launch email sent to the three leads. Before sending, I should check the attachment is the latest plan. The file in the thread is launch-plan.pdf from Tuesday, which matches the timeline we agreed on. Sending is hard to undo, so I'll ask for approval first and show the exact recipients.\";\n\n// Demo loop, in ms.\nconst START = 500;\nconst CHAR_MS = 11;\nconst HOLD = 3400;\nconst TICK = 50;\n\n/** A collapsible block for model reasoning: open and live while the model thinks, folded into \"Thought for Ns\" after. */\nexport function Reasoning({\n  text: textProp,\n  streaming: streamingProp,\n  duration,\n  part,\n  open: openProp,\n  onOpenChange,\n  autoPlay = false,\n  className,\n}: ReasoningProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const bodyRef = useRef<HTMLDivElement>(null);\n  const bodyId = useId();\n  const reduce = useReducedMotion();\n  const [visible, setVisible] = useState(false);\n  const [tick, setTick] = useState(0);\n  const [elapsed, setElapsed] = useState(0);\n\n  const STREAM_END = START + DEMO_TEXT.length * CHAR_MS;\n  const LOOP = STREAM_END + HOLD;\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    const id = setInterval(() => setTick((t) => (t + TICK) % LOOP), TICK);\n    return () => clearInterval(id);\n  }, [autoPlay, visible, reduce, LOOP]);\n\n  let text = textProp ?? part?.text ?? DEMO_TEXT;\n  let streaming = streamingProp ?? (part ? part.state === \"streaming\" : false);\n  let seconds = duration;\n\n  if (autoPlay) {\n    const t = reduce ? LOOP - 1 : tick;\n    text = DEMO_TEXT.slice(0, Math.max(0, Math.floor((t - START) / CHAR_MS)));\n    streaming = t < STREAM_END;\n    seconds = Math.max(1, Math.round(Math.min(t, STREAM_END) / 1000));\n  }\n\n  // Count seconds while streaming when the caller gives no duration.\n  const counting = !autoPlay && streaming && duration === undefined;\n  useEffect(() => {\n    if (!counting) return;\n    const started = Date.now();\n    const id = setInterval(() => setElapsed(Math.max(1, Math.round((Date.now() - started) / 1000))), 250);\n    return () => clearInterval(id);\n  }, [counting]);\n  if (seconds === undefined && elapsed > 0) seconds = elapsed;\n\n  // Open while streaming, fold when done. A click overrides that until the next stream starts.\n  const [userOpen, setUserOpen] = useState<boolean | null>(null);\n  const [wasStreaming, setWasStreaming] = useState(streaming);\n  if (wasStreaming !== streaming) {\n    setWasStreaming(streaming);\n    if (streaming) setUserOpen(null);\n  }\n  const open = openProp ?? userOpen ?? streaming;\n\n  // Keep the newest line in view while text streams in.\n  useEffect(() => {\n    const el = bodyRef.current;\n    if (el && streaming) el.scrollTop = el.scrollHeight;\n  }, [text, streaming]);\n\n  const label = streaming ? \"Thinking\" : seconds ? `Thought for ${seconds}s` : \"Thought\";\n\n  return (\n    <div ref={ref} className={cn(\"w-full max-w-md text-foreground\", className)}>\n      <button\n        type=\"button\"\n        aria-expanded={open}\n        aria-controls={bodyId}\n        onClick={() => {\n          if (openProp === undefined) setUserOpen(!open);\n          onOpenChange?.(!open);\n        }}\n        className=\"group flex items-center gap-2 rounded-md py-1 text-sm text-muted-foreground outline-none transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n      >\n        <Brain className={cn(\"size-4 shrink-0\", streaming && !reduce && \"animate-pulse\")} aria-hidden />\n        <span className={cn(\"font-medium\", streaming && \"reasoning-shimmer\")}>{label}</span>\n        {streaming && seconds ? <span className=\"font-mono text-xs tabular-nums\">{seconds}s</span> : null}\n        <ChevronDown\n          className={cn(\"size-3.5 shrink-0 transition-transform duration-300\", !open && \"-rotate-90\")}\n          aria-hidden\n        />\n      </button>\n\n      <div\n        id={bodyId}\n        className={cn(\n          \"grid transition-[grid-template-rows,opacity] duration-300 ease-[cubic-bezier(0.22,1,0.36,1)]\",\n          open ? \"grid-rows-[1fr] opacity-100\" : \"grid-rows-[0fr] opacity-0\",\n        )}\n      >\n        <div className=\"overflow-hidden\">\n          <div\n            ref={bodyRef}\n            className=\"mt-1.5 ml-2 max-h-48 overflow-y-auto border-l-2 pl-4 text-[13px] leading-relaxed whitespace-pre-wrap text-muted-foreground [scrollbar-width:thin]\"\n          >\n            {text}\n            {streaming && !reduce && (\n              <span aria-hidden className=\"ml-px inline-block h-3 w-1.5 translate-y-0.5 animate-pulse rounded-sm bg-muted-foreground/60\" />\n            )}\n          </div>\n        </div>\n      </div>\n\n      <p className=\"sr-only\" role=\"status\">\n        {streaming ? \"Thinking\" : label}\n      </p>\n      <style>{`\n        .reasoning-shimmer {\n          color: transparent;\n          background: linear-gradient(90deg, var(--muted-foreground) 40%, var(--foreground) 50%, var(--muted-foreground) 60%) 0 0 / 250% 100%;\n          background-clip: text;\n          -webkit-background-clip: text;\n          animation: reasoning-shimmer 2s ease-in-out infinite;\n        }\n        @keyframes reasoning-shimmer {\n          from { background-position: 100% 0; }\n          to { background-position: 0% 0; }\n        }\n        @media (prefers-reduced-motion: reduce) {\n          .reasoning-shimmer { animation: none; color: var(--muted-foreground); }\n        }\n      `}</style>\n    </div>\n  );\n}\n\nexport default function ReasoningDemo() {\n  return <Reasoning autoPlay />;\n}\n",
      "type": "registry:component",
      "target": "components/nextui/reasoning.tsx"
    }
  ],
  "meta": {
    "kind": "component",
    "category": "ai",
    "previewHeight": 320,
    "new": true,
    "free": true,
    "openSource": true,
    "added": "2026-10-31"
  },
  "categories": [
    "ai"
  ],
  "type": "registry:component"
}