{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "star-rating",
  "type": "registry:component",
  "title": "Star Rating",
  "description": "Interactive star rating with hover preview, half steps and keyboard control.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "micro",
    "rating",
    "stars",
    "input",
    "review"
  ],
  "docs": "https://hyperblur.dev/effects/star-rating",
  "files": [
    {
      "path": "registry/hyperblur/star-rating.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/star-rating.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface StarRatingProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n  value?: number;\n  defaultValue?: number;\n  onChange?: (value: number) => void;\n  max?: number;\n  precision?: 1 | 0.5;\n  size?: number;\n  color?: string;\n  readOnly?: boolean;\n}\n\nconst css = `\n.hb-sra {\n  display: inline-flex;\n  vertical-align: middle;\n  border-radius: 6px;\n  outline: none;\n  touch-action: manipulation;\n  user-select: none;\n  -webkit-tap-highlight-color: transparent;\n}\n.hb-sra[role=\"slider\"] { cursor: pointer; }\n.hb-sra:focus-visible { outline: 2px solid var(--hb-c); outline-offset: 3px; }\n.hb-sra-s {\n  position: relative;\n  flex: none;\n  width: var(--hb-s);\n  height: var(--hb-s);\n}\n.hb-sra-s svg {\n  position: absolute;\n  inset: 0;\n  width: var(--hb-s);\n  height: var(--hb-s);\n  display: block;\n}\n.hb-sra-b { fill: rgba(255, 255, 255, 0.16); }\n.hb-sra-f {\n  position: absolute;\n  top: 0;\n  left: 0;\n  height: 100%;\n  overflow: hidden;\n  transition: width 0.12s ease-out;\n}\n.hb-sra-f svg { fill: var(--hb-c); }\n.hb-sra-s[data-pop] { animation: hb-sra-pop 0.28s cubic-bezier(0.3, 1.6, 0.5, 1); }\n@keyframes hb-sra-pop {\n  0% { transform: scale(1); }\n  40% { transform: scale(1.3); }\n  100% { transform: scale(1); }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-sra-f { transition: none; }\n  .hb-sra-s[data-pop] { animation: none; }\n}\n`;\n\nconst path =\n  \"M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z\";\n\nexport function StarRating({\n  value,\n  defaultValue = 0,\n  onChange,\n  max = 5,\n  precision = 1,\n  size = 24,\n  color = \"#f5b301\",\n  readOnly = false,\n  className = \"\",\n  style,\n  onKeyDown,\n  onPointerMove,\n  onPointerDown,\n  onPointerLeave,\n  onClick,\n  ...rest\n}: StarRatingProps) {\n  const ref = React.useRef<HTMLDivElement>(null);\n  const count = Math.max(1, Math.round(max));\n\n  const snap = (v: number) => {\n    const s = Math.round(v / precision) * precision;\n    return Math.min(count, Math.max(0, s));\n  };\n\n  const [inner, setInner] = React.useState(() => snap(defaultValue));\n  const [hover, setHover] = React.useState<number | null>(null);\n  const [pop, setPop] = React.useState<number | null>(null);\n\n  const controlled = value !== undefined;\n  const current = snap(controlled ? value : inner);\n  const shown = hover ?? current;\n\n  function commit(next: number) {\n    const v = snap(next);\n    if (!controlled) setInner(v);\n    if (v !== current) onChange?.(v);\n    setPop(v > 0 ? Math.ceil(v) - 1 : null);\n  }\n\n  function fromX(clientX: number) {\n    const el = ref.current;\n    if (!el) return current;\n    const r = el.getBoundingClientRect();\n    if (r.width <= 0) return current;\n    const raw = ((clientX - r.left) / r.width) * count;\n    const v = Math.ceil(raw / precision) * precision;\n    return Math.min(count, Math.max(precision, v));\n  }\n\n  function handleMove(e: React.PointerEvent<HTMLDivElement>) {\n    onPointerMove?.(e);\n    if (readOnly) return;\n    setHover(fromX(e.clientX));\n  }\n\n  function handleDown(e: React.PointerEvent<HTMLDivElement>) {\n    onPointerDown?.(e);\n    if (readOnly) return;\n    setHover(fromX(e.clientX));\n  }\n\n  function handleLeave(e: React.PointerEvent<HTMLDivElement>) {\n    onPointerLeave?.(e);\n    setHover(null);\n  }\n\n  function handleClick(e: React.MouseEvent<HTMLDivElement>) {\n    onClick?.(e);\n    if (readOnly) return;\n    if (e.detail === 0) return;\n    commit(fromX(e.clientX));\n  }\n\n  function handleKey(e: React.KeyboardEvent<HTMLDivElement>) {\n    onKeyDown?.(e);\n    if (readOnly || e.defaultPrevented) return;\n    let next: number | null = null;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\") next = current + precision;\n    else if (e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") next = current - precision;\n    else if (e.key === \"Home\") next = 0;\n    else if (e.key === \"End\") next = count;\n    if (next === null) return;\n    e.preventDefault();\n    setHover(null);\n    commit(next);\n  }\n\n  const vars = {\n    \"--hb-s\": `${size}px`,\n    \"--hb-c\": color,\n    ...style,\n  } as React.CSSProperties;\n\n  const popIndex = hover !== null ? Math.ceil(hover) - 1 : pop;\n\n  const a11y: React.HTMLAttributes<HTMLDivElement> = readOnly\n    ? { role: \"img\", \"aria-label\": `Rated ${current} out of ${count}` }\n    : {\n        role: \"slider\",\n        tabIndex: 0,\n        \"aria-label\": \"Rating\",\n        \"aria-valuemin\": 0,\n        \"aria-valuemax\": count,\n        \"aria-valuenow\": current,\n        \"aria-valuetext\": `${current} out of ${count} stars`,\n      };\n\n  return (\n    <div\n      ref={ref}\n      className={`hb-sra ${className}`}\n      style={vars}\n      {...a11y}\n      {...rest}\n      onKeyDown={handleKey}\n      onPointerMove={handleMove}\n      onPointerDown={handleDown}\n      onPointerLeave={handleLeave}\n      onClick={handleClick}\n    >\n      <style href=\"hb-star-rating\" precedence=\"default\">{css}</style>\n      {Array.from({ length: count }, (_, i) => {\n        const fill = Math.min(1, Math.max(0, shown - i));\n        return (\n          <span\n            key={i}\n            className=\"hb-sra-s\"\n            data-pop={popIndex === i ? \"\" : undefined}\n            onAnimationEnd={() => setPop((p) => (p === i ? null : p))}\n            aria-hidden=\"true\"\n          >\n            <svg viewBox=\"0 0 24 24\" className=\"hb-sra-b\">\n              <path d={path} />\n            </svg>\n            <span className=\"hb-sra-f\" style={{ width: `${fill * 100}%` }}>\n              <svg viewBox=\"0 0 24 24\">\n                <path d={path} />\n              </svg>\n            </span>\n          </span>\n        );\n      })}\n    </div>\n  );\n}\n"
    }
  ]
}