{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rating",
  "title": "Rating",
  "description": "Star, heart, circle, bolt, diamond or custom-icon rating in solid, soft, outline, glow and neo styles: half steps, hover preview, clear, animated clip fill with pop or wave, value, count and word labels, read-only exact averages, RTL fill and arrow flipping, with radiogroup or slider semantics.",
  "dependencies": [],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json",
    "https://goodui.dev/r/sounds.json"
  ],
  "files": [
    {
      "path": "registry/components/core/rating.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport type { SoundName } from \"@/lib/goodui-sounds\"\nimport { GooduiTokens } from \"@/lib/goodui-motion\"\n\n/*\n * Rating input and read-only display, independent layers: every axis combines with every other.\n *   variant    solid, soft, outline, glow, neo        tone       fill color (warning = classic gold, the default)\n *   icon       star, heart, circle, bolt, diamond or any icon node                  size  xs … xl\n *   animation  none, fill (smooth clip), pop (picked icon bounces), wave (icons pop one after another)\n * Whole steps render a radiogroup (one radio per icon, roving focus). Half steps render a slider, because a radio per\n * half icon reads badly. Read-only renders role=\"img\" and shows exact fractions (4.3 fills 30% of the fifth star).\n * The fill grows from the inline start, so it runs right-to-left under dir=\"rtl\" and arrow keys flip with it.\n */\n\nexport type RatingVariant = \"solid\" | \"soft\" | \"outline\" | \"glow\" | \"neo\"\nexport type RatingTone = \"primary\" | \"neutral\" | \"success\" | \"warning\" | \"danger\" | \"info\"\nexport type RatingSize = \"xs\" | \"sm\" | \"md\" | \"lg\" | \"xl\"\nexport type RatingAnimation = \"none\" | \"fill\" | \"pop\" | \"wave\"\nexport type RatingIcon = \"star\" | \"heart\" | \"circle\" | \"bolt\" | \"diamond\"\n\nexport type RatingLabels = {\n  /** Accessible name of the control when no aria-label is passed. */\n  group: string\n  /** Name of one radio (whole steps). */\n  item: (n: number, max: number) => string\n  /** Spoken value of the slider (half steps). */\n  valueText: (value: number, max: number) => string\n  /** Accessible name in read-only mode. */\n  readOnly: (value: number, max: number) => string\n  /** Visible count next to the icons. */\n  count: (count: number) => string\n  /** Spoken count. */\n  countSr: (count: number) => string\n}\nconst nf = new Intl.NumberFormat(\"en-US\")\nconst DEFAULT_LABELS: RatingLabels = {\n  group: \"Rating\",\n  item: (n) => `${n} ${n === 1 ? \"star\" : \"stars\"}`,\n  valueText: (v, max) => (v ? `${v} out of ${max}` : \"No rating\"),\n  readOnly: (v, max) => `Rated ${v} out of ${max}`,\n  count: (c) => `(${nf.format(c)})`,\n  countSr: (c) => `${nf.format(c)} ${c === 1 ? \"review\" : \"reviews\"}`,\n}\n\nexport type RatingProps = Omit<React.ComponentProps<\"div\">, \"defaultValue\" | \"onChange\" | \"children\" | \"dir\"> & {\n  /** Current rating (controlled); 0 means no rating. */\n  value?: number\n  /** Initial rating when uncontrolled. */\n  defaultValue?: number\n  /** Called with the new rating; 0 after clearing. */\n  onValueChange?: (value: number) => void\n  /** Number of icons. */\n  max?: number\n  /** 1 for whole icons (radiogroup), 0.5 for half steps (slider). */\n  precision?: 1 | 0.5\n  /** Display only: no hover or focus, shows exact fractions. */\n  readOnly?: boolean\n  disabled?: boolean\n  /** Picking the current value again, Backspace, Delete or 0 clears the rating. */\n  clearable?: boolean\n  /** Fill previews under the pointer before you click. */\n  hoverPreview?: boolean\n  /** Plays a \"tick\" per icon under the pointer (pitched up along the row) and a \"pop\" on pick, when the visitor has sounds on (lib/goodui-sounds); a sound name replaces the pop. Off by default. */\n  sound?: boolean | SoundName\n  /** Built-in icon name, or your own icon node (a lucide icon works: it gets filled with CSS). */\n  icon?: RatingIcon | React.ReactNode\n  /** Different icon for the empty state; defaults to `icon`. */\n  emptyIcon?: React.ReactNode\n  /** How icons are painted: solid (default), soft (tinted empties), outline (stroked empties), glow, or neo. */\n  variant?: RatingVariant\n  /** Fill color. Defaults to warning, the classic gold. */\n  tone?: RatingTone\n  size?: RatingSize\n  /** What moves when the rating changes. Reduced motion turns all of it off. */\n  animation?: RatingAnimation\n  /** Shows the number (\"4.5\") after the icons. */\n  showValue?: boolean\n  /** Review count shown after the value, e.g. 1284 → \"(1,284)\". */\n  count?: number\n  /** Word per icon (\"Poor\" … \"Excellent\"); read with the radios and shown when showLabel is set. */\n  itemLabels?: string[]\n  /** Shows the word for the current or hovered value after the icons. */\n  showLabel?: boolean\n  /** Form field name (renders a hidden input with the number). */\n  name?: string\n  /** Class for the icon row. */\n  iconsClassName?: string\n  /** Text for screen readers and the count; pass translations here. */\n  labels?: Partial<RatingLabels>\n}\n\nconst tones = (c: string) => `\n.${c}{--pc-c:var(--primary);--pc-fg:var(--primary-foreground)}\n.${c}[data-tone=neutral]{--pc-c:var(--foreground);--pc-fg:var(--background)}\n.${c}[data-tone=success]{--pc-c:oklch(0.53 0.13 155);--pc-fg:#fff}\n.${c}[data-tone=warning]{--pc-c:oklch(0.75 0.16 70);--pc-fg:oklch(0.25 0.05 70)}\n.${c}[data-tone=danger]{--pc-c:oklch(0.56 0.21 27);--pc-fg:#fff}\n.${c}[data-tone=info]{--pc-c:oklch(0.55 0.17 250);--pc-fg:#fff}\n.dark .${c}[data-tone=success]{--pc-c:oklch(0.7 0.16 155);--pc-fg:oklch(0.2 0.05 155)}\n.dark .${c}[data-tone=danger]{--pc-c:oklch(0.68 0.2 25);--pc-fg:oklch(0.2 0.05 25)}\n.dark .${c}[data-tone=info]{--pc-c:oklch(0.7 0.15 245);--pc-fg:oklch(0.2 0.05 245)}\n.${c}{--pc-tint:color-mix(in oklab,var(--pc-c) 14%,transparent);--pc-ink:color-mix(in oklab,var(--pc-c) 82%,var(--foreground))}\n.${c}[data-tone=warning]{--pc-ink:color-mix(in oklab,var(--pc-c) 62%,var(--foreground))}\n`\n\nconst R = \".pc-rating\"\nconst CSS = `${tones(\"pc-rating\")}\n${R}{--pc-e:color-mix(in oklab,var(--foreground) 13%,transparent);--pc-es:var(--pc-e);--pc-esw:1px;--pc-fs:var(--pc-c);--pc-fsw:1px}\n${R}[data-variant=soft]{--pc-e:color-mix(in oklab,var(--pc-c) 22%,transparent);--pc-es:var(--pc-e)}\n${R}[data-variant=outline]{--pc-e:transparent;--pc-es:color-mix(in oklab,var(--foreground) 32%,transparent);--pc-esw:1.5px}\n${R}[data-variant=neo]{--pc-e:var(--background);--pc-es:var(--foreground);--pc-esw:1.75px;--pc-fs:var(--foreground);--pc-fsw:1.75px}\n.pc-rating-ico{display:grid;place-items:center;width:100%;height:100%}\n.pc-rating-ico svg{width:100%;height:100%;overflow:visible;stroke-linejoin:round;stroke-linecap:round}\n.pc-rating-empty svg{fill:var(--pc-e);stroke:var(--pc-es);stroke-width:var(--pc-esw)}\n.pc-rating-fill{position:absolute;inset:0;clip-path:inset(-20% calc(100% - var(--pc-f)) -20% -20%)}\n.pc-rating-fill:dir(rtl){clip-path:inset(-20% -20% -20% calc(100% - var(--pc-f)))}\n.pc-rating-fill svg{fill:var(--pc-c);stroke:var(--pc-fs);stroke-width:var(--pc-fsw)}\n${R}[data-variant=glow] .pc-rating-fill svg{filter:drop-shadow(0 0 5px color-mix(in oklab,var(--pc-c) 70%,transparent))}\n${R}[data-preview] .pc-rating-fill{opacity:.72}\n.pc-rating-item{position:relative;-webkit-tap-highlight-color:transparent;outline:none;border-radius:6px}\n.pc-rating-item:focus-visible,.pc-rating-icons:focus-visible{box-shadow:0 0 0 3px color-mix(in oklab,var(--pc-c) 40%,transparent)}\n.pc-rating-icons{outline:none;border-radius:8px}\n${R}:not([data-anim=none]) .pc-rating-fill{transition:clip-path .28s var(--pc-ease-out),opacity .15s}\n${R}:not([data-anim=none]) .pc-rating-item{transition:transform .18s var(--pc-ease-out)}\n${R}:not([data-anim=none]) .pc-rating-item[data-hover]{transform:scale(1.14)}\n.pc-rating-meta{color:color-mix(in oklab,var(--muted-foreground) 85%,var(--foreground))}\n@media (prefers-reduced-motion:reduce){\n  ${R} .pc-rating-fill,${R} .pc-rating-item{transition:none!important;animation:none!important}\n  ${R} .pc-rating-item[data-hover]{transform:none!important}\n}\n`\n\nconst ICONS: Record<RatingIcon, string> = {\n  star: \"M12 2.6l2.9 5.9 6.5.95-4.7 4.6 1.1 6.45L12 17.45l-5.8 3.05 1.1-6.45-4.7-4.6 6.5-.95z\",\n  heart: \"M12 20.4s-7.6-4.7-9.3-9.2C1.6 8.1 3.5 4.4 7.1 4.4c2 0 3.7 1.1 4.9 2.9 1.2-1.8 2.9-2.9 4.9-2.9 3.6 0 5.5 3.7 4.4 6.8-1.7 4.5-9.3 9.2-9.3 9.2z\",\n  circle: \"M12 3.4a8.6 8.6 0 1 0 0 17.2 8.6 8.6 0 1 0 0-17.2z\",\n  bolt: \"M13.6 2.4 4.4 13.6h6.8l-1 8 9.4-11.4h-6.8z\",\n  diamond: \"M12 2.6 20.4 12 12 21.4 3.6 12z\",\n}\n\nconst SIZES: Record<RatingSize, { icon: string; gap: string; text: string }> = {\n  xs: { icon: \"size-3.5\", gap: \"gap-0.5\", text: \"text-xs\" },\n  sm: { icon: \"size-4\", gap: \"gap-0.5\", text: \"text-sm\" },\n  md: { icon: \"size-6\", gap: \"gap-1\", text: \"text-sm\" },\n  lg: { icon: \"size-8\", gap: \"gap-1\", text: \"text-base\" },\n  xl: { icon: \"size-10\", gap: \"gap-1.5\", text: \"text-lg\" },\n}\n\nconst isBuiltIn = (i: unknown): i is RatingIcon => typeof i === \"string\" && i in ICONS\n\n// Optional UI sound: the sound module is loaded only when `sound` is on, so silent apps pay nothing.\nfunction useCue(sound: boolean | SoundName | undefined) {\n  React.useEffect(() => {\n    if (sound) void import(\"@/lib/goodui-sounds\")\n  }, [sound])\n  return React.useCallback(\n    (fallback: SoundName, o?: { rate?: number; exact?: boolean }) => {\n      if (!sound) return\n      void import(\"@/lib/goodui-sounds\").then((m) => m.play(sound === true || o?.exact ? fallback : sound, { rate: o?.rate }))\n    },\n    [sound],\n  )\n}\n\nexport function Rating({\n  value: valueProp,\n  defaultValue = 0,\n  onValueChange,\n  max = 5,\n  precision = 1,\n  readOnly,\n  disabled,\n  clearable = true,\n  hoverPreview = true,\n  sound,\n  icon = \"star\",\n  emptyIcon,\n  variant = \"solid\",\n  tone = \"warning\",\n  size = \"md\",\n  animation = \"pop\",\n  showValue,\n  count,\n  itemLabels,\n  showLabel,\n  name,\n  className,\n  iconsClassName,\n  labels,\n  \"aria-label\": ariaLabel,\n  \"aria-labelledby\": ariaLabelledBy,\n  ...props\n}: RatingProps) {\n  const l = useLabels(\"rating\", DEFAULT_LABELS, labels)\n  const [inner, setInner] = React.useState(defaultValue)\n  const value = valueProp ?? inner\n  const [hover, setHover] = React.useState<number | null>(null)\n  const cue = useCue(sound)\n  // One tick per icon as the pointer moves along the row, each a little higher than the last.\n  const preview = (v: number | null) => {\n    if (v !== null && v > 0 && v !== hover) cue(\"tick\", { exact: true, rate: 0.9 + 0.06 * Math.ceil(v) })\n    setHover(v)\n  }\n  const root = React.useRef<HTMLDivElement>(null)\n  const items = React.useRef<(HTMLSpanElement | null)[]>([])\n  const interactive = !readOnly && !disabled\n  const half = precision === 0.5\n  const shown = interactive && hoverPreview && hover != null ? hover : value\n  const s = SIZES[size]\n  const snap = (v: number) => Math.min(max, Math.max(0, Math.round(v / precision) * precision))\n\n  const glyph = (node: RatingIcon | React.ReactNode) =>\n    isBuiltIn(node) ? (\n      <svg viewBox=\"0 0 24 24\" aria-hidden=\"true\">\n        <path d={ICONS[node]} />\n      </svg>\n    ) : (\n      node\n    )\n\n  const animate = (v: number) => {\n    if (animation !== \"pop\" && animation !== \"wave\") return\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return\n    const last = Math.ceil(v) - 1\n    const pop = (i: number, delay: number) =>\n      items.current[i]?.animate?.([{ transform: \"scale(1)\" }, { transform: \"scale(1.32) rotate(-6deg)\" }, { transform: \"scale(1)\" }], {\n        duration: 320,\n        delay,\n        easing: \"cubic-bezier(.34,1.56,.64,1)\",\n      })\n    if (animation === \"pop\") {\n      if (last >= 0) pop(last, 0)\n    } else for (let i = 0; i <= last; i++) pop(i, i * 55)\n  }\n\n  const commit = (v: number, focus = false) => {\n    if (!interactive) return\n    const next = snap(v)\n    if (valueProp === undefined) setInner(next)\n    onValueChange?.(next)\n    if (next > 0) cue(\"pop\", { rate: 0.9 + 0.06 * Math.ceil(next) })\n    else cue(\"tick\", { exact: true, rate: 0.8 })\n    if (next > 0 && next !== value) animate(next)\n    if (focus && !half) requestAnimationFrame(() => items.current[Math.max(0, Math.ceil(next) - 1)]?.focus())\n  }\n\n  const isRtl = () => !!root.current && getComputedStyle(root.current).direction === \"rtl\"\n  const pointValue = (e: React.PointerEvent | React.MouseEvent, i: number) => {\n    const r = e.currentTarget.getBoundingClientRect()\n    let f = r.width ? (e.clientX - r.left) / r.width : 1\n    if (isRtl()) f = 1 - f\n    return half && f <= 0.5 ? i + 0.5 : i + 1\n  }\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if (!interactive) return\n    const rtl = isRtl()\n    const up = [\"ArrowUp\", rtl ? \"ArrowLeft\" : \"ArrowRight\"]\n    const down = [\"ArrowDown\", rtl ? \"ArrowRight\" : \"ArrowLeft\"]\n    const floor = clearable ? 0 : precision\n    let next: number | null = null\n    if (up.includes(e.key)) next = Math.min(max, value + precision)\n    else if (down.includes(e.key)) next = Math.max(floor, value - precision)\n    else if (e.key === \"Home\") next = precision\n    else if (e.key === \"End\") next = max\n    else if ((e.key === \"Backspace\" || e.key === \"Delete\" || e.key === \"0\") && clearable) next = 0\n    else if (/^[1-9]$/.test(e.key) && Number(e.key) <= max) next = Number(e.key)\n    else if ((e.key === \" \" || e.key === \"Enter\") && !half) {\n      const i = items.current.indexOf(document.activeElement as HTMLSpanElement)\n      if (i >= 0) next = clearable && value === i + 1 ? 0 : i + 1\n    }\n    if (next == null) return\n    e.preventDefault()\n    if (next !== value) commit(next, true)\n  }\n\n  const fraction = (i: number) => Math.min(1, Math.max(0, shown - i))\n  const display = readOnly ? (Math.round(value * 10) / 10).toFixed(1) : value % 1 ? value.toFixed(1) : String(value)\n  const word = itemLabels?.[Math.ceil(shown) - 1] ?? \"\"\n  const focusIndex = Math.max(0, Math.ceil(value) - 1)\n\n  const groupProps: React.HTMLAttributes<HTMLSpanElement> = readOnly\n    ? { role: \"img\", \"aria-label\": ariaLabel ?? l.readOnly(Math.round(value * 10) / 10, max) }\n    : half\n      ? {\n          role: \"slider\",\n          tabIndex: disabled ? -1 : 0,\n          \"aria-label\": ariaLabelledBy ? undefined : (ariaLabel ?? l.group),\n          \"aria-labelledby\": ariaLabelledBy,\n          \"aria-valuemin\": 0,\n          \"aria-valuemax\": max,\n          \"aria-valuenow\": value,\n          \"aria-valuetext\": l.valueText(value, max),\n          \"aria-disabled\": disabled || undefined,\n          onKeyDown,\n        }\n      : {\n          role: \"radiogroup\",\n          \"aria-label\": ariaLabelledBy ? undefined : (ariaLabel ?? l.group),\n          \"aria-labelledby\": ariaLabelledBy,\n          \"aria-disabled\": disabled || undefined,\n          onKeyDown,\n        }\n\n  return (\n    <>\n      <GooduiTokens />\n      <style href=\"pc-rating\" precedence=\"default\">\n        {CSS}\n      </style>\n      <div\n        {...props}\n        ref={root}\n        data-slot=\"rating\"\n        data-variant={variant}\n        data-tone={tone}\n        data-anim={animation}\n        data-readonly={readOnly ? \"\" : undefined}\n        data-preview={interactive && hoverPreview && hover != null && hover !== value ? \"\" : undefined}\n        className={cn(\"pc-rating inline-flex max-w-full flex-wrap items-center gap-x-2 gap-y-1\", s.text, disabled && \"pointer-events-none opacity-50\", className)}\n      >\n        <span\n          {...groupProps}\n          data-slot=\"rating-icons\"\n          className={cn(\"pc-rating-icons inline-flex items-center\", s.gap, iconsClassName)}\n          onPointerLeave={interactive ? () => setHover(null) : undefined}\n        >\n          {Array.from({ length: max }, (_, i) => {\n            const radio = interactive && !half\n            return (\n              <span\n                key={i}\n                ref={(el) => {\n                  items.current[i] = el\n                }}\n                data-slot=\"rating-item\"\n                data-hover={interactive && hoverPreview && hover != null && Math.ceil(hover) - 1 === i ? \"\" : undefined}\n                role={radio ? \"radio\" : undefined}\n                aria-checked={radio ? value === i + 1 : undefined}\n                aria-label={radio ? l.item(i + 1, max) + (itemLabels?.[i] ? `, ${itemLabels[i]}` : \"\") : undefined}\n                aria-hidden={radio ? undefined : true}\n                tabIndex={radio ? (i === focusIndex ? 0 : -1) : undefined}\n                // Empty and full icons clip 20% past the box so strokes and the glow are never cut at the edge.\n                style={{ \"--pc-f\": fraction(i) === 0 ? \"-20%\" : fraction(i) === 1 ? \"120%\" : `${fraction(i) * 100}%` } as React.CSSProperties}\n                onPointerMove={interactive ? (e) => preview(pointValue(e, i)) : undefined}\n                onClick={\n                  interactive\n                    ? (e) => {\n                        const v = pointValue(e, i)\n                        commit(clearable && v === value ? 0 : v)\n                        setHover(null)\n                      }\n                    : undefined\n                }\n                className={cn(\"pc-rating-item inline-grid shrink-0\", s.icon, interactive && \"cursor-pointer\")}\n              >\n                <span className=\"pc-rating-ico pc-rating-empty\">{glyph(emptyIcon ?? icon)}</span>\n                <span className=\"pc-rating-ico pc-rating-fill\">{glyph(icon)}</span>\n              </span>\n            )\n          })}\n        </span>\n        {showValue && (\n          <span data-slot=\"rating-value\" aria-hidden={readOnly || undefined} className=\"font-semibold tabular-nums\">\n            {display}\n          </span>\n        )}\n        {count != null && (\n          <span data-slot=\"rating-count\" className=\"pc-rating-meta tabular-nums\">\n            <span aria-hidden=\"true\">{l.count(count)}</span>\n            <span className=\"sr-only\">{l.countSr(count)}</span>\n          </span>\n        )}\n        {showLabel && word && (\n          <span data-slot=\"rating-label\" aria-hidden=\"true\" className=\"pc-rating-meta font-medium\">\n            {word}\n          </span>\n        )}\n        {name && <input type=\"hidden\" name={name} value={value} />}\n      </div>\n    </>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/rating.tsx"
    }
  ],
  "categories": [
    "components",
    "core"
  ],
  "type": "registry:component"
}