{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rating",
  "title": "Rating",
  "description": "A star rating built on a radio group: point to preview a score, click or use the arrow keys to pick one, in whole or half stars. The fill wipes along the row from the old score to the new one. Read only, it shows any fraction, such as 4.3.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core",
    "class-variance-authority"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/choice.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/rating.tsx",
      "content": "'use client'\n\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { choicePressingClassName } from '@/registry/lib/choice'\nimport { renderElement } from '@ui-sdk/react/primitive'\nimport {\n  Rating as RatingPrimitive,\n  RatingItem as RatingItemPrimitive,\n} from '@ui-sdk/react/rating'\nimport { cva } from 'class-variance-authority'\nimport {\n  type ComponentProps,\n  type CSSProperties,\n  createContext,\n  type KeyboardEvent,\n  type PointerEvent,\n  use,\n  useState,\n} from 'react'\n\nimport { cn } from '@/lib/utils'\n\ntype RatingProps = Omit<\n  ComponentProps<typeof RatingPrimitive>,\n  'className' | 'children' | 'step'\n> & {\n  className?: string\n  /** `0.5` lets a person pick half a star. */\n  step?: 0.5 | 1\n  /** `sm` fits a read-only score to a line of small text, where its stars are no targets. */\n  size?: 'default' | 'sm'\n}\n\nconst defaultValueLabel = (value: number, max: number) => `${value} out of ${max} stars`\n\n// Each star sits in a cell as large as the density's smallest control, which\n// is what the pointer aims at: a whole star is one target, and with half steps\n// the half it lands on picks the score, as a slider's track does. In a column\n// narrower than the row, the cells share it and the stars shrink to fit.\nconst ratingVariants = cva(\n  'group/rating relative inline-flex w-[calc(var(--rating-max)*var(--rating-cell))] max-w-full',\n  {\n    variants: {\n      size: {\n        default: '[--rating-cell:var(--control-height-xs)] [--rating-star:1.25rem]',\n        // A line of text-sm is 1.25rem: the stars sit on it.\n        sm: '[--rating-cell:1.25rem] [--rating-star:1rem]',\n      },\n    },\n    defaultVariants: { size: 'default' },\n  },\n)\n\nconst cellClassName =\n  'relative flex h-(--rating-cell) min-w-0 flex-1 items-center justify-center'\n\nconst starClassName =\n  'flex size-full items-center justify-center [&_svg]:h-(--rating-star) [&_svg]:w-[min(var(--rating-star),100%)]'\n\n// Phosphor fills a star with its weight, which the other libraries ignore.\nconst phosphorFill = { weight: 'fill' } as const\n\nconst emptyStarClassName = cn(\n  choicePressingClassName,\n  starClassName,\n  'text-muted-foreground group-aria-invalid/rating:text-destructive',\n)\n\n// The filled stars are one layer as wide as the row, clipped to the score, so\n// a new score wipes along the row from the old one in a single move and turns\n// back from wherever it is when the score changes again.\nconst fillClassName =\n  'text-primary pointer-events-none absolute inset-0 flex transition-[clip-path] duration-(--motion-draw-duration) ease-(--motion-draw-easing) [clip-path:inset(0_calc(100%_-_var(--rating-reach))_0_0)] motion-reduce:transition-none rtl:[clip-path:inset(0_0_0_calc(100%_-_var(--rating-reach)))] forced-colors:text-[Highlight] [&_svg]:fill-current'\n\n/** The star a press is on, by pointer or Space, for its stars to sink a little. */\nconst PressContext = createContext<{\n  pressed: number | null\n  press: (index: number | null) => void\n}>({ pressed: null, press: () => {} })\n\nfunction EmptyStar({ pressing }: { pressing: boolean }) {\n  return (\n    <span data-pressing={pressing || undefined} className={emptyStarClassName}>\n      <IconPlaceholder\n        lucide='StarIcon'\n        tabler='IconStar'\n        hugeicons='StarIcon'\n        phosphor='StarIcon'\n        remixicon='RiStarLine'\n        aria-hidden\n      />\n    </span>\n  )\n}\n\n/** The filled stars, one layer over the row, clipped to the score shown. */\nfunction Fill({\n  max,\n  shown,\n  pressed,\n}: {\n  max: number\n  shown: number\n  pressed: number | null\n}) {\n  return (\n    <span\n      aria-hidden\n      data-rating-fill=''\n      className={fillClassName}\n      style={{ '--rating-reach': ratingFill(shown, max) } as CSSProperties}\n    >\n      {Array.from({ length: max }, (_, at) => (\n        // oxlint-disable-next-line react/no-array-index-key -- a star is its place in the row\n        <span key={at} className={cellClassName}>\n          <span\n            data-pressing={pressed === at + 1 || undefined}\n            className={cn(choicePressingClassName, starClassName)}\n          >\n            <IconPlaceholder\n              lucide='StarIcon'\n              tabler='IconStarFilled'\n              hugeicons='StarIcon'\n              phosphor='StarIcon'\n              remixicon='RiStarFill'\n              aria-hidden\n              {...phosphorFill}\n            />\n          </span>\n        </span>\n      ))}\n    </span>\n  )\n}\n\n/** One star's cell: its empty icon, under ui-sdk's radios for its score. */\nfunction RatingItem() {\n  const { pressed, press } = use(PressContext)\n  return (\n    <RatingItemPrimitive\n      onPointerDown={(event: PointerEvent<HTMLSpanElement>) => {\n        if (event.button === 0) press(Number(event.currentTarget.dataset.index))\n      }}\n      onKeyDown={(event: KeyboardEvent<HTMLSpanElement>) => {\n        if (event.key === ' ') press(Number(event.currentTarget.dataset.index))\n      }}\n      onKeyUp={() => press(null)}\n      onBlur={() => press(null)}\n      render={(props, { index }) => (\n        <span {...props} data-index={index}>\n          {props.children}\n        </span>\n      )}\n      // A half's radio covers the star's first half and the whole's the rest, so the ring shows which is focused.\n      className={cn(\n        cellClassName,\n        '[&_[role=radio]]:rounded-(--control-radius-xs) [&_[role=radio]]:focus-visible:ring-3 [&_[role=radio]]:focus-visible:ring-ring/50 [&_[role=radio]]:focus-visible:outline-hidden',\n      )}\n    >\n      {({ index }) => <EmptyStar pressing={pressed === index} />}\n    </RatingItemPrimitive>\n  )\n}\n\nfunction Rating({\n  className,\n  max = 5,\n  getValueLabel = defaultValueLabel,\n  readOnly,\n  size,\n  style,\n  render,\n  onPointerLeave,\n  onPointerUp,\n  onPointerCancel,\n  ...props\n}: RatingProps) {\n  const [pressed, press] = useState<number | null>(null)\n  return (\n    <PressContext value={{ pressed, press }}>\n      <RatingPrimitive\n        max={max}\n        getValueLabel={getValueLabel}\n        readOnly={readOnly}\n        onPointerLeave={(event) => {\n          press(null)\n          onPointerLeave?.(event)\n        }}\n        onPointerUp={(event) => {\n          press(null)\n          onPointerUp?.(event)\n        }}\n        onPointerCancel={(event) => {\n          press(null)\n          onPointerCancel?.(event)\n        }}\n        style={(state) =>\n          ({\n            ...(typeof style === 'function' ? style(state) : style),\n            '--rating-max': max,\n          }) as CSSProperties\n        }\n        className={cn(\n          ratingVariants({ size }),\n          !readOnly &&\n            'data-disabled:pointer-events-none data-disabled:opacity-50 forced-colors:aria-invalid:outline-2 forced-colors:aria-invalid:outline-dashed',\n          className,\n        )}\n        // The filled stars are one layer after the stars, so a new score wipes along the row.\n        render={(rootProps, state) => {\n          const children = (\n            <>\n              {rootProps.children}\n              <Fill\n                max={max}\n                shown={state.highlighted ?? state.value ?? 0}\n                pressed={readOnly ? null : pressed}\n              />\n            </>\n          )\n          return renderElement('div', { ...rootProps, children }, render, state)\n        }}\n        {...props}\n      >\n        {/* Each star, which ui-sdk repeats along the row. A score that can't change is one image: its stars hold no radios, which a screen reader could still reach. */}\n        {readOnly ? (\n          <span aria-hidden className={cellClassName}>\n            <EmptyStar pressing={false} />\n          </span>\n        ) : (\n          <RatingItem />\n        )}\n      </RatingPrimitive>\n    </PressContext>\n  )\n}\n\n/**\n * How far along the row the filled stars reach for `value`, as a CSS length\n * in percent of the row: every whole star's cell, then that share of the next\n * star's own width, which is `--rating-star` or its cell's, if narrower.\n */\nfunction ratingFill(value: number, max: number) {\n  const score = Math.min(max, Math.max(0, value))\n  const whole = Math.floor(score)\n  const part = Math.round((score - whole) * 1000) / 1000\n  const cell = `(100% / ${max})`\n  if (part === 0) return `calc(${whole} * ${cell})`\n  const star = `min(var(--rating-star), ${cell})`\n  return `calc(${whole} * ${cell} + (${cell} - ${star}) / 2 + ${part} * ${star})`\n}\n\nexport { Rating }\nexport { ratingFill }\nexport type { RatingProps }\n",
      "type": "registry:component",
      "target": "@components/kino/rating.tsx"
    }
  ],
  "type": "registry:component"
}