{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rating",
  "title": "Rating",
  "description": "Star rating input and a compact rating badge.",
  "dependencies": [
    "class-variance-authority@^0.7.1",
    "lucide-react@^1.27.0"
  ],
  "registryDependencies": [
    "@pestoui/style",
    "@pestoui/utils"
  ],
  "files": [
    {
      "path": "pestoui/ui/rating.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { cva, type VariantProps } from \"class-variance-authority\"\nimport { StarIcon } from \"lucide-react\"\n\nimport { cn } from \"@/lib/utils\"\n\n/* The star box is a squircle, not a circle — the radius scale would exceed half\n   the box at these sizes. Star glyph sizes cascade from the root via [&_svg]. */\nconst ratingVariants = cva(\n  \"group/rating inline-flex w-fit items-center [&_svg]:pointer-events-none [&_svg]:shrink-0\",\n  {\n    variants: {\n      size: {\n        sm: \"gap-px *:data-[slot=rating-star]:size-5 *:data-[slot=rating-star]:rounded-[7px] [&_svg]:size-3.5\",\n        default:\n          \"gap-0.5 *:data-[slot=rating-star]:size-6 *:data-[slot=rating-star]:rounded-[8px] [&_svg]:size-4.5\",\n        lg: \"gap-0.5 *:data-[slot=rating-star]:size-7.5 *:data-[slot=rating-star]:rounded-[10px] [&_svg]:size-5.5\",\n      },\n    },\n    defaultVariants: {\n      size: \"default\",\n    },\n  }\n)\n\n/* One star = a muted glyph with an amber copy clipped to `fraction` on top.\n   The wrapper shrink-wraps the glyph so the clip is measured against the star\n   itself, not the padded hit box around it. */\nfunction RatingStarGlyph({\n  fraction,\n  index = 0,\n  live = false,\n}: {\n  fraction: number\n  /* Position in the row. The fill sweeps out from the first star rather than\n     lighting the whole run in one frame — a rating of four is four stars\n     filling, and the sweep is what says so. */\n  index?: number\n  /* Hold the amber copy in the DOM at zero width so the fill can travel instead\n     of appearing. Read-only ratings opt out: their value never changes, and they\n     are the ones that repeat fifty times down a list of results. */\n  live?: boolean\n}) {\n  return (\n    <span className=\"relative inline-flex\">\n      <StarIcon\n        aria-hidden=\"true\"\n        className=\"block fill-muted text-muted-foreground/30\"\n      />\n      {(live || fraction > 0) && (\n        <span\n          aria-hidden=\"true\"\n          className={cn(\n            \"pointer-events-none absolute inset-y-0 left-0 overflow-hidden\",\n            live && \"transition-[width]\",\n            /* Filling is arrival and gets the press spring; emptying is\n               dismissal and gets the short tween, so the two halves of the\n               interaction never feel like the same move played backwards. */\n            live &&\n              (fraction > 0\n                ? \"duration-spring-press ease-spring-press\"\n                : \"duration-exit ease-out-expo\")\n          )}\n          style={{\n            width: `${fraction * 100}%`,\n            /* The sweep is on the way up only: filling runs left to right one\n               star behind the last, emptying drops the whole run at once. */\n            transitionDelay:\n              live && fraction > 0\n                ? `calc(${index} * var(--stagger-tight))`\n                : undefined,\n          }}\n        >\n          <StarIcon className=\"block fill-warning text-warning\" />\n        </span>\n      )}\n    </span>\n  )\n}\n\ntype RatingProps = Omit<\n  React.ComponentProps<\"div\">,\n  \"onChange\" | \"value\" | \"defaultValue\"\n> &\n  VariantProps<typeof ratingVariants> & {\n    /** Controlled value. Supports halves (4.5) for display. */\n    value?: number\n    /** Uncontrolled starting value. */\n    defaultValue?: number\n    max?: number\n    readOnly?: boolean\n    onChange?: (value: number) => void\n  }\n\nfunction Rating({\n  className,\n  size = \"default\",\n  value,\n  defaultValue = 0,\n  max = 5,\n  readOnly = false,\n  onChange,\n  onMouseLeave,\n  ...props\n}: RatingProps) {\n  const [uncontrolled, setUncontrolled] = React.useState(defaultValue)\n  const [preview, setPreview] = React.useState<number | null>(null)\n  const starRefs = React.useRef<Array<HTMLButtonElement | null>>([])\n\n  const current = value ?? uncontrolled\n  const stars = Array.from({ length: max }, (_, index) => index)\n\n  if (readOnly) {\n    return (\n      <div\n        data-slot=\"rating\"\n        data-size={size}\n        role=\"img\"\n        aria-label={`${current} out of ${max} stars`}\n        className={cn(ratingVariants({ size }), className)}\n        onMouseLeave={onMouseLeave}\n        {...props}\n      >\n        {stars.map((index) => (\n          <span\n            key={index}\n            data-slot=\"rating-star\"\n            className=\"inline-flex items-center justify-center\"\n          >\n            <RatingStarGlyph\n              fraction={Math.min(1, Math.max(0, current - index))}\n            />\n          </span>\n        ))}\n      </div>\n    )\n  }\n\n  /* Hover and roving focus both drive the same preview fill. */\n  const display = preview ?? current\n  const activeIndex = Math.max(0, Math.ceil(current) - 1)\n\n  const commit = (next: number) => {\n    if (value === undefined) setUncontrolled(next)\n    onChange?.(next)\n  }\n\n  const handleKeyDown = (event: React.KeyboardEvent<HTMLButtonElement>) => {\n    let next: number | undefined\n\n    if (event.key === \"ArrowRight\" || event.key === \"ArrowUp\") {\n      next = Math.min(max, Math.floor(current) + 1)\n    } else if (event.key === \"ArrowLeft\" || event.key === \"ArrowDown\") {\n      next = Math.max(1, Math.ceil(current) - 1)\n    } else if (event.key === \"Home\") {\n      next = 1\n    } else if (event.key === \"End\") {\n      next = max\n    }\n\n    if (next === undefined) return\n\n    event.preventDefault()\n    commit(next)\n    setPreview(next)\n    starRefs.current[next - 1]?.focus()\n  }\n\n  return (\n    <div\n      data-slot=\"rating\"\n      data-size={size}\n      role=\"radiogroup\"\n      aria-label=\"Rating\"\n      className={cn(ratingVariants({ size }), className)}\n      {...props}\n      /* Composed, not overwritten — a consumer handler must not silently\n         disable the hover preview reset. */\n      onMouseLeave={(event) => {\n        onMouseLeave?.(event)\n        setPreview(null)\n      }}\n    >\n      {stars.map((index) => {\n        const starValue = index + 1\n\n        return (\n          <button\n            key={index}\n            ref={(node) => {\n              starRefs.current[index] = node\n            }}\n            type=\"button\"\n            data-slot=\"rating-star\"\n            role=\"radio\"\n            aria-checked={Math.ceil(current) === starValue}\n            aria-label={`${starValue} ${starValue === 1 ? \"star\" : \"stars\"}`}\n            tabIndex={index === activeIndex ? 0 : -1}\n            className=\"relative inline-flex cursor-pointer items-center justify-center border border-transparent transition-all duration-spring-press ease-spring-press outline-none select-none focus-visible:z-10 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:translate-y-px active:scale-90\"\n            onClick={() => commit(starValue)}\n            onMouseEnter={() => setPreview(starValue)}\n            onFocus={() => setPreview(starValue)}\n            onBlur={() => setPreview(null)}\n            onKeyDown={handleKeyDown}\n          >\n            <RatingStarGlyph\n              live\n              index={index}\n              fraction={Math.min(1, Math.max(0, display - index))}\n            />\n          </button>\n        )\n      })}\n    </div>\n  )\n}\n\nconst ratingBadgeVariants = cva(\n  \"inline-flex w-fit shrink-0 items-center gap-1 rounded-full bg-secondary font-medium whitespace-nowrap text-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0\",\n  {\n    variants: {\n      size: {\n        sm: \"h-5.5 px-2 text-caption-sm [&_svg]:size-3\",\n        default: \"h-6 px-2.5 text-caption [&_svg]:size-3.5\",\n        lg: \"h-7 gap-1.5 px-3 text-body-sm [&_svg]:size-4\",\n      },\n    },\n    defaultVariants: {\n      size: \"default\",\n    },\n  }\n)\n\nfunction RatingBadge({\n  className,\n  size = \"default\",\n  value,\n  count,\n  locale,\n  ...props\n}: Omit<React.ComponentProps<\"span\">, \"value\"> &\n  VariantProps<typeof ratingBadgeVariants> & {\n    value: number\n    /** Optional review count, rendered in parentheses. */\n    count?: number\n    /** Locale for the count's digit grouping; the browser's own when unset.\n     *  Set it on a server-rendered page, so the server's markup and the\n     *  browser's first render agree. */\n    locale?: string\n  }) {\n  return (\n    <span\n      data-slot=\"rating-badge\"\n      data-size={size}\n      className={cn(ratingBadgeVariants({ size }), className)}\n      {...props}\n    >\n      <span className=\"font-semibold tabular-nums\">{value.toFixed(1)}</span>\n      <StarIcon aria-hidden=\"true\" className=\"fill-warning text-warning\" />\n      <span className=\"sr-only\">stars</span>\n      {count !== undefined && (\n        <>\n          <span className=\"tabular-nums text-muted-foreground\">\n            ({count.toLocaleString(locale)})\n          </span>\n          <span className=\"sr-only\">reviews</span>\n        </>\n      )}\n    </span>\n  )\n}\n\nexport { Rating, RatingBadge, ratingVariants, ratingBadgeVariants }\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}