{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"star-rating","type":"registry:ui","title":"Star Rating","description":"Stars that fill in a stagger on hover, pop on click and follow the arrow keys.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/star-rating.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { motion, useReducedMotion } from \"motion/react\";\nimport { useId, useRef, useState, type KeyboardEvent } from \"react\";\n\ntype StarRatingProps = {\n  value?: number;\n  defaultValue?: number;\n  onChange?: (value: number) => void;\n  /** Number of stars. */\n  max?: number;\n  /** Star size in pixels. */\n  size?: number;\n  color?: string;\n  /** Display only. Accepts fractions, like 4.5. */\n  readOnly?: boolean;\n  /** Accessible name of the group. */\n  label?: string;\n  name?: string;\n  id?: string;\n  className?: string;\n};\n\nconst STAR =\n  \"M12 2.5 14.53 9.02 21.51 9.41 16.09 13.83 17.88 20.59 12 16.8 6.12 20.59 7.91 13.83 2.49 9.41 9.47 9.02Z\";\nconst STAGGER = 0.035;\n\nfunction Star({ fill, size, color, delay, reduce }: { fill: number; size: number; color: string; delay: number; reduce: boolean }) {\n  return (\n    <span className=\"relative block\" style={{ width: size, height: size }}>\n      <svg viewBox=\"0 0 24 24\" width={size} height={size} className=\"absolute inset-0\" aria-hidden=\"true\">\n        <path d={STAR} fill=\"#171412\" stroke=\"#3d3631\" strokeWidth={1.4} strokeLinejoin=\"round\" />\n      </svg>\n      <motion.span\n        className=\"absolute inset-y-0 left-0 overflow-hidden\"\n        initial={false}\n        animate={{ width: `${fill * 100}%` }}\n        transition={reduce ? { duration: 0 } : { duration: 0.18, ease: \"easeOut\", delay }}\n      >\n        <svg viewBox=\"0 0 24 24\" width={size} height={size} className=\"max-w-none\" aria-hidden=\"true\">\n          <path d={STAR} fill={color} stroke={color} strokeWidth={1.4} strokeLinejoin=\"round\" />\n        </svg>\n      </motion.span>\n    </span>\n  );\n}\n\nexport function StarRating({\n  value,\n  defaultValue = 0,\n  onChange,\n  max = 5,\n  size = 28,\n  color = \"#ffc24a\",\n  readOnly = false,\n  label = \"Rating\",\n  name,\n  id,\n  className = \"\",\n}: StarRatingProps) {\n  const autoId = useId();\n  const groupId = id ?? autoId;\n  const reduce = Boolean(useReducedMotion());\n  const [inner, setInner] = useState(defaultValue);\n  const current = Math.max(0, Math.min(max, value ?? inner));\n  const [preview, setPreview] = useState<number | null>(null);\n  // The value shown before the last change, so the stagger runs outward from it.\n  const [from, setFrom] = useState(current);\n  const [pop, setPop] = useState({ index: -1, n: 0 });\n  const stars = useRef<(HTMLButtonElement | null)[]>([]);\n\n  const shown = preview ?? current;\n  const rounded = Math.round(current);\n  const tabbable = rounded > 0 ? rounded - 1 : 0;\n\n  function show(next: number | null) {\n    if (next === preview) return;\n    setFrom(shown);\n    setPreview(next);\n  }\n\n  function commit(next: number) {\n    setFrom(shown);\n    setPreview(null);\n    setPop((p) => ({ index: next - 1, n: p.n + 1 }));\n    if (value === undefined) setInner(next);\n    onChange?.(next);\n  }\n\n  function onKeyDown(e: KeyboardEvent<HTMLDivElement>) {\n    let next: number | null = null;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\") next = Math.min(max, rounded + 1);\n    else if (e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") next = Math.max(1, rounded - 1);\n    else if (e.key === \"Home\") next = 1;\n    else if (e.key === \"End\") next = max;\n    else if (/^[1-9]$/.test(e.key) && Number(e.key) <= max) next = Number(e.key);\n    if (next === null) return;\n    e.preventDefault();\n    if (next !== rounded) commit(next);\n    stars.current[next - 1]?.focus();\n  }\n\n  function delayFor(i: number) {\n    if (reduce) return 0;\n    return shown >= from ? Math.max(0, i - Math.floor(from)) * STAGGER : Math.max(0, Math.ceil(from) - 1 - i) * STAGGER;\n  }\n\n  const hidden = name ? <input type=\"hidden\" name={name} value={current} /> : null;\n\n  if (readOnly) {\n    return (\n      <div id={groupId} role=\"img\" aria-label={`${label}: ${current} out of ${max}`} className={`inline-flex gap-1 ${className}`}>\n        {Array.from({ length: max }, (_, i) => (\n          <Star key={i} fill={Math.max(0, Math.min(1, current - i))} size={size} color={color} delay={0} reduce />\n        ))}\n        {hidden}\n      </div>\n    );\n  }\n\n  return (\n    <div\n      id={groupId}\n      role=\"radiogroup\"\n      aria-label={label}\n      onKeyDown={onKeyDown}\n      onPointerLeave={() => show(null)}\n      className={`inline-flex ${className}`}\n    >\n      {Array.from({ length: max }, (_, i) => {\n        const popping = pop.index === i && !reduce;\n        return (\n          <motion.button\n            key={i}\n            ref={(el) => {\n              stars.current[i] = el;\n            }}\n            type=\"button\"\n            role=\"radio\"\n            aria-checked={rounded === i + 1}\n            aria-label={`${i + 1} ${i === 0 ? \"star\" : \"stars\"}`}\n            tabIndex={i === tabbable ? 0 : -1}\n            onClick={() => commit(i + 1)}\n            onPointerMove={(e) => {\n              if (e.pointerType === \"mouse\") show(i + 1);\n            }}\n            whileHover={reduce ? undefined : { scale: 1.14 }}\n            whileTap={reduce ? undefined : { scale: 0.9 }}\n            className=\"cursor-pointer rounded-md p-0.5\"\n          >\n            <motion.span\n              key={popping ? `pop-${pop.n}` : \"still\"}\n              className=\"block\"\n              initial={popping ? { scale: 0.55, rotate: -24 } : false}\n              animate={{ scale: 1, rotate: 0 }}\n              transition={{ type: \"spring\", stiffness: 520, damping: 12 }}\n            >\n              <Star fill={Math.max(0, Math.min(1, shown - i))} size={size} color={color} delay={delayFor(i)} reduce={reduce} />\n            </motion.span>\n          </motion.button>\n        );\n      })}\n      {hidden}\n    </div>\n  );\n}\n"}]}