{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "elastic-string",
  "type": "registry:ui",
  "title": "Elastic String",
  "description": "A bead on a string that can be pulled aside; the string snaps back with a visible vibration.",
  "dependencies": [
    "motion",
    "lucide-react"
  ],
  "files": [
    {
      "path": "components/prequelui/moments/shared.tsx",
      "target": "components/prequelui/moments/shared.tsx",
      "type": "registry:lib",
      "content": "\"use client\";\n\nimport { useEffect, useState } from \"react\";\nimport type { Transition } from \"motion/react\";\nimport { useHydratedReducedMotion } from \"@/lib/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\nexport { cn, useHydratedReducedMotion };\n\nexport type MomentProps = {\n  className?: string;\n  preview?: boolean;\n};\n\nexport const springFast: Transition = { type: \"spring\", stiffness: 620, damping: 32 };\nexport const springSoft: Transition = { type: \"spring\", stiffness: 260, damping: 22 };\nexport const springBounce: Transition = { type: \"spring\", stiffness: 380, damping: 18 };\nexport const springHeavy: Transition = { type: \"spring\", stiffness: 200, damping: 26 };\nexport const springSnap: Transition = { type: \"spring\", stiffness: 700, damping: 38 };\n\nexport const tweenFast: Transition = { duration: 0.18, ease: [0.23, 1, 0.32, 1] };\nexport const tweenSoft: Transition = { duration: 0.32, ease: [0.23, 1, 0.32, 1] };\n\nexport const surface =\n  \"relative flex h-[150px] w-[230px] max-w-full select-none items-center justify-center text-foreground\";\n\nexport function usePreviewPulse(\n  preview: boolean | undefined,\n  duration = 900,\n  gap = 2500,\n) {\n  const [on, setOn] = useState(false);\n  const reduce = useHydratedReducedMotion();\n\n  useEffect(() => {\n    if (!preview || reduce) {\n      setOn(false);\n      return;\n    }\n    let close = 0;\n    let next = 0;\n    const run = () => {\n      setOn(true);\n      close = window.setTimeout(() => setOn(false), duration);\n      next = window.setTimeout(run, gap);\n    };\n    run();\n    return () => {\n      window.clearTimeout(close);\n      window.clearTimeout(next);\n    };\n  }, [preview, reduce, duration, gap]);\n\n  return on && !reduce;\n}\n\nexport function usePreviewStep(\n  preview: boolean | undefined,\n  steps: number,\n  interval = 1500,\n) {\n  const [step, setStep] = useState(0);\n  const reduce = useHydratedReducedMotion();\n\n  useEffect(() => {\n    if (!preview || reduce) return;\n    setStep(1);\n    const id = window.setInterval(\n      () => setStep((current) => (current + 1) % steps),\n      interval,\n    );\n    return () => window.clearInterval(id);\n  }, [preview, reduce, steps, interval]);\n\n  return reduce ? 0 : step;\n}\n\nexport function usePreviewPointer(\n  preview: boolean | undefined,\n  width = 220,\n  height = 120,\n) {\n  const [point, setPoint] = useState({ x: width / 2, y: height / 2 });\n  const reduce = useHydratedReducedMotion();\n\n  useEffect(() => {\n    if (!preview || reduce) return;\n    let frame = 0;\n    let start = 0;\n    const loop = (time: number) => {\n      if (!start) start = time;\n      const t = (time - start) / 1000;\n      const x = width / 2 + Math.cos(t * 1.4) * (width * 0.36);\n      const y = height / 2 + Math.sin(t * 2.1) * (height * 0.28);\n      setPoint({ x, y });\n      frame = requestAnimationFrame(loop);\n    };\n    frame = requestAnimationFrame(loop);\n    return () => cancelAnimationFrame(frame);\n  }, [preview, reduce, width, height]);\n\n  return [point, setPoint] as const;\n}\n\nexport function clamp(value: number, min: number, max: number) {\n  return Math.min(max, Math.max(min, value));\n}\n\n/**\n * The docs stage zooms its children (CSS `zoom`), so a pointer event reports\n * positions in zoomed pixels while the component lays itself out unscaled.\n * Normalise the event back into the component's own coordinate space.\n */\nexport function localPoint(\n  event: { clientX: number; clientY: number },\n  node: HTMLElement,\n) {\n  const rect = node.getBoundingClientRect();\n  const scaleX = node.offsetWidth ? rect.width / node.offsetWidth : 1;\n  const scaleY = node.offsetHeight ? rect.height / node.offsetHeight : 1;\n  return {\n    x: (event.clientX - rect.left) / (scaleX || 1),\n    y: (event.clientY - rect.top) / (scaleY || 1),\n  };\n}\n\nexport function naturalWidth(node: HTMLElement) {\n  return node.offsetWidth || node.getBoundingClientRect().width;\n}\n\nexport function pointerScaleX(node: HTMLElement) {\n  const rect = node.getBoundingClientRect();\n  return node.offsetWidth ? rect.width / node.offsetWidth : 1;\n}\n"
    },
    {
      "path": "components/prequelui/moments/elastic-string.tsx",
      "target": "components/prequelui/moments/elastic-string.tsx",
      "type": "registry:ui",
      "content": "\"use client\";\n\nimport { motion } from \"motion/react\";\nimport { useRef, useState } from \"react\";\nimport {\n  clamp,\n  cn,\n  localPoint,\n  naturalWidth,\n  springBounce,\n  surface,\n  useHydratedReducedMotion,\n  usePreviewPulse,\n  type MomentProps,\n} from \"./shared\";\n\nconst WIDTH = 206;\nconst HEIGHT = 84;\nconst MID = HEIGHT / 2;\nconst LEFT = 18;\nconst RIGHT = WIDTH - 18;\nconst CENTER = WIDTH / 2;\nconst LIMIT = 56;\n\nfunction stringPath(pull: number, wobble = 0) {\n  const bead = CENTER + pull;\n  const bow = 2 + (Math.abs(pull) / LIMIT) * 12;\n  const controlY = MID - bow + wobble;\n  const leftControl = (LEFT + bead) / 2;\n  const rightControl = (bead + RIGHT) / 2;\n  return `M ${LEFT} ${MID} Q ${leftControl} ${controlY} ${bead} ${MID} Q ${rightControl} ${controlY} ${RIGHT} ${MID}`;\n}\n\nconst VIBRATION = [0, 9, -6, 4, -2, 0];\n\nexport function ElasticString({ className, preview }: MomentProps) {\n  const [pull, setPull] = useState(0);\n  const [release, setRelease] = useState(0);\n  const dragging = useRef(false);\n  const band = useRef<HTMLDivElement>(null);\n  const reduce = useHydratedReducedMotion();\n  const pulse = usePreviewPulse(preview, 1100, 2600);\n  const shown = preview ? (pulse ? 44 : 0) : pull;\n\n  const setFromEvent = (event: React.PointerEvent) => {\n    const node = band.current;\n    if (!node) return;\n    const point = localPoint(event, node);\n    setPull(clamp(point.x - naturalWidth(node) / 2, -LIMIT, LIMIT));\n  };\n\n  const releaseString = () => {\n    dragging.current = false;\n    setPull(0);\n    setRelease((value) => value + 1);\n  };\n\n  const key = (event: React.KeyboardEvent) => {\n    if (event.key === \"ArrowRight\") {\n      event.preventDefault();\n      setPull((value) => clamp(value + 14, -LIMIT, LIMIT));\n    }\n    if (event.key === \"ArrowLeft\") {\n      event.preventDefault();\n      setPull((value) => clamp(value - 14, -LIMIT, LIMIT));\n    }\n    if (event.key === \"Home\") {\n      event.preventDefault();\n      setPull(-LIMIT);\n    }\n    if (event.key === \"End\") {\n      event.preventDefault();\n      setPull(LIMIT);\n    }\n    if (event.key === \"Enter\" || event.key === \" \") {\n      event.preventDefault();\n      releaseString();\n    }\n  };\n\n  const restPath = stringPath(shown, 0);\n  const pathAnimate =\n    reduce || release === 0\n      ? { d: restPath }\n      : { d: VIBRATION.map((wobble) => stringPath(0, wobble)) };\n\n  return (\n    <div className={cn(surface, \"flex-col gap-1\", className)}>\n      <div\n        ref={band}\n        role=\"slider\"\n        tabIndex={0}\n        aria-label=\"Elastic string\"\n        aria-valuemin={-LIMIT}\n        aria-valuemax={LIMIT}\n        aria-valuenow={Math.round(shown)}\n        aria-valuetext={`${Math.round(Math.abs(shown))} pixels ${\n          shown < 0 ? \"left\" : \"right\"\n        } of centre`}\n        onKeyDown={key}\n        onPointerDown={(event) => {\n          dragging.current = true;\n          event.currentTarget.setPointerCapture(event.pointerId);\n          setFromEvent(event);\n        }}\n        onPointerMove={(event) => {\n          if (dragging.current) setFromEvent(event);\n        }}\n        onPointerUp={releaseString}\n        onPointerCancel={releaseString}\n        className=\"relative h-[84px] w-[206px] touch-none rounded-xl outline-none focus-visible:ring-2 focus-visible:ring-white/50\"\n      >\n        <svg\n          aria-hidden=\"true\"\n          viewBox={`0 0 ${WIDTH} ${HEIGHT}`}\n          className=\"absolute inset-0 size-full overflow-visible\"\n        >\n          <path\n            d={stringPath(0, 0)}\n            fill=\"none\"\n            stroke=\"rgba(255,255,255,.08)\"\n            strokeWidth=\"10\"\n            strokeLinecap=\"round\"\n          />\n          <motion.path\n            key={`string-${release}`}\n            fill=\"none\"\n            stroke=\"rgba(255,255,255,.62)\"\n            strokeWidth=\"2\"\n            strokeLinecap=\"round\"\n            initial={release === 0 ? false : { d: stringPath(0, 0) }}\n            animate={pathAnimate}\n            transition={\n              reduce\n                ? { duration: 0 }\n                : release === 0\n                  ? { duration: 0.12, ease: \"easeOut\" }\n                  : { duration: 0.55, ease: [0.22, 1, 0.36, 1] }\n            }\n          />\n          <circle cx={LEFT} cy={MID} r=\"4\" fill=\"rgba(255,255,255,.75)\" />\n          <circle cx={RIGHT} cy={MID} r=\"4\" fill=\"rgba(255,255,255,.75)\" />\n          <motion.circle\n            aria-hidden=\"true\"\n            cx={CENTER}\n            cy={MID}\n            r=\"15\"\n            fill=\"rgba(255,255,255,.12)\"\n            initial={false}\n            animate={{ cx: CENTER + shown }}\n            transition={reduce ? { duration: 0 } : springBounce}\n          />\n          <motion.circle\n            cx={CENTER}\n            cy={MID}\n            r=\"8\"\n            fill=\"#ffffff\"\n            stroke=\"rgba(255,255,255,.4)\"\n            strokeWidth=\"1\"\n            initial={false}\n            animate={{ cx: CENTER + shown }}\n            transition={reduce ? { duration: 0 } : springBounce}\n          />\n        </svg>\n      </div>\n      <span className=\"text-[13px] uppercase tracking-[0.2em] text-white/35\">\n        {Math.round(Math.abs(shown)) === 0 ? \"Pull the bead\" : `${Math.round(Math.abs(shown))} px`}\n      </span>\n    </div>\n  );\n}\n"
    },
    {
      "path": "lib/use-reduced-motion.ts",
      "target": "lib/use-reduced-motion.ts",
      "type": "registry:lib",
      "content": "\"use client\";\n\nimport { useReducedMotion } from \"motion/react\";\nimport { useSyncExternalStore } from \"react\";\n\nconst subscribe = () => () => {};\n\nexport function useHydratedReducedMotion() {\n  const reduce = useReducedMotion() ?? false;\n  const mounted = useSyncExternalStore(\n    subscribe,\n    () => true,\n    () => false,\n  );\n  return mounted ? reduce : false;\n}\n"
    }
  ]
}
