{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "prize-wheel",
  "title": "Prize Wheel",
  "author": "VenusUI",
  "description": "A spin-to-win wheel that eases to a stop while its pointer ticks past each segment.",
  "files": [
    {
      "path": "registry/default/ui/prize-wheel.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\nexport interface WheelSegment {\n  label: string\n  /** Any CSS color. */\n  color: string\n}\n\ninterface PrizeWheelProps extends React.ComponentProps<\"div\"> {\n  segments: WheelSegment[]\n  /** Index of the segment to land on. Random when unset. */\n  pick?: number\n  onResult?: (segment: WheelSegment, index: number) => void\n  /** Spin duration in ms. */\n  duration?: number\n}\n\nconst SIZE = 200\nconst C = SIZE / 2\nconst R = SIZE / 2 - 6\n\nfunction slice(index: number, count: number) {\n  const a0 = ((index / count) * 360 - 90) * (Math.PI / 180)\n  const a1 = (((index + 1) / count) * 360 - 90) * (Math.PI / 180)\n  const p = (a: number) => `${(C + R * Math.cos(a)).toFixed(2)} ${(C + R * Math.sin(a)).toFixed(2)}`\n  return `M ${C} ${C} L ${p(a0)} A ${R} ${R} 0 ${count <= 2 ? 1 : 0} 1 ${p(a1)} Z`\n}\n\n/**\n * A spin-to-win wheel. \"Spin\" turns it several times and eases to a stop;\n * the pointer ticks as it passes each segment. The result is announced.\n * With reduced motion it shows the result straight away.\n */\nfunction PrizeWheel({ segments, pick, onResult, duration = 5200, className, ...props }: PrizeWheelProps) {\n  const wheelRef = React.useRef<HTMLDivElement>(null)\n  const pointerRef = React.useRef<HTMLSpanElement>(null)\n  const [rotation, setRotation] = React.useState(0)\n  const [spinning, setSpinning] = React.useState(false)\n  const [result, setResult] = React.useState<number | null>(null)\n  const count = segments.length\n  const step = 360 / count\n\n  // While spinning, tick the pointer each time a segment edge passes it.\n  React.useEffect(() => {\n    if (!spinning) return\n    let frame = 0\n    let lastEdge = -1\n    const tick = () => {\n      const wheel = wheelRef.current\n      if (wheel) {\n        const matrix = new DOMMatrixReadOnly(getComputedStyle(wheel).transform)\n        const angle = ((Math.atan2(matrix.b, matrix.a) * 180) / Math.PI + 360) % 360\n        const edge = Math.floor(angle / step)\n        if (edge !== lastEdge && lastEdge !== -1) {\n          pointerRef.current?.animate([{ transform: \"rotate(-22deg)\" }, { transform: \"rotate(0deg)\" }], { duration: 140, easing: \"ease-out\" })\n        }\n        lastEdge = edge\n      }\n      frame = requestAnimationFrame(tick)\n    }\n    frame = requestAnimationFrame(tick)\n    return () => cancelAnimationFrame(frame)\n  }, [spinning, step])\n\n  const spin = () => {\n    if (spinning || count === 0) return\n    const target = pick ?? Math.floor(Math.random() * count)\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches\n    // Land the middle of the target segment under the pointer (at the top), with a little jitter.\n    const jitter = (Math.random() - 0.5) * step * 0.6\n    const landing = 360 - (target * step + step / 2) + jitter\n    const base = rotation - (rotation % 360)\n    setResult(null)\n    setRotation(base + (reduced ? 360 : 360 * 6) + landing)\n    setSpinning(!reduced)\n    const finish = () => {\n      setSpinning(false)\n      setResult(target)\n      onResult?.(segments[target], target)\n    }\n    if (reduced) finish()\n    else setTimeout(finish, duration)\n  }\n\n  return (\n    <div data-slot=\"prize-wheel\" className={cn(\"flex w-full max-w-xs flex-col items-center gap-5\", className)} {...props}>\n      <div className=\"relative aspect-square w-full\">\n        {/* Pointer */}\n        <span\n          ref={pointerRef}\n          aria-hidden=\"true\"\n          className=\"absolute -top-2 left-1/2 z-10 -ml-3 h-9 w-6 origin-[50%_20%] drop-shadow-md\"\n        >\n          <svg viewBox=\"0 0 24 36\" className=\"size-full\">\n            <path d=\"M12 36 L2 8 A10 10 0 1 1 22 8 Z\" className=\"fill-foreground\" />\n            <circle cx=\"12\" cy=\"9\" r=\"4\" className=\"fill-background\" />\n          </svg>\n        </span>\n        {/* Wheel */}\n        <div\n          ref={wheelRef}\n          aria-hidden=\"true\"\n          className=\"size-full rounded-full shadow-[0_20px_40px_-20px_rgb(0_0_0/0.6)] motion-reduce:transition-none\"\n          style={{ transform: `rotate(${rotation}deg)`, transition: `transform ${duration}ms cubic-bezier(0.12, 0.7, 0.12, 1)` }}\n        >\n          <svg viewBox={`0 0 ${SIZE} ${SIZE}`} className=\"size-full\">\n            <circle cx={C} cy={C} r={C} className=\"fill-neutral-900 dark:fill-neutral-700\" />\n            {segments.map((s, i) => (\n              <g key={s.label + i}>\n                <path d={slice(i, count)} fill={s.color} stroke=\"white\" strokeOpacity={0.6} strokeWidth={1} />\n                <text\n                  x={C}\n                  y={C - R * 0.62}\n                  textAnchor=\"middle\"\n                  dominantBaseline=\"middle\"\n                  transform={`rotate(${i * step + step / 2} ${C} ${C})`}\n                  className=\"fill-white text-[9px] font-bold\"\n                  style={{ paintOrder: \"stroke\", stroke: \"rgb(0 0 0 / 0.25)\", strokeWidth: 2 }}\n                >\n                  {s.label}\n                </text>\n              </g>\n            ))}\n            {/* Pegs on the rim */}\n            {segments.map((_, i) => {\n              const a = ((i / count) * 360 - 90) * (Math.PI / 180)\n              return <circle key={i} cx={C + (R - 3) * Math.cos(a)} cy={C + (R - 3) * Math.sin(a)} r={2.2} className=\"fill-amber-200\" />\n            })}\n          </svg>\n        </div>\n        {/* Hub with the spin button */}\n        <button\n          type=\"button\"\n          onClick={spin}\n          disabled={spinning}\n          className=\"absolute top-1/2 left-1/2 grid size-[26%] -translate-1/2 place-items-center rounded-full bg-foreground text-sm font-bold text-background shadow-lg ring-4 ring-background outline-none focus-visible:ring-ring/50 disabled:cursor-wait\"\n        >\n          {spinning ? \"…\" : \"Spin\"}\n        </button>\n      </div>\n      <p aria-live=\"polite\" className=\"min-h-6 text-center font-medium\">\n        {result !== null ? `You won: ${segments[result].label}!` : spinning ? \"Spinning…\" : \"\"}\n      </p>\n    </div>\n  )\n}\n\nexport { PrizeWheel }\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "version": "0.1.0"
  },
  "categories": [
    "interactive",
    "animations"
  ],
  "type": "registry:ui"
}