{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"star-rating","type":"registry:ui","title":"Star rating","description":"Animated fractional star rating with review count, plus an accessible interactive input mode.","dependencies":["lucide-react","motion"],"registryDependencies":["utils"],"files":[{"path":"registry/items/star-rating/star-rating.tsx","type":"registry:ui","target":"components/facade/star-rating.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { motion, useInView, useReducedMotion } from \"motion/react\";\nimport { Star } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst SIZES = {\n  sm: { star: \"size-3.5\", gap: \"gap-0.5\", text: \"text-xs\" },\n  md: { star: \"size-5\", gap: \"gap-1\", text: \"text-sm\" },\n  lg: { star: \"size-7\", gap: \"gap-1.5\", text: \"text-base\" },\n} as const;\n\nconst EASE = [0.16, 1, 0.3, 1] as const;\n\n/* ------------------------------------------------------------------\n   Display mode\n------------------------------------------------------------------- */\n\nexport interface StarRatingProps {\n  /** Rating value, fractional allowed (e.g. 4.8). Defaults to 5. */\n  value?: number;\n  max?: number;\n  /** Number of reviews. Shown as \"4.8 · 312 Google reviews\". */\n  count?: number;\n  /** Review source shown before \"reviews\", e.g. \"Google\". */\n  source?: string;\n  size?: keyof typeof SIZES;\n  /** Hide the numeric value and count. */\n  starsOnly?: boolean;\n  /** Animate the fill when scrolled into view. */\n  animate?: boolean;\n  className?: string;\n}\n\nexport function StarRating({\n  value = 5,\n  max = 5,\n  count,\n  source,\n  size = \"md\",\n  starsOnly = false,\n  animate = true,\n  className,\n}: StarRatingProps) {\n  const ref = React.useRef<HTMLDivElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-40px 0px\" });\n  const reduce = useReducedMotion();\n  const clamped = Math.max(0, Math.min(max, value));\n  const s = SIZES[size];\n  const shouldAnimate = animate && !reduce;\n  const label = `Rated ${clamped.toFixed(1)} out of ${max}${count ? `, based on ${count.toLocaleString(\"en-US\")} reviews` : \"\"}`;\n\n  return (\n    <div ref={ref} className={cn(\"inline-flex items-center gap-2\", className)} role=\"img\" aria-label={label}>\n      <div className={cn(\"flex items-center\", s.gap)} aria-hidden>\n        {Array.from({ length: max }, (_, i) => {\n          const fill = Math.max(0, Math.min(1, clamped - i));\n          return (\n            <span key={i} className={cn(\"relative inline-block shrink-0\", s.star)}>\n              <Star className={cn(\"absolute inset-0 fill-foreground/15 text-foreground/15\", s.star)} strokeWidth={0} />\n              <motion.span\n                className=\"absolute inset-0 overflow-hidden\"\n                initial={shouldAnimate ? { clipPath: \"inset(0 100% 0 0)\" } : false}\n                animate={\n                  inView || !shouldAnimate\n                    ? { clipPath: `inset(0 ${(1 - fill) * 100}% 0 0)` }\n                    : { clipPath: \"inset(0 100% 0 0)\" }\n                }\n                transition={{ duration: 0.6, delay: shouldAnimate ? 0.08 * i : 0, ease: EASE }}\n              >\n                <Star className={cn(\"fill-star text-star\", s.star)} strokeWidth={0} />\n              </motion.span>\n            </span>\n          );\n        })}\n      </div>\n      {!starsOnly && (\n        <span className={cn(\"flex items-center gap-1.5 whitespace-nowrap\", s.text)} aria-hidden>\n          <span className=\"font-semibold tabular-nums text-foreground\">{clamped.toFixed(1)}</span>\n          {typeof count === \"number\" && (\n            <span className=\"text-muted-foreground\">\n              {count.toLocaleString(\"en-US\")} {source ? `${source} ` : \"\"}\n              {count === 1 ? \"review\" : \"reviews\"}\n            </span>\n          )}\n        </span>\n      )}\n    </div>\n  );\n}\n\n/* ------------------------------------------------------------------\n   Input mode (radio group semantics)\n------------------------------------------------------------------- */\n\nexport interface StarRatingInputProps {\n  value?: number;\n  defaultValue?: number;\n  onChange?: (value: number) => void;\n  max?: number;\n  size?: keyof typeof SIZES;\n  /** Accessible group label. */\n  label?: string;\n  /** Labels per star, shown as hint text. */\n  hints?: string[];\n  name?: string;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst DEFAULT_HINTS = [\"Poor\", \"Fair\", \"Good\", \"Great\", \"Excellent\"];\n\nexport function StarRatingInput({\n  value: valueProp,\n  defaultValue = 0,\n  onChange,\n  max = 5,\n  size = \"lg\",\n  label = \"Your rating\",\n  hints = DEFAULT_HINTS,\n  name,\n  disabled = false,\n  className,\n}: StarRatingInputProps) {\n  const [inner, setInner] = React.useState(defaultValue);\n  const value = valueProp ?? inner;\n  const [hover, setHover] = React.useState<number | null>(null);\n  const refs = React.useRef<Array<HTMLButtonElement | null>>([]);\n  const reduce = useReducedMotion();\n  const s = SIZES[size];\n  const shown = hover ?? value;\n  const groupId = React.useId();\n\n  const commit = (v: number) => {\n    if (disabled) return;\n    if (valueProp === undefined) setInner(v);\n    onChange?.(v);\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent, i: number) => {\n    let next: number | null = null;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\") next = Math.min(max, i + 2);\n    if (e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") next = Math.max(1, i);\n    if (e.key === \"Home\") next = 1;\n    if (e.key === \"End\") next = max;\n    if (next !== null) {\n      e.preventDefault();\n      commit(next);\n      refs.current[next - 1]?.focus();\n    }\n  };\n\n  return (\n    <div className={cn(\"inline-flex flex-col gap-2\", className)}>\n      <div\n        role=\"radiogroup\"\n        aria-label={label}\n        aria-describedby={`${groupId}-hint`}\n        aria-disabled={disabled || undefined}\n        className={cn(\"flex items-center\", s.gap, disabled && \"opacity-50\")}\n        onMouseLeave={() => setHover(null)}\n      >\n        {Array.from({ length: max }, (_, i) => {\n          const n = i + 1;\n          const active = n <= shown;\n          const checked = value === n;\n          const tabbable = value === 0 ? i === 0 : checked;\n          return (\n            <motion.button\n              key={n}\n              ref={(el) => {\n                refs.current[i] = el;\n              }}\n              type=\"button\"\n              role=\"radio\"\n              aria-checked={checked}\n              aria-label={`${n} ${n === 1 ? \"star\" : \"stars\"}${hints[i] ? `, ${hints[i]}` : \"\"}`}\n              tabIndex={tabbable ? 0 : -1}\n              disabled={disabled}\n              onClick={() => commit(n)}\n              onMouseEnter={() => !disabled && setHover(n)}\n              onFocus={() => setHover(null)}\n              onKeyDown={(e) => onKeyDown(e, i)}\n              whileTap={reduce ? undefined : { scale: 0.9 }}\n              className={cn(\n                \"relative rounded-md p-0.5 outline-none transition-colors\",\n                \"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background\",\n                disabled ? \"cursor-not-allowed\" : \"cursor-pointer\",\n              )}\n            >\n              <motion.span\n                className=\"block\"\n                animate={reduce ? undefined : { scale: active && hover !== null ? 1.1 : 1 }}\n                transition={{ type: \"spring\", stiffness: 400, damping: 32 }}\n              >\n                <Star\n                  className={cn(\n                    s.star,\n                    \"transition-colors duration-150\",\n                    active ? \"fill-star text-star\" : \"fill-foreground/15 text-foreground/15\",\n                    hover !== null && active && n > value && \"fill-star/60 text-star/60\",\n                  )}\n                  strokeWidth={0}\n                  aria-hidden\n                />\n              </motion.span>\n            </motion.button>\n          );\n        })}\n      </div>\n      <p id={`${groupId}-hint`} className=\"h-5 text-sm text-muted-foreground\" aria-live=\"polite\">\n        {shown > 0 ? (\n          <span>\n            <span className=\"font-medium text-foreground\">{hints[shown - 1] ?? `${shown} stars`}</span>\n            {hover === null && value > 0 ? \" selected\" : \"\"}\n          </span>\n        ) : (\n          \"Tap a star to rate\"\n        )}\n      </p>\n      {name && <input type=\"hidden\" name={name} value={value || \"\"} />}\n    </div>\n  );\n}\n\nexport default StarRating;\n"}],"categories":["feedback"],"meta":{"tier":"free","demo":"\"use client\";\n\nimport * as React from \"react\";\nimport { CircleCheck } from \"lucide-react\";\nimport { StarRating, StarRatingInput } from \"./star-rating\";\n\nconst breakdown = [\n  { stars: 5, pct: 86 },\n  { stars: 4, pct: 9 },\n  { stars: 3, pct: 3 },\n  { stars: 2, pct: 1 },\n  { stars: 1, pct: 1 },\n];\n\nexport default function Demo() {\n  const [rating, setRating] = React.useState(0);\n\n  return (\n    <div className=\"mx-auto grid w-full max-w-3xl gap-4 md:grid-cols-2\">\n      <section className=\"rounded-[28px] bg-muted p-6 text-foreground sm:p-7\">\n        <p className=\"text-xs font-medium uppercase tracking-wider text-muted-foreground\">Bright Smile Dental, Denver</p>\n        <div className=\"mt-4 flex items-end gap-3\">\n          <span className=\"font-display text-[56px] font-semibold leading-none tracking-[-0.04em]\">4.8</span>\n          <span className=\"pb-1 text-sm text-muted-foreground\">out of 5</span>\n        </div>\n        <StarRating value={4.8} count={312} source=\"Google\" className=\"mt-3\" />\n        <ul className=\"mt-6 space-y-2\">\n          {breakdown.map((b) => (\n            <li key={b.stars} className=\"flex items-center gap-3 text-xs text-muted-foreground\">\n              <span className=\"w-3 tabular-nums\">{b.stars}</span>\n              <span className=\"h-1.5 flex-1 overflow-hidden rounded-full bg-foreground/10\">\n                <span className=\"block h-full rounded-full bg-star\" style={{ width: `${b.pct}%` }} />\n              </span>\n              <span className=\"w-8 text-right tabular-nums\">{b.pct}%</span>\n            </li>\n          ))}\n        </ul>\n      </section>\n\n      <section className=\"flex flex-col rounded-[28px] bg-muted p-6 text-foreground sm:p-7\">\n        <h3 className=\"font-display text-[19px] font-semibold tracking-[-0.02em]\">How was your cleaning with Dr. Patel?</h3>\n        <p className=\"mt-1.5 text-sm text-muted-foreground\">Your feedback helps neighbors in Cherry Creek find great care.</p>\n        <StarRatingInput className=\"mt-6\" value={rating} onChange={setRating} label=\"Rate your visit\" />\n        <div className=\"mt-auto flex items-center justify-between gap-3 pt-6\">\n          <span className=\"flex items-center gap-1.5 text-xs text-muted-foreground\">\n            <CircleCheck className=\"size-3.5 text-success\" aria-hidden />\n            Verified patient\n          </span>\n          <button\n            type=\"button\"\n            disabled={rating === 0}\n            className=\"h-10 rounded-full bg-foreground px-5 text-sm font-medium text-background transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-30\"\n          >\n            Continue\n          </button>\n        </div>\n      </section>\n    </div>\n  );\n}\n"}}