{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "curve-editor",
  "title": "Curve editor",
  "description": "a cubic-bezier easing: drag or arrow the two handles, pick a preset or paste CSS · a live preview",
  "dependencies": [
    "cn@^0.5.2",
    "lucide-react@^1.52.0"
  ],
  "registryDependencies": [
    "@halftone-web/control-select",
    "@halftone-web/hook-use-reduced-motion",
    "@halftone-web/kit-setup",
    "@halftone-web/lib-controls-bezier",
    "@halftone-web/lib-controls-scrub"
  ],
  "files": [
    {
      "path": "components/controls/curve-editor.tsx",
      "content": "'use client';\nimport { cn } from 'cn';\nimport { PlayIcon } from 'lucide-react';\nimport * as React from 'react';\nimport { ControlSelect } from '@/components/controls/select';\nimport { useReducedMotion } from '@/hooks/use-reduced-motion';\nimport { bezierPath, bezierToCss, clampBezier, EASING_PRESETS, parseBezier, type Bezier } from '@/lib/controls/bezier';\nimport { modifierOf, roundTo } from '@/lib/controls/scrub';\n\n/** The plot is PLOT units square; the box leaves PAD above and below for a curve that overshoots. */\nconst PLOT = 160;\nconst PAD_X = 12;\nconst PAD_Y = 40;\nconst W = PLOT + PAD_X * 2;\nconst H = PLOT + PAD_Y * 2;\nconst Y_MIN = -PAD_Y / PLOT;\nconst Y_MAX = 1 + PAD_Y / PLOT;\n\nconst toX = (x: number) => PAD_X + x * PLOT;\nconst toY = (y: number) => PAD_Y + (1 - y) * PLOT;\nconst fix = (n: number) => roundTo(n, 0.001);\n\ntype Handle = 0 | 1;\n\n/**\n * A cubic-bezier easing, edited by hand: drag the two handles (or focus one and use the arrows: Left/Right move it\n * across, Up/Down move it up; Shift for fine, Alt for coarse), pick a preset, or paste any CSS curve into the field. The\n * preview plays the curve on a moving dot; with reduced motion it stays still.\n */\nexport function CurveEditor({\n  label,\n  value,\n  onChange,\n  duration = 600,\n  className,\n}: {\n  label: string;\n  value: Bezier;\n  onChange: (value: Bezier) => void;\n  /** How long the preview runs, in ms. */\n  duration?: number;\n  className?: string;\n}) {\n  const svgRef = React.useRef<SVGSVGElement>(null);\n  const [drag, setDrag] = React.useState<Handle | null>(null);\n  const [draft, setDraft] = React.useState<string | null>(null);\n  const css = bezierToCss(value);\n  const preset = Object.entries(EASING_PRESETS).find(([, c]) => c.every((n, i) => n === value[i]))?.[0] ?? 'custom';\n  const invalid = draft !== null && parseBezier(draft) === null;\n\n  const setHandle = (h: Handle, x: number, y: number) => {\n    const next = value.slice() as Bezier;\n    next[h * 2] = fix(Math.min(1, Math.max(0, x)));\n    next[h * 2 + 1] = fix(y);\n    onChange(clampBezier(next));\n  };\n\n  const fromPointer = (e: React.PointerEvent) => {\n    const rect = svgRef.current!.getBoundingClientRect();\n    const sx = ((e.clientX - rect.left) / rect.width) * W;\n    const sy = ((e.clientY - rect.top) / rect.height) * H;\n    return { x: (sx - PAD_X) / PLOT, y: Math.min(Y_MAX, Math.max(Y_MIN, 1 - (sy - PAD_Y) / PLOT)) };\n  };\n\n  const onKey = (h: Handle) => (e: React.KeyboardEvent) => {\n    const step = { fine: 0.001, normal: 0.01, coarse: 0.1 }[modifierOf(e)];\n    const [x, y] = [value[h * 2], value[h * 2 + 1]];\n    const moves: Record<string, [number, number]> = { ArrowLeft: [-step, 0], ArrowRight: [step, 0], ArrowUp: [0, step], ArrowDown: [0, -step] };\n    const m = moves[e.key];\n    if (!m) return;\n    e.preventDefault();\n    setHandle(h, x + m[0], y + m[1]);\n  };\n\n  const handles: [Handle, number, number][] = [\n    [0, value[0], value[1]],\n    [1, value[2], value[3]],\n  ];\n\n  return (\n    <div className={cn('flex min-w-0 flex-col gap-2', className)}>\n      <svg\n        ref={svgRef}\n        viewBox={`0 0 ${W} ${H}`}\n        role=\"group\"\n        aria-label={`${label}: ${css}`}\n        className=\"bg-muted-subtle border-border w-full max-w-56 touch-none self-center rounded-md border select-none\"\n        onPointerMove={(e) => {\n          if (drag === null) return;\n          const p = fromPointer(e);\n          setHandle(drag, p.x, p.y);\n        }}\n        onPointerUp={() => setDrag(null)}\n        onPointerCancel={() => setDrag(null)}>\n        <rect x={toX(0)} y={toY(1)} width={PLOT} height={PLOT} className=\"fill-card stroke-border\" strokeWidth={1} />\n        <line x1={toX(0)} y1={toY(0)} x2={toX(1)} y2={toY(1)} className=\"stroke-border\" strokeDasharray=\"3 3\" />\n        {handles.map(([h, x, y]) => (\n          <line key={`arm-${h}`} x1={toX(h)} y1={toY(h)} x2={toX(x)} y2={toY(y)} className=\"stroke-muted-foreground\" strokeWidth={1} />\n        ))}\n        <path d={bezierPath(value, PLOT, PLOT)} transform={`translate(${PAD_X} ${PAD_Y})`} className=\"stroke-foreground\" strokeWidth={2} fill=\"none\" />\n        {handles.map(([h, x, y]) => (\n          <circle\n            key={`handle-${h}`}\n            cx={toX(x)}\n            cy={toY(y)}\n            r={7}\n            tabIndex={0}\n            role=\"slider\"\n            aria-label={`${label}, ${h === 0 ? 'first' : 'second'} handle`}\n            aria-valuemin={Y_MIN}\n            aria-valuemax={Y_MAX}\n            aria-valuenow={y}\n            aria-valuetext={`x ${x}, y ${y}`}\n            onPointerDown={(e) => {\n              e.preventDefault();\n              (e.currentTarget as SVGCircleElement).ownerSVGElement?.setPointerCapture(e.pointerId);\n              (e.currentTarget as SVGElement).focus();\n              setDrag(h);\n            }}\n            onKeyDown={onKey(h)}\n            className=\"fill-card stroke-foreground focus-visible:stroke-ring cursor-grab outline-none focus-visible:[stroke-width:4]\"\n            strokeWidth={2}\n          />\n        ))}\n      </svg>\n      <div className=\"flex min-w-0 items-center gap-1.5\">\n        <ControlSelect\n          label={`${label} preset`}\n          value={preset}\n          options={[...Object.keys(EASING_PRESETS).map((k) => ({ value: k, label: k })), ...(preset === 'custom' ? [{ value: 'custom', label: 'Custom' }] : [])]}\n          onChange={(k) => k in EASING_PRESETS && onChange(EASING_PRESETS[k].slice() as Bezier)}\n          className=\"w-28 shrink-0\"\n        />\n        <input\n          value={draft ?? value.join(', ')}\n          aria-label={`${label}, as cubic-bezier numbers`}\n          aria-invalid={invalid || undefined}\n          spellCheck={false}\n          autoComplete=\"off\"\n          onChange={(e) => {\n            setDraft(e.target.value);\n            const curve = parseBezier(e.target.value);\n            if (curve) onChange(clampBezier(curve));\n          }}\n          onBlur={() => setDraft(null)}\n          className=\"border-border bg-background focus-visible:ring-ring/50 aria-invalid:border-destructive h-7 w-full min-w-0 rounded-md border px-2 font-mono text-xs tabular-nums outline-none focus-visible:ring-[3px]\"\n        />\n      </div>\n      <MotionPreview easing={css} duration={duration} label={label} />\n    </div>\n  );\n}\n\n/**\n * A dot that runs the track with the given easing: on every change, and on Play. With reduced motion it doesn't move,\n * and says so.\n */\nexport function MotionPreview({ easing, duration, label, className }: { easing: string; duration: number; label: string; className?: string }) {\n  const reduced = useReducedMotion();\n  const runner = React.useRef<HTMLDivElement>(null);\n  const play = React.useCallback(() => {\n    if (reduced) return;\n    runner.current?.animate([{ transform: 'translateX(0)' }, { transform: 'translateX(100%)' }], { duration, easing, fill: 'forwards' });\n  }, [reduced, duration, easing]);\n  React.useEffect(play, [play]);\n  if (reduced) return <p className={cn('text-muted-foreground text-xs', className)}>The preview is still: your system asks for less motion.</p>;\n  return (\n    <div className={cn('flex items-center gap-2', className)}>\n      <div className=\"bg-muted relative h-5 min-w-0 flex-1 overflow-hidden rounded-full\">\n        <div ref={runner} className=\"absolute inset-y-0 left-0 w-[calc(100%-1.25rem)]\">\n          <span className=\"bg-foreground absolute top-0.5 left-0.5 size-4 rounded-full\" />\n        </div>\n      </div>\n      <button\n        type=\"button\"\n        onClick={play}\n        aria-label={`Play ${label.toLowerCase()}`}\n        className=\"text-muted-foreground hover:text-foreground hover:bg-accent focus-visible:ring-ring/50 inline-flex size-6 shrink-0 items-center justify-center rounded-md outline-none focus-visible:ring-[3px]\">\n        <PlayIcon className=\"size-3.5\" aria-hidden />\n      </button>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/controls/curve-editor.tsx"
    }
  ],
  "meta": {
    "exports": [
      "CurveEditor",
      "MotionPreview"
    ],
    "api": "<CurveEditor label value={[x1,y1,x2,y2]} onChange duration? />"
  },
  "categories": [
    "controls"
  ],
  "type": "registry:component"
}