{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rating-stars",
  "title": "Rating Stars",
  "description": "An animated star rating input with half-star precision, keyboard support, a dynamic low/mid/high color range, glow effect, and an optional progress bar.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/rating-stars/rating-stars.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type RatingStarsSize = \"xs\" | \"sm\" | \"md\" | \"lg\" | \"xl\" | \"custom\";\nexport type RatingStarsLabelStyle = \"score\" | \"fraction\" | \"percent\" | \"label\";\nexport type RatingStarsLabelPosition = \"right\" | \"left\" | \"top\" | \"bottom\";\nexport type RatingStarsAnimStyle = \"instant\" | \"wave\";\n\nexport interface RatingStarsProps {\n  defaultValue?: number;\n  value?: number;\n  onRatingChange?: (value: number) => void;\n  maxStars?: number;\n  readOnly?: boolean;\n  allowHalf?: boolean;\n  sizePreset?: RatingStarsSize;\n  starSize?: number;\n  gap?: number;\n  fillColor?: string;\n  emptyColor?: string;\n  strokeWidth?: number;\n  colorRange?: boolean;\n  colorLow?: string;\n  colorMid?: string;\n  colorHigh?: string;\n  showGlow?: boolean;\n  glowSize?: number;\n  labelPosition?: RatingStarsLabelPosition;\n  showLabel?: boolean;\n  labelStyle?: RatingStarsLabelStyle;\n  showReviewCount?: boolean;\n  reviewCount?: number;\n  fontSize?: number;\n  textColor?: string;\n  showBar?: boolean;\n  barHeight?: number;\n  barRadius?: number;\n  barTrackColor?: string;\n  animated?: boolean;\n  animStyle?: RatingStarsAnimStyle;\n  className?: string;\n}\n\nconst SIZE_MAP: Record<Exclude<RatingStarsSize, \"custom\">, number> = { xs: 20, sm: 28, md: 40, lg: 56, xl: 72 };\n\nfunction hexToRgb(hex: string): [number, number, number] {\n  const h = hex.replace(\"#\", \"\").padEnd(6, \"0\");\n  return [parseInt(h.slice(0, 2), 16) || 0, parseInt(h.slice(2, 4), 16) || 0, parseInt(h.slice(4, 6), 16) || 0];\n}\n\nfunction lerpColor(a: string, b: string, t: number): string {\n  try {\n    const [ar, ag, ab] = hexToRgb(a);\n    const [br, bg, bb] = hexToRgb(b);\n    return `rgb(${Math.round(ar + (br - ar) * t)},${Math.round(ag + (bg - ag) * t)},${Math.round(ab + (bb - ab) * t)})`;\n  } catch {\n    return a;\n  }\n}\n\nfunction getRangeColor(ratio: number, low: string, mid: string, high: string): string {\n  const t = Math.max(0, Math.min(1, ratio));\n  return t <= 0.5 ? lerpColor(low, mid, t * 2) : lerpColor(mid, high, (t - 0.5) * 2);\n}\n\nfunction StarIcon({\n  filled,\n  half,\n  size,\n  fillColor,\n  emptyColor,\n  strokeWidth,\n  glowSize,\n}: {\n  filled: boolean;\n  half: boolean;\n  size: number;\n  fillColor: string;\n  emptyColor: string;\n  strokeWidth: number;\n  glowSize: number;\n}) {\n  const id = React.useId();\n  const path = \"M12 2.5l2.83 5.73 6.32.92-4.57 4.45 1.08 6.28L12 16.9l-5.66 2.98 1.08-6.28L2.85 9.15l6.32-.92L12 2.5z\";\n  const glow = glowSize > 0 && (filled || half) ? `drop-shadow(0 0 ${glowSize}px ${fillColor})` : undefined;\n\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 24 24\" fill=\"none\" style={{ filter: glow, display: \"block\" }}>\n      {half && (\n        <defs>\n          <linearGradient id={id} x1=\"0\" x2=\"1\" y1=\"0\" y2=\"0\">\n            <stop offset=\"50%\" stopColor={fillColor} />\n            <stop offset=\"50%\" stopColor=\"transparent\" />\n          </linearGradient>\n        </defs>\n      )}\n      <path\n        d={path}\n        fill={filled ? fillColor : half ? `url(#${id})` : \"transparent\"}\n        stroke={filled || half ? fillColor : emptyColor}\n        strokeWidth={strokeWidth}\n        strokeLinejoin=\"round\"\n        strokeLinecap=\"round\"\n      />\n    </svg>\n  );\n}\n\nfunction Burst({ active, color, size }: { active: boolean; color: string; size: number }) {\n  const count = 8;\n  const radius = size * 0.9;\n  return (\n    <AnimatePresence>\n      {active &&\n        Array.from({ length: count }).map((_, i) => {\n          const angle = (i / count) * 360;\n          const rad = (angle * Math.PI) / 180;\n          const x = Math.cos(rad) * radius;\n          const y = Math.sin(rad) * radius;\n          return (\n            <motion.div\n              key={i}\n              className=\"pointer-events-none absolute top-1/2 left-1/2 rounded-full\"\n              style={{ width: size * 0.12, height: size * 0.12, backgroundColor: color, marginTop: -(size * 0.06), marginLeft: -(size * 0.06) }}\n              initial={{ x: 0, y: 0, opacity: 1, scale: 1 }}\n              animate={{ x, y, opacity: 0, scale: 0 }}\n              exit={{ opacity: 0 }}\n              transition={{ duration: 0.5, ease: \"easeOut\" }}\n            />\n          );\n        })}\n    </AnimatePresence>\n  );\n}\n\nfunction Star({\n  index,\n  value,\n  hoverValue,\n  allowHalf,\n  size,\n  fillColor,\n  emptyColor,\n  strokeWidth,\n  glowSize,\n  onHover,\n  onLeave,\n  onClick,\n  animate: shouldAnimate,\n  waveDelay,\n}: {\n  index: number;\n  value: number;\n  hoverValue: number | null;\n  allowHalf: boolean;\n  size: number;\n  fillColor: string;\n  emptyColor: string;\n  strokeWidth: number;\n  glowSize: number;\n  onHover: (v: number) => void;\n  onLeave: () => void;\n  onClick: (v: number) => void;\n  animate: boolean;\n  waveDelay: number;\n}) {\n  const [burst, setBurst] = React.useState(false);\n  const active = hoverValue !== null ? hoverValue : value;\n  const isFilled = active >= index + 1;\n  const isHalf = allowHalf && !isFilled && active >= index + 0.5;\n\n  const handleMouseMove = React.useCallback(\n    (e: React.MouseEvent<HTMLDivElement>) => {\n      if (!allowHalf) {\n        onHover(index + 1);\n        return;\n      }\n      const rect = e.currentTarget.getBoundingClientRect();\n      onHover(e.clientX - rect.left < rect.width / 2 ? index + 0.5 : index + 1);\n    },\n    [index, allowHalf, onHover],\n  );\n\n  const handleClick = React.useCallback(\n    (e: React.MouseEvent<HTMLDivElement>) => {\n      let v = index + 1;\n      if (allowHalf) {\n        const rect = e.currentTarget.getBoundingClientRect();\n        v = e.clientX - rect.left < rect.width / 2 ? index + 0.5 : index + 1;\n      }\n      onClick(v);\n      if (shouldAnimate) {\n        setBurst(true);\n        setTimeout(() => setBurst(false), 600);\n      }\n    },\n    [index, allowHalf, onClick, shouldAnimate],\n  );\n\n  const starValue = index + 1;\n\n  return (\n    <div\n      role=\"radio\"\n      aria-label={`${starValue} ${starValue === 1 ? \"star\" : \"stars\"}`}\n      aria-checked={isFilled}\n      tabIndex={-1}\n      className=\"relative inline-flex cursor-pointer\"\n      onMouseMove={handleMouseMove}\n      onMouseLeave={onLeave}\n      onClick={handleClick}\n    >\n      <motion.div\n        animate={shouldAnimate && isFilled ? \"filled\" : \"idle\"}\n        whileHover={shouldAnimate ? \"hovered\" : undefined}\n        whileTap={shouldAnimate ? \"tapped\" : undefined}\n        variants={{\n          idle: { scale: 1, rotate: 0, transition: { type: \"spring\", stiffness: 400, damping: 15 } },\n          filled: { scale: [1, 1.18, 1], rotate: [0, -10, 10, 0], transition: { duration: 0.4, ease: \"easeOut\", delay: waveDelay } },\n          hovered: { scale: 1.2, transition: { type: \"spring\", stiffness: 500, damping: 20 } },\n          tapped: { scale: 0.9, transition: { type: \"spring\", stiffness: 500, damping: 20 } },\n        }}\n        className=\"inline-flex\"\n      >\n        <StarIcon filled={isFilled} half={isHalf} size={size} fillColor={fillColor} emptyColor={emptyColor} strokeWidth={strokeWidth} glowSize={glowSize} />\n      </motion.div>\n      {shouldAnimate && <Burst active={burst} color={fillColor} size={size} />}\n    </div>\n  );\n}\n\nfunction RatingLabel({\n  value,\n  max,\n  showCount,\n  reviewCount,\n  fontSize,\n  textColor,\n  labelStyle,\n}: {\n  value: number;\n  max: number;\n  showCount: boolean;\n  reviewCount: number;\n  fontSize: number;\n  textColor: string;\n  labelStyle: RatingStarsLabelStyle;\n}) {\n  const labels = [\"Terrible\", \"Poor\", \"Fair\", \"Good\", \"Excellent\"];\n  const labelIndex = Math.min(Math.ceil(value) - 1, max - 1);\n  let text = \"\";\n  if (labelStyle === \"score\") text = value.toFixed(1);\n  else if (labelStyle === \"fraction\") text = `${value.toFixed(1)} / ${max}`;\n  else if (labelStyle === \"percent\") text = `${Math.round((value / max) * 100)}%`;\n  else text = value > 0 ? labels[Math.min(labelIndex, labels.length - 1)] : \"-\";\n\n  return (\n    <div className=\"flex select-none items-center gap-1.5\" style={{ fontSize, color: textColor }}>\n      <span className=\"font-bold\">{text}</span>\n      {showCount && <span className=\"opacity-50\">({reviewCount.toLocaleString()})</span>}\n    </div>\n  );\n}\n\nfunction RatingBar({ value, max, fillColor, trackColor, height, radius, animated }: { value: number; max: number; fillColor: string; trackColor: string; height: number; radius: number; animated: boolean }) {\n  const pct = Math.min(Math.max(value / max, 0), 1) * 100;\n  return (\n    <div className=\"w-full overflow-hidden\" style={{ height, borderRadius: radius, backgroundColor: trackColor }}>\n      <motion.div\n        style={{ height: \"100%\", backgroundColor: fillColor, borderRadius: radius }}\n        initial={{ width: 0 }}\n        animate={{ width: `${pct}%` }}\n        transition={animated ? { duration: 0.6, ease: \"easeOut\" } : { duration: 0 }}\n      />\n    </div>\n  );\n}\n\nexport function RatingStars({\n  defaultValue = 3,\n  value: controlledValue,\n  onRatingChange,\n  maxStars = 5,\n  readOnly = false,\n  allowHalf = true,\n  sizePreset = \"md\",\n  starSize = 40,\n  gap = 8,\n  fillColor = \"#F5C518\",\n  emptyColor = \"#9CA3AF\",\n  strokeWidth = 1.5,\n  colorRange = false,\n  colorLow = \"#EF4444\",\n  colorMid = \"#F5C518\",\n  colorHigh = \"#22C55E\",\n  showGlow = false,\n  glowSize = 8,\n  labelPosition = \"right\",\n  showLabel = true,\n  labelStyle = \"score\",\n  showReviewCount = false,\n  reviewCount = 1284,\n  fontSize = 16,\n  textColor = \"currentColor\",\n  showBar = false,\n  barHeight = 6,\n  barRadius = 999,\n  barTrackColor = \"rgba(128,128,128,0.2)\",\n  animated = true,\n  animStyle = \"instant\",\n  className,\n}: RatingStarsProps) {\n  const resolvedSize = sizePreset === \"custom\" ? starSize : SIZE_MAP[sizePreset];\n  const isControlled = controlledValue !== undefined;\n  const [internalValue, setInternalValue] = React.useState(defaultValue);\n  const value = isControlled ? controlledValue : internalValue;\n  const [hoverValue, setHoverValue] = React.useState<number | null>(null);\n\n  const handleClick = React.useCallback(\n    (v: number) => {\n      if (readOnly) return;\n      if (!isControlled) setInternalValue(v);\n      onRatingChange?.(v);\n    },\n    [readOnly, isControlled, onRatingChange],\n  );\n\n  const handleLeave = React.useCallback(() => setHoverValue(null), []);\n\n  const handleKeyDown = React.useCallback(\n    (e: React.KeyboardEvent) => {\n      if (readOnly) return;\n      const step = allowHalf ? 0.5 : 1;\n      let next = value;\n      if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\") {\n        e.preventDefault();\n        next = Math.min(value + step, maxStars);\n      } else if (e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") {\n        e.preventDefault();\n        next = Math.max(value - step, 0);\n      } else if (e.key === \"Home\") {\n        e.preventDefault();\n        next = 0;\n      } else if (e.key === \"End\") {\n        e.preventDefault();\n        next = maxStars;\n      } else {\n        return;\n      }\n      if (!isControlled) setInternalValue(next);\n      onRatingChange?.(next);\n    },\n    [readOnly, allowHalf, value, maxStars, isControlled, onRatingChange],\n  );\n\n  const activeValue = hoverValue !== null ? hoverValue : value;\n  const activeFill = colorRange ? getRangeColor(activeValue / maxStars, colorLow, colorMid, colorHigh) : fillColor;\n  const isVertical = labelPosition === \"top\" || labelPosition === \"bottom\";\n\n  const starsRow = (\n    <div\n      role=\"slider\"\n      aria-label=\"Star rating\"\n      aria-valuemin={0}\n      aria-valuemax={maxStars}\n      aria-valuenow={value}\n      aria-valuetext={`${value} out of ${maxStars} stars`}\n      aria-readonly={readOnly || undefined}\n      tabIndex={readOnly ? -1 : 0}\n      onKeyDown={handleKeyDown}\n      className=\"flex flex-row items-center outline-none\"\n      style={{ gap }}\n    >\n      {Array.from({ length: maxStars }).map((_, i) => (\n        <Star\n          key={i}\n          index={i}\n          value={value}\n          hoverValue={readOnly ? null : hoverValue}\n          allowHalf={allowHalf}\n          size={resolvedSize}\n          fillColor={activeFill}\n          emptyColor={emptyColor}\n          strokeWidth={strokeWidth}\n          glowSize={(showGlow ? glowSize : 0)}\n          onHover={readOnly ? () => {} : setHoverValue}\n          onLeave={readOnly ? () => {} : handleLeave}\n          onClick={handleClick}\n          animate={animated && !readOnly}\n          waveDelay={animated && animStyle === \"wave\" ? i * 0.07 : 0}\n        />\n      ))}\n    </div>\n  );\n\n  const label = (\n    <RatingLabel value={value} max={maxStars} showCount={showReviewCount} reviewCount={reviewCount} fontSize={fontSize} textColor={textColor} labelStyle={labelStyle} />\n  );\n\n  const bar = <RatingBar value={value} max={maxStars} fillColor={activeFill} trackColor={barTrackColor} height={barHeight} radius={barRadius} animated={animated} />;\n\n  const ratingUI = (\n    <div className={cn(\"flex items-center\", isVertical ? \"flex-col items-start\" : \"flex-row\", showBar ? \"w-full\" : \"w-fit\", className)} style={{ gap: isVertical ? gap * 0.6 : gap }}>\n      {showLabel && (labelPosition === \"top\" || labelPosition === \"left\") && label}\n      <div className=\"flex flex-col\" style={{ gap: gap * 0.5, flex: showBar ? 1 : \"0 0 auto\" }}>\n        {starsRow}\n        {showBar && bar}\n      </div>\n      {showLabel && (labelPosition === \"right\" || labelPosition === \"bottom\") && label}\n    </div>\n  );\n\n  return ratingUI;\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}