{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rating",
  "title": "Rating",
  "description": "Astro star displays for a score, with an optional control to set it.",
  "dependencies": [
    "@lucide/astro",
    "cn"
  ],
  "registryDependencies": [
    "@gnostic/merge-styles"
  ],
  "files": [
    {
      "path": "packages/ui/src/components/ui/rating/index.ts",
      "content": "export { default as Rating } from './rating.astro'\nexport { default as RatingValue } from './rating-value.astro'\n\nexport type { RatingProps } from './types.ts'\n",
      "type": "registry:file",
      "target": "~/src/components/ui/rating/index.ts"
    },
    {
      "path": "packages/ui/src/components/ui/rating/rating.astro",
      "content": "---\nimport type { HTMLAttributes, HTMLTag, Polymorphic } from \"astro/types\"\nimport Star from \"@lucide/astro/icons/star\"\n\nimport { cn } from \"cn\"\nimport type { RatingProps } from \"./types.ts\"\nimport { mergeStyles } from \"../../../lib/merge-styles.ts\"\n\n/**\n * Star score.\n * Div attributes pass through. `value` and `interactive` come from `RatingProps`.\n */\ntype Props<T extends HTMLTag = \"div\"> = Polymorphic<{ as: T }> &\n  Omit<HTMLAttributes<T>, \"name\"> &\n  RatingProps\n\nconst {\n  as: Tag = \"div\",\n  class: className,\n  interactive = false,\n  max = 5,\n  value,\n  name,\n  label = \"Rating\",\n  disabled = false,\n  starColor = \"var(--primary)\",\n  style: styleProp,\n  ...props\n} = Astro.props\n\n/** Use a whole star count of at least 1. */\nconst starCount = Number.isFinite(max) ? Math.max(1, Math.trunc(max)) : 5\n\n/** Keep a non-numeric score at 0, and keep every score inside the star count. */\nconst numericValue = Number(value)\nconst score = Number.isFinite(numericValue)\n  ? Math.min(starCount, Math.max(0, numericValue))\n  : 0\n\n/** Screen-reader text for the current score. */\nconst valueText = `${score} out of ${starCount}`\n\nconst style = mergeStyles(\n  {\n    \"--rating-value\": String(score),\n    \"--star-color\": starColor,\n  },\n  styleProp,\n)\n---\n\n<Tag\n  data-slot=\"rating\"\n  data-interactive={interactive ? \"\" : undefined}\n  data-disabled={interactive && disabled ? \"\" : undefined}\n  data-max={starCount}\n  role={interactive ? undefined : \"img\"}\n  aria-label={interactive ? undefined : valueText}\n  class={cn(\n    \"relative flex w-fit items-center text-base gap-4\",\n    interactive && !disabled && \"cursor-pointer select-none\",\n    interactive &&\n      !disabled &&\n      \"rounded-sm outline-none has-focus-visible:ring-2 has-focus-visible:ring-ring\",\n    interactive && disabled && \"cursor-not-allowed opacity-64\",\n    className,\n  )}\n  style={style}\n  {...props}\n>\n  <div\n    class={\"flex items-center gap-1 text-[size:inherit]\"}\n    data-slot=\"rating-stars\"\n  >\n    {Array.from({ length: starCount }, (_, index) => (\n      <span\n        data-slot=\"rating-star\"\n        data-value={index + 1}\n        aria-hidden=\"true\"\n        class=\"text-muted-foreground/25 relative inline-flex size-[1em] shrink-0\"\n        style={{ \"--star-index\": String(index + 1) }}\n      >\n        <Star class=\"size-[1em] fill-current\" />\n        <span\n          data-slot=\"rating-star-fill\"\n          class=\"text-(--star-color) pointer-events-none absolute inset-y-0 inset-s-0 overflow-hidden transition-[width] duration-150\"\n          style={{\n            // Fill one star from the shared score.\n            // `--star-index` starts at 1. A score of 3.5 fills the first three stars\n            // and half of the fourth.\n            width:\n              \"calc(clamp(0, var(--rating-value) - (var(--star-index) - 1), 1) * 100%)\",\n          }}\n        >\n          <Star class=\"size-[1em] fill-current\" />\n        </span>\n      </span>\n    ))}\n  </div>\n  {interactive && (\n    <input\n      data-slot=\"rating-input\"\n      type=\"range\"\n      min={0}\n      max={starCount}\n      step={1}\n      value={score}\n      name={name}\n      disabled={disabled}\n      aria-label={label}\n      aria-valuetext={valueText}\n      class=\"pointer-events-none sr-only outline-none\"\n    />\n  )}\n  <slot />\n</Tag>\n\n<script>\n  /** Roots already bound on this page. A later page load skips them. */\n  const bound = new WeakSet<HTMLElement>()\n\n  /**\n   * Clicks and keyboard input on one rating.\n   * The range control stores the score. The stars read `--rating-value`.\n   */\n  function bindRating(root: HTMLElement) {\n    if (bound.has(root)) return\n    const input = root.querySelector<HTMLInputElement>(\n      \"[data-slot='rating-input']\",\n    )\n    if (!input) return\n    bound.add(root)\n\n    const max = Number(root.dataset.max ?? input.max)\n\n    /**\n     * Paint the stars for a score.\n     * `announce` copies the saved score into this rating's value text.\n     * A hover preview does not. The value text must be inside this rating.\n     */\n    const paint = (raw: number, announce = false) => {\n      const next = Number.isFinite(raw) ? Math.min(max, Math.max(0, raw)) : 0\n      root.style.setProperty(\"--rating-value\", String(next))\n      if (!announce) return\n      const text = String(next)\n      input.setAttribute(\"aria-valuetext\", `${text} out of ${max}`)\n      for (const valueElement of root.querySelectorAll<HTMLElement>(\n        \"[data-slot='rating-value']\",\n      )) {\n        if (valueElement.closest(\"[data-slot='rating']\") !== root) continue\n        valueElement.textContent = text\n      }\n    }\n\n    /** Star value under the pointer, when the event hit a star in this root. */\n    const starFrom = (target: EventTarget | null) => {\n      if (!(target instanceof Element)) return\n      const star = target.closest(\"[data-slot='rating-star']\")\n      if (!(star instanceof HTMLElement) || !root.contains(star)) return\n      return star.dataset.value\n    }\n\n    input.addEventListener(\"input\", () => paint(input.valueAsNumber, true))\n    input.addEventListener(\"focus\", () => paint(input.valueAsNumber, true))\n\n    // hover preview - no announcement, only visual preview\n    root.addEventListener(\"pointermove\", (event) => {\n      if (input.disabled) return\n      const next = Number(starFrom(event.target) ?? input.valueAsNumber)\n\n      paint(next)\n    })\n    root.addEventListener(\"pointerleave\", () => paint(input.valueAsNumber))\n\n    // click commits the value to the input\n    root.addEventListener(\"click\", (event) => {\n      if (input.disabled) return\n      const next = starFrom(event.target)\n      if (!next) return\n      input.value = next\n      input.dispatchEvent(new Event(\"input\", { bubbles: true }))\n      input.dispatchEvent(new Event(\"change\", { bubbles: true }))\n      input.focus({ preventScroll: true })\n    })\n\n    // The value returns to the initial score after the reset event.\n    input.form?.addEventListener(\"reset\", () => {\n      requestAnimationFrame(() => paint(input.valueAsNumber, true))\n    })\n\n    // Read the range control once. Later changes reuse the input listener.\n    paint(input.valueAsNumber, true)\n  }\n\n  const initialize = () => {\n    for (const root of document.querySelectorAll<HTMLElement>(\n      \"[data-slot='rating'][data-interactive]\",\n    ))\n      bindRating(root)\n  }\n\n  initialize()\n  document.addEventListener(\"astro:page-load\", initialize)\n</script>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/rating/rating.astro"
    },
    {
      "path": "packages/ui/src/components/ui/rating/rating-value.astro",
      "content": "---\nimport type { HTMLAttributes, HTMLTag, Polymorphic } from \"astro/types\"\nimport type { RatingValueProps } from \"./types\"\nimport { cn } from \"cn\"\n\n/**\n * Score text for the parent rating.\n * Element attributes such as `class` pass through.\n * Place this part inside `Rating`. It has no score prop.\n */\ntype Props<T extends HTMLTag = \"span\"> = Polymorphic<{ as: T }> &\n  HTMLAttributes<T> &\n  RatingValueProps\n\nconst { as: Tag = \"span\", class: className, ...props } = Astro.props\n---\n\n<Tag\n  data-slot=\"rating-value\"\n  class={cn(\"text-muted-foreground text-sm tabular-nums\", className)}\n  {...props}\n/>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/rating/rating-value.astro"
    },
    {
      "path": "packages/ui/src/components/ui/rating/types.ts",
      "content": "import type { HTMLTag } from 'astro/types'\n/**\n * Props for a star score.\n * Div attributes such as `class` and `id` pass through.\n * The root element can change with `as`. The default element is `div`.\n */\nexport interface RatingProps {\n    /**\n     * Score to show.\n     * A fraction fills part of the next star.\n     */\n    value: number\n    /**\n     * Let the visitor set the score.\n     * The component then renders a range control. A click on a star sets a\n     * whole number of stars. Arrow keys change that control by one star.\n     *\n     * @default false\n     */\n    interactive?: boolean\n    /**\n     * Number of stars.\n     * Values below 1 become 1.\n     *\n     * @default 5\n     */\n    max?: number\n    /**\n     * Form field name for the range control.\n     * Used only when `interactive` is set. The submitted value is a number\n     * from 0 through `max`.\n     */\n    name?: string\n    /**\n     * Accessible name for the range control.\n     * Used only when `interactive` is set. Use the same words in a visible caption.\n     *\n     * @default \"Rating\"\n     */\n    label?: string\n    /**\n     * Block changes to the score.\n     * Used only when `interactive` is set. A disabled control is not submitted.\n     *\n     * @default false\n     */\n    disabled?: boolean\n    /**\n     * Color of the stars.\n     *\n     * @default \"var(--primary)\"\n     */\n    starColor?: string\n    /**\n     * The type of the element to render.\n     *\n     * @default \"div\"\n     */\n    as?: HTMLTag\n}\n\nexport interface RatingValueProps {\n    /**\n     * Score to show.\n     * A fraction fills part of the next star.\n     */\n    value: number\n    /**\n     * The type of the element to render.\n     *\n     * @default \"span\"\n     */\n    as?: HTMLTag\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/rating/types.ts"
    }
  ],
  "meta": {
    "framework": "astro",
    "source": "@gnostic/ui",
    "testedDependencies": {
      "@lucide/astro": "^1.68.0",
      "cn": "0.5.2"
    }
  },
  "docs": "https://ui.gnosticai.com/components/rating",
  "categories": [
    "ui"
  ],
  "type": "registry:ui"
}
