{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "newtons-cradle",
  "title": "Newton's Cradle",
  "author": "VenusUI",
  "description": "A Newton's cradle: lift an end ball and the swing passes back and forth until it rests.",
  "files": [
    {
      "path": "registry/default/ui/newtons-cradle.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\ninterface NewtonsCradleProps extends React.ComponentProps<\"div\"> {\n  balls?: number\n  /** Seconds before it comes to rest. */\n  duration?: number\n}\n\nconst SWING = 34\nconst PERIOD = 1.05\n\n/**\n * A Newton's cradle. Press \"Lift and let go\" (or click an end ball): the\n * outer balls trade the swing back and forth, losing a little height each\n * time, and click to rest.\n */\nfunction NewtonsCradle({ balls = 5, duration = 7, className, ...props }: NewtonsCradleProps) {\n  const [angles, setAngles] = React.useState({ left: 0, right: 0 })\n  const [swinging, setSwinging] = React.useState(false)\n  const frame = React.useRef(0)\n\n  React.useEffect(() => () => cancelAnimationFrame(frame.current), [])\n\n  const release = (side: \"left\" | \"right\") => {\n    cancelAnimationFrame(frame.current)\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return\n    setSwinging(true)\n    let start: number | undefined\n    const step = (now: number) => {\n      start ??= now\n      const t = (now - start) / 1000\n      const amplitude = SWING * Math.exp(-t / (duration / 3))\n      // Each half period one end ball is out: cos from the top down to the stack.\n      const phase = (t % PERIOD) / PERIOD\n      const out = Math.sin(phase * Math.PI) * amplitude\n      const firstHalf = phase < 0.5\n      const leftOut = side === \"left\" ? (firstHalf ? 0 : out) : firstHalf ? out : 0\n      const rightOut = side === \"left\" ? (firstHalf ? out : 0) : firstHalf ? 0 : out\n      setAngles({ left: leftOut, right: rightOut })\n      if (t < duration && amplitude > 0.4) frame.current = requestAnimationFrame(step)\n      else {\n        setAngles({ left: 0, right: 0 })\n        setSwinging(false)\n      }\n    }\n    // Start with the chosen ball lifted.\n    setAngles(side === \"left\" ? { left: SWING, right: 0 } : { left: 0, right: SWING })\n    frame.current = requestAnimationFrame(step)\n  }\n\n  return (\n    <div data-slot=\"newtons-cradle\" className={cn(\"flex w-80 max-w-full flex-col items-center\", className)} {...props}>\n      <div className=\"relative h-56 w-full\">\n        <span aria-hidden=\"true\" className=\"absolute inset-x-4 top-2 h-2 rounded-full bg-linear-to-b from-neutral-300 to-neutral-500 shadow\" />\n        <span aria-hidden=\"true\" className=\"absolute top-2 left-4 h-52 w-1.5 rounded-full bg-neutral-400\" />\n        <span aria-hidden=\"true\" className=\"absolute top-2 right-4 h-52 w-1.5 rounded-full bg-neutral-400\" />\n        <span aria-hidden=\"true\" className=\"absolute inset-x-0 bottom-0 h-3 rounded-full bg-linear-to-b from-neutral-700 to-neutral-900\" />\n        <div className=\"absolute inset-x-0 top-3 flex justify-center\">\n          {Array.from({ length: balls }, (_, i) => {\n            const end = i === 0 ? \"left\" : i === balls - 1 ? \"right\" : null\n            const angle = i === 0 ? angles.left : i === balls - 1 ? -angles.right : 0\n            const Ball = end ? \"button\" : \"span\"\n            return (\n              <span key={i} className=\"flex origin-top flex-col items-center\" style={{ rotate: `${angle}deg` }}>\n                <span aria-hidden=\"true\" className=\"h-36 w-px bg-neutral-500\" />\n                <Ball\n                  {...(end ? { type: \"button\" as const, onClick: () => release(end), \"aria-label\": `Lift the ${end} ball and let go`, disabled: swinging } : { \"aria-hidden\": true })}\n                  className=\"-mt-1 size-10 rounded-full bg-[radial-gradient(circle_at_35%_30%,#ffffff,#d4d4d8_30%,#71717a_75%,#3f3f46)] shadow-[0_6px_10px_-4px_rgb(0_0_0/0.5)] outline-none focus-visible:ring-3 focus-visible:ring-ring/60\"\n                />\n              </span>\n            )\n          })}\n        </div>\n      </div>\n      <button type=\"button\" onClick={() => release(\"left\")} disabled={swinging} className=\"mt-4 rounded-md bg-primary px-3 py-1.5 text-sm font-medium text-primary-foreground outline-none hover:bg-primary/90 focus-visible:ring-3 focus-visible:ring-ring/50 disabled:opacity-60\">\n        Lift and let go\n      </button>\n      <p role=\"status\" className=\"mt-2 min-h-4 text-xs text-muted-foreground\">\n        {swinging ? \"Click… click… click…\" : \"\"}\n      </p>\n    </div>\n  )\n}\n\nexport { NewtonsCradle }\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "version": "0.1.0"
  },
  "categories": [
    "animations",
    "interactive"
  ],
  "type": "registry:ui"
}