{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"shy-button","type":"registry:ui","title":"Shy Button","description":"A playful yes/no prompt whose No button springs away from the cursor, leaning into each hop, until it gets tired. Keyboard users can always press it.","dependencies":["motion"],"registryDependencies":[],"files":[{"path":"registry/ui/shy-button.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useRef, useState, type PointerEvent } from \"react\";\nimport { AnimatePresence, MotionConfig, animate, motion, useMotionValue, useReducedMotion, useTransform, useVelocity } from \"motion/react\";\n\nexport interface ShyButtonProps {\n  /** The question above the buttons. */\n  question?: string;\n  yesLabel?: string;\n  noLabel?: string;\n  /** Px around the No button in which the pointer makes it flee. */\n  radius?: number;\n  /** Dodges before No gives up and lets itself be clicked. 0 = never gives up. */\n  maxDodges?: number;\n  /** Yes button and celebration color. */\n  color?: string;\n  onYes?: () => void;\n  onNo?: () => void;\n  className?: string;\n}\n\nconst hop = { type: \"spring\", stiffness: 380, damping: 17, mass: 0.8 } as const;\n\nexport function ShyButton({\n  question = \"Deploy on a Friday afternoon?\",\n  yesLabel = \"Yes\",\n  noLabel = \"No\",\n  radius = 80,\n  maxDodges = 8,\n  color = \"#f472b6\",\n  onYes,\n  onNo,\n  className,\n}: ShyButtonProps) {\n  const root = useRef<HTMLDivElement>(null);\n  const no = useRef<HTMLButtonElement>(null);\n  const fledAt = useRef(0);\n  const [dodges, setDodges] = useState(0);\n  const [answer, setAnswer] = useState<\"yes\" | \"no\" | null>(null);\n  const reduce = useReducedMotion();\n\n  const x = useMotionValue(0);\n  const y = useMotionValue(0);\n  const vx = useVelocity(x);\n  const vy = useVelocity(y);\n  // Lean into the direction of travel and stretch with speed: cartoon follow-through.\n  const rotate = useTransform(vx, [-1600, 1600], [-16, 16]);\n  const scaleX = useTransform(() => 1 + Math.min(Math.hypot(vx.get(), vy.get()), 2400) / 6000);\n  const scaleY = useTransform(scaleX, (s) => 2 - s);\n\n  const gaveUp = maxDodges > 0 && dodges >= maxDodges;\n\n  const flee = (px: number, py: number) => {\n    const btn = no.current;\n    const box = root.current;\n    if (!btn || !box || gaveUp || answer) return false;\n    const b = btn.getBoundingClientRect();\n    const r = box.getBoundingClientRect();\n    const cx = b.left + b.width / 2;\n    const cy = b.top + b.height / 2;\n    if (Math.hypot(cx - px, cy - py) > radius) return false;\n\n    // Untransformed centre, so targets can be converted back into x/y offsets.\n    const bx = cx - x.get();\n    const by = cy - y.get();\n    const angle = Math.atan2(cy - py, cx - px) + (Math.random() - 0.5) * 1.3;\n    const jump = radius + 30 + Math.random() * 50;\n    let tx = cx + Math.cos(angle) * jump;\n    let ty = cy + Math.sin(angle) * jump;\n    const hw = b.width / 2 + 10;\n    const hh = b.height / 2 + 10;\n    // Cornered against a wall: bolt to the far side of the field instead.\n    if (tx < r.left + hw || tx > r.right - hw)\n      tx = px < r.left + r.width / 2 ? r.right - hw - Math.random() * 40 : r.left + hw + Math.random() * 40;\n    if (ty < r.top + hh || ty > r.bottom - hh)\n      ty = py < r.top + r.height / 2 ? r.bottom - hh - Math.random() * 24 : r.top + hh + Math.random() * 24;\n\n    if (reduce) {\n      x.set(tx - bx);\n      y.set(ty - by);\n    } else {\n      animate(x, tx - bx, hop);\n      animate(y, ty - by, hop);\n    }\n    fledAt.current = performance.now();\n    setDodges((n) => n + 1);\n    return true;\n  };\n\n  const reset = () => {\n    setAnswer(null);\n    setDodges(0);\n    animate(x, 0, hop);\n    animate(y, 0, hop);\n  };\n\n  const onMove = (e: PointerEvent<HTMLDivElement>) => {\n    if (e.pointerType === \"mouse\") flee(e.clientX, e.clientY);\n  };\n\n  const yesScale = 1 + Math.min(dodges, 8) * 0.06;\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div\n        ref={root}\n        onPointerMove={onMove}\n        className={`relative h-64 w-[26rem] max-w-full select-none overflow-hidden rounded-2xl border border-border bg-card text-foreground shadow-xl ${className ?? \"\"}`}\n      >\n        <p className=\"absolute inset-x-6 top-8 text-center text-lg font-semibold tracking-tight\">{question}</p>\n        <p className=\"absolute inset-x-6 top-16 h-5 text-center text-xs text-muted-foreground\" aria-live=\"polite\">\n          {answer ? \"\" : gaveUp ? \"Okay, okay. It stopped running.\" : dodges > 2 ? `Dodged ${dodges} times` : \"\"}\n        </p>\n\n        <AnimatePresence mode=\"wait\" initial={false}>\n          {answer ? (\n            <motion.div\n              key=\"answer\"\n              initial={{ opacity: 0, scale: 0.8 }}\n              animate={{ opacity: 1, scale: 1 }}\n              exit={{ opacity: 0, scale: 0.8 }}\n              transition={{ type: \"spring\", stiffness: 420, damping: 22 }}\n              className=\"absolute inset-x-0 top-[42%] grid justify-items-center gap-3\"\n              role=\"status\"\n            >\n              <span className=\"relative text-2xl font-bold\" style={{ color: answer === \"yes\" ? color : undefined }}>\n                {answer === \"yes\" ? \"Brave. Let's ship it.\" : \"Wise choice.\"}\n                {answer === \"yes\" &&\n                  Array.from({ length: 14 }, (_, i) => {\n                    const a = (i / 14) * Math.PI * 2;\n                    return (\n                      <motion.span\n                        key={i}\n                        aria-hidden\n                        className=\"absolute left-1/2 top-1/2 size-2 rounded-full\"\n                        style={{ background: i % 2 ? color : \"var(--foreground)\" }}\n                        initial={{ x: 0, y: 0, scale: 1, opacity: 1 }}\n                        animate={{ x: Math.cos(a) * (90 + (i % 3) * 25), y: Math.sin(a) * (50 + (i % 3) * 18), scale: 0, opacity: 0 }}\n                        transition={{ duration: 0.8, ease: [0.2, 0.7, 0.3, 1] }}\n                      />\n                    );\n                  })}\n              </span>\n              <button\n                type=\"button\"\n                onClick={reset}\n                className=\"rounded-full px-3 py-1 text-xs text-muted-foreground outline-none hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/60\"\n              >\n                Ask again\n              </button>\n            </motion.div>\n          ) : (\n            <motion.div key=\"buttons\" className=\"absolute inset-0\" exit={{ opacity: 0 }} transition={{ duration: 0.15 }}>\n              <motion.button\n                type=\"button\"\n                onClick={() => {\n                  setAnswer(\"yes\");\n                  onYes?.();\n                }}\n                animate={{ scale: yesScale }}\n                whileTap={{ scale: yesScale * 0.92 }}\n                transition={{ type: \"spring\", stiffness: 500, damping: 15 }}\n                className=\"absolute left-[30%] top-[62%] -ml-12 -mt-6 h-12 w-24 rounded-full font-semibold text-white outline-none focus-visible:ring-[3px] focus-visible:ring-ring/70 focus-visible:ring-offset-2 focus-visible:ring-offset-card\"\n                style={{ background: color, boxShadow: `inset 0 1px 0 rgb(255 255 255/.35), 0 10px 26px -10px ${color}` }}\n              >\n                {yesLabel}\n              </motion.button>\n              <motion.button\n                ref={no}\n                type=\"button\"\n                onPointerDown={(e) => {\n                  // Touch has no hover, so it flees from the tap itself.\n                  if (e.pointerType !== \"mouse\") flee(e.clientX, e.clientY);\n                }}\n                onClick={() => {\n                  if (performance.now() - fledAt.current < 400) return;\n                  setAnswer(\"no\");\n                  onNo?.();\n                }}\n                style={{ x, y, rotate: gaveUp ? 8 : rotate, scaleX, scaleY }}\n                className=\"absolute left-[70%] top-[62%] -ml-12 -mt-6 h-12 w-24 rounded-full border border-border bg-muted font-semibold text-foreground outline-none transition-[opacity] focus-visible:ring-[3px] focus-visible:ring-ring/70 focus-visible:ring-offset-2 focus-visible:ring-offset-card\"\n              >\n                {gaveUp ? `${noLabel}…` : noLabel}\n              </motion.button>\n            </motion.div>\n          )}\n        </AnimatePresence>\n      </div>\n    </MotionConfig>\n  );\n}\n"}]}