{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"peekaboo-stars","type":"registry:ui","title":"Peekaboo Stars","description":"Star rating with a little character that peeks over the card, rising and grinning wider the higher you hover. Arrow keys, Home/End and digits work too.","dependencies":["motion"],"registryDependencies":[],"files":[{"path":"registry/ui/peekaboo-stars.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useState, type KeyboardEvent } from \"react\";\nimport { MotionConfig, motion } from \"motion/react\";\n\nexport interface PeekabooStarsProps {\n  /** Controlled rating, 0 to `max`. */\n  value?: number;\n  /** Initial rating when uncontrolled. */\n  defaultValue?: number;\n  /** Number of stars. */\n  max?: number;\n  /** Called with the new rating. */\n  onChange?: (value: number) => void;\n  /** One word per star, shown under the row. */\n  labels?: string[];\n  /** Star fill color. */\n  color?: string;\n  /** Color of the peeking character. */\n  characterColor?: string;\n  /** Accessible name of the rating. */\n  label?: string;\n  className?: string;\n}\n\nconst STAR = \"M12 2.8l2.8 5.7 6.3.9-4.6 4.4 1.1 6.2L12 17.1 6.4 20l1.1-6.2L2.9 9.4l6.3-.9z\";\nconst pop = { type: \"spring\", stiffness: 520, damping: 14 } as const;\nconst peek = { type: \"spring\", stiffness: 260, damping: 13 } as const;\n\nexport function PeekabooStars({\n  value,\n  defaultValue = 0,\n  max = 5,\n  onChange,\n  labels = [\"Awful\", \"Meh\", \"Okay\", \"Good\", \"Love it!\"],\n  color = \"#facc15\",\n  characterColor = \"#fb923c\",\n  label = \"Rating\",\n  className,\n}: PeekabooStarsProps) {\n  const [inner, setInner] = useState(defaultValue);\n  const [hover, setHover] = useState<number | null>(null);\n  const current = value ?? inner;\n  const shown = hover ?? current;\n  const mood = shown / max; // 0 sad .. 1 delighted\n\n  const set = (v: number) => {\n    const next = Math.max(0, Math.min(max, v));\n    if (value === undefined) setInner(next);\n    onChange?.(next);\n  };\n\n  const onKey = (e: KeyboardEvent) => {\n    const map: Record<string, number> = {\n      ArrowRight: current + 1,\n      ArrowUp: current + 1,\n      ArrowLeft: current - 1,\n      ArrowDown: current - 1,\n      Home: 0,\n      End: max,\n    };\n    const next = e.key in map ? map[e.key] : /^\\d$/.test(e.key) ? Number(e.key) : null;\n    if (next === null) return;\n    e.preventDefault();\n    set(next);\n  };\n\n  const word = shown === 0 ? \"Rate it\" : (labels[Math.round(mood * labels.length) - 1] ?? `${shown}/${max}`);\n  // Mouth: frown -> grin. Brows: worried -> raised. Eyes squint shut when delighted.\n  const curve = -9 + mood * 22;\n  const brow = shown === 0 ? 0 : 18 * (1 - mood); // inner ends lift when sad\n  const happy = mood >= 0.8;\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div className={`relative inline-flex flex-col items-center pt-[4.5rem] ${className ?? \"\"}`}>\n        {/* The character lives in a clipped strip above the card and peeks over its edge. */}\n        <div aria-hidden className=\"pointer-events-none absolute inset-x-0 top-0 h-[5.25rem] overflow-hidden\">\n          <motion.svg\n            viewBox=\"0 0 100 100\"\n            className=\"absolute bottom-0 left-1/2 size-20 -translate-x-1/2\"\n            initial={false}\n            animate={{ y: 28 - mood * 28 + (shown === 0 ? 5 : 0), rotate: (mood - 0.5) * 8 }}\n            transition={peek}\n          >\n            <circle cx=\"22\" cy=\"20\" r=\"9\" fill={characterColor} />\n            <circle cx=\"78\" cy=\"20\" r=\"9\" fill={characterColor} />\n            <rect x=\"8\" y=\"12\" width=\"84\" height=\"110\" rx=\"42\" fill={characterColor} />\n            <motion.g initial={false} animate={{ x: (mood - 0.5) * 6 }} transition={peek}>\n              <motion.line\n                x1=\"29\" y1=\"31\" x2=\"43\" y2=\"31\" stroke=\"#1c1917\" strokeWidth=\"3.5\" strokeLinecap=\"round\"\n                initial={false} animate={{ rotate: -brow, y: happy ? -3 : 0 }} transition={peek}\n              />\n              <motion.line\n                x1=\"57\" y1=\"31\" x2=\"71\" y2=\"31\" stroke=\"#1c1917\" strokeWidth=\"3.5\" strokeLinecap=\"round\"\n                initial={false} animate={{ rotate: brow, y: happy ? -3 : 0 }} transition={peek}\n              />\n              <motion.ellipse cx=\"36\" cy=\"44\" rx=\"5\" fill=\"#1c1917\" initial={false} animate={{ ry: happy ? 1.8 : 6 }} transition={pop} />\n              <motion.ellipse cx=\"64\" cy=\"44\" rx=\"5\" fill=\"#1c1917\" initial={false} animate={{ ry: happy ? 1.8 : 6 }} transition={pop} />\n            </motion.g>\n            <motion.circle cx=\"24\" cy=\"58\" r=\"7\" fill=\"#f43f5e\" initial={false} animate={{ opacity: mood > 0.55 ? 0.45 : 0 }} />\n            <motion.circle cx=\"76\" cy=\"58\" r=\"7\" fill=\"#f43f5e\" initial={false} animate={{ opacity: mood > 0.55 ? 0.45 : 0 }} />\n            <motion.path\n              stroke=\"#1c1917\"\n              strokeWidth=\"4\"\n              strokeLinecap=\"round\"\n              fill={happy ? \"#7f1d1d\" : \"none\"}\n              initial={false}\n              animate={{ d: `M38 62 Q50 ${62 + curve} 62 62` }}\n              transition={peek}\n            />\n          </motion.svg>\n        </div>\n\n        <div\n          role=\"slider\"\n          tabIndex={0}\n          aria-label={label}\n          aria-valuemin={0}\n          aria-valuemax={max}\n          aria-valuenow={current}\n          aria-valuetext={current === 0 ? \"No rating\" : `${current} of ${max} stars`}\n          onKeyDown={onKey}\n          onPointerLeave={() => setHover(null)}\n          className=\"relative z-10 flex flex-col items-center gap-2 rounded-2xl border border-border bg-card px-5 pb-3 pt-4 shadow-xl outline-none focus-visible:ring-[3px] focus-visible:ring-ring/60\"\n        >\n          <div className=\"flex gap-1\">\n            {Array.from({ length: max }, (_, i) => {\n              const lit = i < shown;\n              return (\n                <motion.svg\n                  key={i}\n                  aria-hidden\n                  viewBox=\"0 0 24 24\"\n                  className=\"size-9 cursor-pointer\"\n                  onPointerEnter={() => setHover(i + 1)}\n                  onClick={() => set(current === i + 1 ? 0 : i + 1)}\n                  initial={false}\n                  animate={{ scale: lit ? 1.12 : 1, rotate: lit ? (i % 2 ? 8 : -8) : 0, y: lit && hover !== null ? -2 : 0 }}\n                  whileTap={{ scale: 0.8 }}\n                  transition={{ ...pop, delay: lit ? i * 0.03 : 0 }}\n                >\n                  <path\n                    d={STAR}\n                    strokeWidth=\"1.6\"\n                    strokeLinejoin=\"round\"\n                    style={{\n                      fill: lit ? color : \"transparent\",\n                      stroke: lit ? color : \"var(--muted-foreground)\",\n                      transition: \"fill .15s, stroke .15s\",\n                    }}\n                  />\n                </motion.svg>\n              );\n            })}\n          </div>\n          <span aria-hidden className=\"h-4 text-xs font-medium text-muted-foreground\">\n            {word}\n          </span>\n        </div>\n      </div>\n    </MotionConfig>\n  );\n}\n"}]}