{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ui-send-button",
  "title": "Send / stop button",
  "description": "Round send button that turns into a stop button with an orbiting ring while a reply streams, then closes the ring with sparks on completion.",
  "registryDependencies": [
    "https://elements.lumesec.ai/r/lumesec-theme.json",
    "https://elements.lumesec.ai/r/lumesec-motion.json",
    "https://elements.lumesec.ai/r/lumesec-sparks.json",
    "https://elements.lumesec.ai/r/lumesec-use-controllable-state.json",
    "utils"
  ],
  "files": [
    {
      "path": "registry/components/lumesec/ui-send-button.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { prefersReducedMotion, useReducedMotion } from \"@/lib/lumesec/motion\";\nimport { BurstLayer, useBurst } from \"@/lib/lumesec/sparks\";\nimport { useControllableState } from \"@/lib/lumesec/use-controllable-state\";\nimport { cn } from \"@/lib/utils\";\n\n/** Imperative actions on the send button, exposed through `ref`. */\nexport interface UiSendButtonHandle {\n  /** Switch to the running state and call `onSend`. Ignored while running. */\n  send(): void;\n  /** Return to idle and call `onStop`. Ignored when idle or while the ring is closing. */\n  stop(): void;\n  /**\n   * Close the ring, burst sparks, return to idle and call `onComplete` about 380 ms later. Ignored when\n   * idle or already completing.\n   */\n  complete(): void;\n}\n\nexport interface UiSendButtonProps extends Omit<React.ComponentProps<\"button\">, \"ref\" | \"children\"> {\n  /** Controlled running state. Use with `onRunningChange`. */\n  running?: boolean;\n  /** Initial running state when uncontrolled. */\n  defaultRunning?: boolean;\n  /** Called when a send, stop or completion changes the running state. */\n  onRunningChange?: (running: boolean) => void;\n  /**\n   * Seconds after which a send completes on its own, for demos. `0` means the button waits for\n   * `complete()` or a stop. Read on each send.\n   */\n  demoDuration?: number;\n  /** Called when the idle button is pressed or `send()` is called. */\n  onSend?: () => void;\n  /** Called when a running send is stopped with the button or `stop()`. */\n  onStop?: () => void;\n  /** Called when a running send completes, after the ring has closed. */\n  onComplete?: () => void;\n  /** Exposes `send()`, `stop()` and `complete()`. */\n  ref?: React.Ref<UiSendButtonHandle>;\n}\n\ntype RingPhase = \"idle\" | \"complete\" | \"fade\";\n\n/**\n * Round send button that turns into a stop button with an orbiting ring while a reply streams, then\n * closes the ring with sparks on completion. The button does not stream anything itself: call\n * `complete()` through `ref` when the reply finishes.\n *\n * @example\n * const button = React.useRef<UiSendButtonHandle>(null);\n * <UiSendButton ref={button} onSend={() => stream().then(() => button.current?.complete())} />\n */\nexport function UiSendButton({\n  running: runningProp,\n  defaultRunning = false,\n  onRunningChange,\n  demoDuration = 0,\n  onSend,\n  onStop,\n  onComplete,\n  ref,\n  className,\n  onClick,\n  ...props\n}: UiSendButtonProps) {\n  const [running, setRunning] = useControllableState({\n    value: runningProp,\n    defaultValue: defaultRunning,\n    onChange: onRunningChange,\n  });\n  const [ring, setRing] = React.useState<RingPhase>(\"idle\");\n  const reduced = useReducedMotion();\n  const buttonRef = React.useRef<HTMLButtonElement>(null);\n  const arrowRef = React.useRef<HTMLSpanElement>(null);\n  const stopRef = React.useRef<HTMLSpanElement>(null);\n  const circleRef = React.useRef<SVGCircleElement>(null);\n  const { ref: sparksRef, fire: fireSparks } = useBurst();\n\n  const runningNow = React.useRef(running);\n  const completing = React.useRef(false);\n  const timers = React.useRef({ demo: 0, close: 0, reset: 0 });\n  const latest = React.useRef({ demoDuration, onSend, onStop, onComplete });\n  React.useEffect(() => {\n    runningNow.current = running;\n    latest.current = { demoDuration, onSend, onStop, onComplete };\n  });\n\n  const actions = React.useMemo<UiSendButtonHandle>(() => {\n    const update = (next: boolean) => {\n      runningNow.current = next;\n      setRunning(next);\n    };\n    const complete = () => {\n      if (!runningNow.current || completing.current) return;\n      window.clearTimeout(timers.current.demo);\n      // a previous completion's fade must not reset the ring halfway through this one\n      window.clearTimeout(timers.current.reset);\n      completing.current = true;\n      setRing(\"complete\");\n      timers.current.close = window.setTimeout(() => {\n        completing.current = false;\n        setRing(\"fade\");\n        if (buttonRef.current) fireSparks(buttonRef.current, { count: 26, speed: [60, 210] });\n        update(false);\n        latest.current.onComplete?.();\n        timers.current.reset = window.setTimeout(() => setRing(\"idle\"), 320);\n      }, 380);\n    };\n    return {\n      send() {\n        if (runningNow.current) return;\n        update(true);\n        latest.current.onSend?.();\n        window.clearTimeout(timers.current.demo);\n        const seconds = latest.current.demoDuration;\n        if (seconds > 0) timers.current.demo = window.setTimeout(complete, seconds * 1000);\n      },\n      // a stop while the ring closes would report both outcomes, so it is ignored\n      stop() {\n        if (!runningNow.current || completing.current) return;\n        window.clearTimeout(timers.current.demo);\n        update(false);\n        latest.current.onStop?.();\n      },\n      complete,\n    };\n  }, [setRunning, fireSparks]);\n\n  React.useImperativeHandle(ref, () => actions, [actions]);\n\n  // icons swap with a small choreography whenever the state flips, from a click or from outside\n  const previous = React.useRef(running);\n  React.useLayoutEffect(() => {\n    if (previous.current === running) return;\n    previous.current = running;\n    if (!running) {\n      window.clearTimeout(timers.current.demo);\n      if (completing.current) {\n        // stopped from outside while the ring was closing: drop the completion\n        completing.current = false;\n        window.clearTimeout(timers.current.close);\n        setRing(\"idle\");\n      }\n    }\n    if (prefersReducedMotion()) return;\n    if (running) animateIn(arrowRef.current, stopRef.current);\n    else animateOut(arrowRef.current, stopRef.current);\n  }, [running]);\n\n  // the orbiting arc: 1.1 s per turn until the ring closes\n  React.useEffect(() => {\n    const circle = circleRef.current;\n    if (!running || ring !== \"idle\" || reduced || !circle) return;\n    const spin = circle.animate([{ transform: \"rotate(0deg)\" }, { transform: \"rotate(360deg)\" }], {\n      duration: 1100,\n      iterations: Infinity,\n    });\n    return () => spin.cancel();\n  }, [running, ring, reduced]);\n\n  React.useEffect(() => {\n    const pending = timers.current;\n    return () => {\n      window.clearTimeout(pending.demo);\n      window.clearTimeout(pending.close);\n      window.clearTimeout(pending.reset);\n    };\n  }, []);\n\n  const state = running ? \"running\" : \"idle\";\n  const ringVisible = (running || ring === \"complete\") && ring !== \"fade\";\n\n  return (\n    <button\n      ref={buttonRef}\n      type=\"button\"\n      data-slot=\"ui-send-button\"\n      data-state={state}\n      aria-label={running ? \"Stop\" : \"Send\"}\n      className={cn(\n        \"group relative grid size-10 cursor-pointer place-items-center rounded-full border-0 bg-lumesec p-0 font-[inherit] text-lumesec-foreground outline-none select-none\",\n        \"[transition:background-color_.35s_ease,color_.35s_ease,transform_.2s_cubic-bezier(.2,.8,.2,1),box-shadow_.35s] active:[transform:scale(.9)] disabled:pointer-events-none disabled:opacity-50\",\n        \"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-lumesec focus-visible:outline-solid\",\n        \"data-[state=running]:bg-muted data-[state=running]:text-foreground data-[state=running]:shadow-[inset_0_0_0_1px_var(--border)]\",\n        className,\n      )}\n      onClick={(event) => {\n        onClick?.(event);\n        if (event.defaultPrevented) return;\n        if (runningNow.current) actions.stop();\n        else actions.send();\n      }}\n      {...props}\n    >\n      <span\n        ref={arrowRef}\n        className=\"col-start-1 row-start-1 grid place-items-center group-data-[state=running]:opacity-0\"\n      >\n        <svg\n          viewBox=\"0 0 18 18\"\n          aria-hidden=\"true\"\n          className=\"size-[18px] fill-none stroke-current [stroke-width:2.2] [stroke-linecap:round] [stroke-linejoin:round]\"\n        >\n          <path d=\"M9 14.5V3.8M4.3 8.3L9 3.6l4.7 4.7\" />\n        </svg>\n      </span>\n      <span\n        ref={stopRef}\n        className=\"col-start-1 row-start-1 grid [transform:scale(0)] place-items-center opacity-0 group-data-[state=running]:[transform:none] group-data-[state=running]:opacity-100\"\n      >\n        <span className=\"block size-3 rounded-[3px] bg-current\" />\n      </span>\n      <svg\n        viewBox=\"0 0 48 48\"\n        aria-hidden=\"true\"\n        className={cn(\n          \"pointer-events-none absolute -top-1 -left-1 size-12 overflow-visible [transition:opacity_.3s]\",\n          ringVisible ? \"opacity-100\" : \"opacity-0\",\n        )}\n      >\n        <circle\n          ref={circleRef}\n          cx=\"24\"\n          cy=\"24\"\n          r=\"22\"\n          className={cn(\n            \"origin-[24px_24px] fill-none stroke-lumesec [stroke-width:2.2] [stroke-dasharray:34_105] [stroke-linecap:round]\",\n            ring !== \"idle\" && \"[stroke-dasharray:139_0] [transition:stroke-dasharray_.35s_cubic-bezier(.3,.7,.3,1)]\",\n          )}\n        />\n      </svg>\n      <BurstLayer ref={sparksRef} />\n    </button>\n  );\n}\n\nfunction animateIn(arrow: HTMLElement | null, stop: HTMLElement | null) {\n  arrow?.animate(\n    [\n      { transform: \"translateY(0)\", opacity: 1 },\n      { transform: \"translateY(-16px)\", opacity: 0 },\n    ],\n    { duration: 220, easing: \"cubic-bezier(.5,0,.75,0)\" },\n  );\n  stop?.animate(\n    [\n      { transform: \"scale(0) rotate(-90deg)\", opacity: 0 },\n      { transform: \"scale(1.2) rotate(8deg)\", opacity: 1, offset: 0.6 },\n      { transform: \"scale(1) rotate(0deg)\", opacity: 1 },\n    ],\n    { duration: 380, delay: 90, easing: \"cubic-bezier(.2,.8,.2,1)\", fill: \"backwards\" },\n  );\n}\n\nfunction animateOut(arrow: HTMLElement | null, stop: HTMLElement | null) {\n  stop?.animate(\n    [\n      { transform: \"scale(1)\", opacity: 1 },\n      { transform: \"scale(0)\", opacity: 0 },\n    ],\n    { duration: 180, easing: \"ease-in\" },\n  );\n  arrow?.animate(\n    [\n      { transform: \"translateY(16px)\", opacity: 0 },\n      { transform: \"translateY(-3px)\", opacity: 1, offset: 0.6 },\n      { transform: \"translateY(0)\", opacity: 1 },\n    ],\n    { duration: 420, delay: 100, easing: \"cubic-bezier(.2,.8,.2,1)\", fill: \"backwards\" },\n  );\n}\n",
      "type": "registry:component",
      "target": "@components/lumesec/ui-send-button.tsx"
    }
  ],
  "meta": {
    "tag": "ui-send-button",
    "collection": "interaction",
    "react": "UiSendButton",
    "kind": "react"
  },
  "docs": "Send / stop button docs: https://elements.lumesec.ai/components/interaction/send-button",
  "categories": [
    "interaction",
    "controls"
  ],
  "type": "registry:component"
}