{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ui-thinking",
  "title": "Thinking indicator",
  "description": "Thinking indicator with a pixel wave and a rolling seconds counter; when finished it reads \"Thought for Ns\" and expands to show the reasoning.",
  "registryDependencies": [
    "https://elements.lumesec.ai/r/lumesec-theme.json",
    "https://elements.lumesec.ai/r/lumesec-canvas.json",
    "https://elements.lumesec.ai/r/lumesec-color.json",
    "https://elements.lumesec.ai/r/lumesec-math.json",
    "https://elements.lumesec.ai/r/lumesec-motion.json",
    "https://elements.lumesec.ai/r/lumesec-roll-text.json",
    "https://elements.lumesec.ai/r/lumesec-use-controllable-state.json",
    "utils"
  ],
  "files": [
    {
      "path": "registry/components/lumesec/ui-thinking.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { useCanvas } from \"@/lib/lumesec/canvas\";\nimport { usePalette, type ColorToken, type Palette } from \"@/lib/lumesec/color\";\nimport { mixRgb, rgba } from \"@/lib/lumesec/math\";\nimport { prefersReducedMotion, useAnimationLoop, useReducedMotion } from \"@/lib/lumesec/motion\";\nimport { RollText } from \"@/lib/lumesec/roll-text\";\nimport { useControllableState } from \"@/lib/lumesec/use-controllable-state\";\nimport { cn } from \"@/lib/utils\";\n\n/** Imperative controls, for callers that do not track `done` themselves. */\nexport interface UiThinkingHandle {\n  /** Finish: the glyph draws a check and the label rolls to \"Thought for Ns\". Same as setting `done`. */\n  finish(): void;\n  /** Reset the counter to 0 and return to the thinking state, collapsing the reasoning. */\n  restart(): void;\n}\n\nexport interface UiThinkingProps extends Omit<React.ComponentProps<\"div\">, \"ref\"> {\n  /** Whether thinking has finished (controlled). Use with `onDoneChange`. */\n  done?: boolean;\n  /** Whether it starts finished, when uncontrolled. */\n  defaultDone?: boolean;\n  /** Called when `finish()` or `restart()` changes the state. */\n  onDoneChange?: (done: boolean) => void;\n  /**\n   * Called when the state changes to done, with the whole seconds elapsed (at least 1). Not called when the\n   * component mounts already done.\n   */\n  onDone?: (seconds: number) => void;\n  /** The reasoning, shown in a collapsible panel once done. */\n  children?: React.ReactNode;\n  /** Imperative `finish()` and `restart()`. */\n  ref?: React.Ref<UiThinkingHandle>;\n}\n\n/**\n * Thinking indicator: a pixel wave and a shimmering label while the model works, with a seconds counter that\n * rolls on every tick. When done, the glyph draws a check and the label rolls to \"Thought for Ns\", which opens\n * the reasoning. Seconds count from mount, or from `restart()`.\n *\n * @example\n * <UiThinking done={!streaming}>The reasoning text.</UiThinking>\n */\nexport function UiThinking({\n  done: doneProp,\n  defaultDone = false,\n  onDoneChange,\n  onDone,\n  children,\n  className,\n  ref,\n  ...props\n}: UiThinkingProps) {\n  const [done, setDone] = useControllableState({ value: doneProp, defaultValue: defaultDone, onChange: onDoneChange });\n  const [seconds, setSeconds] = React.useState(0);\n  /** Bumped by `restart()` to restart the counter. */\n  const [run, setRun] = React.useState(0);\n  const [expanded, setExpanded] = React.useState(false);\n\n  // Returning to the thinking state collapses the reasoning.\n  const [wasDone, setWasDone] = React.useState(done);\n  if (wasDone !== done) {\n    setWasDone(done);\n    if (!done) setExpanded(false);\n  }\n\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const labelRef = React.useRef<HTMLSpanElement>(null);\n  const startedAt = React.useRef(0);\n  const reportedDone = React.useRef(done);\n\n  const reducedMotion = useReducedMotion();\n  const panelId = React.useId();\n\n  const loop = useAnimationLoop(rootRef, step);\n  // a resize clears the canvas, so redraw (the still frame with reduced motion would stay blank otherwise)\n  const canvas = useCanvas(canvasRef, () => loop.kick());\n  const colors = usePalette(rootRef, WAVE_COLORS, () => loop.kick());\n\n  function step(time: number) {\n    // once done, the canvas fades out with CSS on the last drawn frame\n    if (done) return false;\n    const surface = canvas.current;\n    const palette = colors.current;\n    if (!surface || !palette) return true;\n    const reduced = prefersReducedMotion();\n    drawWave(surface.context, palette, time, reduced);\n    // the label shimmer rides the same clock\n    const label = labelRef.current;\n    if (reduced) label?.style.removeProperty(\"--thinking-shimmer\");\n    else label?.style.setProperty(\"--thinking-shimmer\", `${100 - ((time / 1.9) % 1) * 100}%`);\n    // with reduced motion the wave is a still frame: draw it once and let the loop sleep\n    return !reduced;\n  }\n\n  // a change of the motion preference wakes the loop, to restart the wave or draw the still frame\n  React.useEffect(() => {\n    loop.kick();\n  }, [reducedMotion, loop]);\n\n  React.useEffect(() => {\n    startedAt.current = performance.now();\n  }, []);\n\n  // The counter runs on a timer, so it stays right while the wave is paused off screen.\n  React.useEffect(() => {\n    if (done) return;\n    let timer = 0;\n    const tick = () => {\n      const elapsed = Math.max(0, performance.now() - startedAt.current);\n      setSeconds(Math.floor(elapsed / 1000));\n      // wake just after the next whole second\n      timer = window.setTimeout(tick, 1000 - (elapsed % 1000));\n    };\n    timer = window.setTimeout(tick, 0);\n    loop.kick();\n    return () => window.clearTimeout(timer);\n  }, [done, run, loop]);\n\n  React.useEffect(() => {\n    if (reportedDone.current === done) return;\n    reportedDone.current = done;\n    if (done) onDone?.(Math.max(1, seconds));\n  }, [done, seconds, onDone]);\n\n  React.useImperativeHandle(\n    ref,\n    () => ({\n      finish: () => setDone(true),\n      restart: () => {\n        startedAt.current = performance.now();\n        setSeconds(0);\n        setRun((value) => value + 1);\n        setDone(false);\n      },\n    }),\n    [setDone],\n  );\n\n  return (\n    <div\n      ref={rootRef}\n      data-slot=\"ui-thinking\"\n      data-state={done ? \"done\" : \"thinking\"}\n      className={cn(\n        \"group/thinking block text-[14px] text-foreground [-webkit-tap-highlight-color:transparent]\",\n        className,\n      )}\n      {...props}\n    >\n      <div className=\"flex items-center gap-2.5\">\n        <span className=\"relative h-3.5 w-5 flex-none\">\n          <canvas\n            ref={canvasRef}\n            aria-hidden=\"true\"\n            className=\"absolute inset-0 block size-full [transition:opacity_.3s_ease,transform_.45s_cubic-bezier(.2,.8,.2,1)] group-data-[state=done]/thinking:[transform:scale(.3)] group-data-[state=done]/thinking:opacity-0\"\n          />\n          <svg\n            viewBox=\"0 0 18 18\"\n            aria-hidden=\"true\"\n            className=\"absolute -top-0.5 left-px size-[18px] fill-none stroke-lumesec stroke-2 [stroke-linecap:round] [stroke-linejoin:round]\"\n          >\n            <path\n              d=\"M4 9.5l3.2 3.2L14 5.5\"\n              className=\"[stroke-dasharray:16] [stroke-dashoffset:16] [transition:stroke-dashoffset_.45s_cubic-bezier(.3,.7,.3,1)_.12s] group-data-[state=done]/thinking:[stroke-dashoffset:0]\"\n            />\n          </svg>\n        </span>\n        <button\n          type=\"button\"\n          aria-expanded={expanded}\n          aria-controls={panelId}\n          disabled={!done}\n          onClick={() => setExpanded((open) => !open)}\n          className=\"-mx-1.5 -my-1 flex cursor-default items-center gap-2 rounded-lg border-0 bg-transparent px-1.5 py-1 text-inherit outline-none select-none [font:inherit] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-lumesec focus-visible:outline-solid enabled:cursor-pointer enabled:hover:bg-foreground/7\"\n        >\n          <RollText\n            ref={labelRef}\n            text={done ? `Thought for ${Math.max(1, seconds)}s` : \"Thinking\"}\n            direction={done ? 1 : -1}\n            className={cn(\"*:font-medium\", !done && SHIMMER_CLASSES)}\n          />\n          <RollText\n            text={`${seconds}s`}\n            animate={seconds > 0}\n            className=\"font-mono text-[12px] text-muted-foreground tabular-nums group-data-[state=done]/thinking:hidden\"\n          />\n          <svg\n            viewBox=\"0 0 12 12\"\n            aria-hidden=\"true\"\n            className={cn(\n              \"size-3 fill-none stroke-muted-foreground [stroke-width:1.6] opacity-0 [stroke-linecap:round] [stroke-linejoin:round] [transition:transform_.35s_cubic-bezier(.2,.8,.2,1),opacity_.3s] group-data-[state=done]/thinking:opacity-100\",\n              expanded && \"[transform:rotate(90deg)]\",\n            )}\n          >\n            <path d=\"M4.5 2.5L8 6l-3.5 3.5\" />\n          </svg>\n        </button>\n      </div>\n      <div\n        id={panelId}\n        data-state={expanded ? \"open\" : \"closed\"}\n        // collapsed reasoning is out of the reading and tab order, not just invisible\n        inert={!expanded}\n        className=\"grid grid-rows-[0fr] opacity-0 [transition:grid-template-rows_.4s_cubic-bezier(.2,.8,.2,1),opacity_.3s] data-[state=open]:grid-rows-[1fr] data-[state=open]:opacity-100\"\n      >\n        <div className=\"overflow-hidden\">\n          <div className=\"mt-2.5 mb-0.5 ml-[9px] border-l-2 border-muted pl-[18px] text-[13px] leading-[1.55] text-muted-foreground\">\n            {children}\n          </div>\n        </div>\n      </div>\n    </div>\n  );\n}\n\nconst WAVE_COLORS = {\n  accent: [\"--lumesec\", \"#6219f3\"],\n  glint: [\"--lumesec-glint\", \"#c9bdff\"],\n} as const satisfies Record<string, ColorToken>;\n\n/**\n * The thinking label: a light band sweeps across the muted text. Applied to each rolling item; the loop moves\n * `--thinking-shimmer` from 100% to 0% every 1.9 s.\n */\nconst SHIMMER_CLASSES =\n  \"*:bg-[linear-gradient(100deg,var(--muted-foreground)_0%,var(--muted-foreground)_38%,var(--foreground)_50%,var(--muted-foreground)_62%,var(--muted-foreground)_100%)] *:bg-clip-text *:text-transparent *:[background-position:var(--thinking-shimmer,0%)_0] *:[background-size:260%_100%]\";\n\n/** A 6 x 4 grid of 2px pixels; a sine wave runs through it and brightens toward the glint. */\nfunction drawWave(\n  context: CanvasRenderingContext2D,\n  colors: Palette<keyof typeof WAVE_COLORS>,\n  time: number,\n  reduced: boolean,\n) {\n  context.clearRect(0, 0, 20, 14);\n  for (let column = 0; column < 6; column++) {\n    for (let row = 0; row < 4; row++) {\n      const wave = reduced ? 0.55 : Math.max(0, Math.sin(time * 5.5 - column * 0.85 + row * 0.55));\n      const k = wave * wave;\n      context.globalAlpha = 0.18 + 0.82 * k;\n      context.fillStyle = rgba(mixRgb(colors.accent, colors.glint, k * 0.7));\n      context.fillRect(1 + column * 3.2, 1 + row * 3.2, 2, 2);\n    }\n  }\n  context.globalAlpha = 1;\n}\n",
      "type": "registry:component",
      "target": "@components/lumesec/ui-thinking.tsx"
    }
  ],
  "meta": {
    "tag": "ui-thinking",
    "collection": "interaction",
    "react": "UiThinking",
    "kind": "react"
  },
  "docs": "Thinking indicator docs: https://elements.lumesec.ai/components/interaction/thinking",
  "categories": [
    "interaction",
    "feedback"
  ],
  "type": "registry:component"
}