{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rating",
  "title": "Rating",
  "description": "A score picked with a click or the arrow keys, as stars, hearts, a joined number scale or a row of segments, with half steps and a read-only form.",
  "dependencies": [
    "radix-ui",
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils",
    "https://miseui.junsu4621.workers.dev/r/motion.json"
  ],
  "files": [
    {
      "path": "registry/miseui/ui/rating.tsx",
      "content": "import type * as React from \"react\"\r\nimport { useState } from \"react\"\r\nimport { RadioGroup as RadioGroupPrimitive } from \"radix-ui\"\r\nimport { useReducedMotion } from \"motion/react\"\r\nimport { ROOT_CLASS, defaultItemLabel, glyphFill, ratingSteps, type RatingStep, type RatingVariant } from \"@/registry/miseui/ui/rating-parts\"\r\nimport { RatingMarks } from \"@/registry/miseui/ui/rating-rows\"\r\nimport { cn } from \"@/registry/miseui/lib/utils\"\r\n\r\ntype RatingEffect = \"pop\"\r\n\r\ntype RatingProps = Omit<React.ComponentProps<\"div\">, \"defaultValue\" | \"dir\" | \"onChange\"> & {\r\n  /** `stars` the familiar row with its score, `hearts` larger marks for liking, `numeric` a joined scale, `bar` a row of segments. */\r\n  variant?: RatingVariant\r\n  /** `pop` springs the mark that was just picked. */\r\n  effect?: RatingEffect\r\n  value?: number\r\n  defaultValue?: number\r\n  onValueChange?: (value: number) => void\r\n  max?: number\r\n  /** 0.5 allows half marks. `numeric` always uses whole numbers, since half a digit means nothing. */\r\n  step?: RatingStep\r\n  readOnly?: boolean\r\n  disabled?: boolean\r\n  required?: boolean\r\n  /** Posts the value with the surrounding form. */\r\n  name?: string\r\n  /** The spoken name of each value, for your language. */\r\n  itemLabel?: (value: number, max: number) => string\r\n}\r\n\r\nfunction RatingScore({ variant, shown, max }: { variant: RatingVariant; shown: number; max: number }) {\r\n  if (variant !== \"stars\" && variant !== \"bar\") return null\r\n  const stars = variant === \"stars\"\r\n  return (\r\n    <span data-slot=\"rating-value\" aria-hidden=\"true\" className={cn(\"text-sm tabular-nums text-muted-foreground\", stars && \"ml-2 min-w-6\")}>\r\n      {stars ? shown || null : `${shown} / ${max}`}\r\n    </span>\r\n  )\r\n}\r\n\r\n/** Holds what is picked, what the pointer previews and how many picks have happened, which is what restarts the pop. */\r\nfunction useRatingValue(value: number | undefined, defaultValue: number, pop: boolean, onValueChange?: (value: number) => void) {\r\n  const [uncontrolled, setUncontrolled] = useState(defaultValue)\r\n  const [preview, setPreview] = useState<number | null>(null)\r\n  const [pulse, setPulse] = useState(0)\r\n  const reduceMotion = useReducedMotion() ?? false\r\n  const pick = (next: string) => {\r\n    const picked = Number(next)\r\n    setUncontrolled(picked)\r\n    setPreview(null)\r\n    if (pop && !reduceMotion) setPulse((count) => count + 1)\r\n    onValueChange?.(picked)\r\n  }\r\n  return { current: value ?? uncontrolled, preview, setPreview, pulse, pick }\r\n}\r\n\r\nfunction Rating({ variant = \"stars\", effect, value, defaultValue = 0, onValueChange, max = 5, step = 1, readOnly = false, disabled, required, name, itemLabel = defaultItemLabel, className, \"aria-label\": ariaLabel, ...props }: RatingProps) {\r\n  const { current, preview, setPreview, pulse, pick } = useRatingValue(value, defaultValue, effect === \"pop\", onValueChange)\r\n  const shown = readOnly ? current : (preview ?? current)\r\n  const root = { ...props, \"data-slot\": \"rating\", \"data-variant\": variant, \"data-effect\": effect, \"data-readonly\": readOnly ? \"true\" : undefined }\r\n  const shared = { variant, max, step: variant === \"numeric\" ? 1 : step, shown, picked: current, pulse, labelFor: itemLabel }\r\n  if (readOnly) {\r\n    const spoken = `${ariaLabel ? `${ariaLabel}: ` : \"\"}${itemLabel(current, max)}`\r\n    return (\r\n      <div {...root} role=\"img\" aria-label={spoken} className={cn(ROOT_CLASS[variant], className)}>\r\n        <RatingMarks {...shared} onPreview={null} />\r\n        <RatingScore variant={variant} shown={shown} max={max} />\r\n      </div>\r\n    )\r\n  }\r\n  return (\r\n    <RadioGroupPrimitive.Root\r\n      {...root}\r\n      aria-label={ariaLabel}\r\n      name={name}\r\n      required={required}\r\n      disabled={disabled}\r\n      // Radix treats \"\" as no selection, which is what a score of zero is.\r\n      value={current > 0 ? String(current) : \"\"}\r\n      className={cn(ROOT_CLASS[variant], disabled && \"opacity-50\", className)}\r\n      onValueChange={pick}\r\n      onPointerLeave={() => setPreview(null)}\r\n    >\r\n      <RatingMarks {...shared} onPreview={setPreview} />\r\n      <RatingScore variant={variant} shown={shown} max={max} />\r\n    </RadioGroupPrimitive.Root>\r\n  )\r\n}\r\n\r\nexport { Rating, glyphFill, ratingSteps, type RatingProps, type RatingVariant, type RatingEffect, type RatingStep }\r\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/rating-parts.tsx",
      "content": "import type * as React from \"react\"\nimport { Heart, Star } from \"lucide-react\"\nimport { motion } from \"motion/react\"\nimport { SPRING_DEFAULT } from \"@/registry/miseui/lib/motion\"\nimport { cn } from \"@/registry/miseui/lib/utils\"\n\ntype RatingVariant = \"stars\" | \"hearts\" | \"numeric\" | \"bar\"\ntype RatingStep = 1 | 0.5\ntype GlyphFill = 0 | 0.5 | 1\n\nconst POP_FROM_SCALE = 0.7\n\n/** Every value someone can pick, lowest first. */\nfunction ratingSteps(max: number, step: RatingStep) {\n  const count = Math.round(max / step)\n  return Array.from({ length: count }, (_, index) => (index + 1) * step)\n}\n\n/** How much of the glyph at this 1-based position a value covers. */\nfunction glyphFill(position: number, value: number): GlyphFill {\n  if (value >= position) return 1\n  return value >= position - 0.5 ? 0.5 : 0\n}\n\nconst defaultItemLabel = (value: number, max: number) => `${value} out of ${max}`\n\nconst GLYPH: Record<\"stars\" | \"hearts\", typeof Star> = { stars: Star, hearts: Heart }\nconst GLYPH_CELL_CLASS: Record<\"stars\" | \"hearts\", string> = {\n  stars: \"size-7 pointer-coarse:size-10\",\n  hearts: \"size-9 pointer-coarse:size-10\",\n}\nconst GLYPH_ICON_CLASS: Record<\"stars\" | \"hearts\", string> = {\n  stars: \"size-5\",\n  hearts: \"size-6\",\n}\n\n/** Logical clip, so half a star fills from the reading side in a right-to-left page too. */\nconst HALF_CLIP = \"[clip-path:inset(0_50%_0_0)] rtl:[clip-path:inset(0_0_0_50%)]\"\n\ninterface GlyphProps {\n  variant: \"stars\" | \"hearts\"\n  fill: GlyphFill\n}\n\nfunction Glyph({ variant, fill }: GlyphProps) {\n  const Icon = GLYPH[variant]\n  return (\n    <span aria-hidden=\"true\" data-slot=\"rating-glyph\" data-fill={fill} className=\"relative block\">\n      <Icon className={cn(\"block text-muted-foreground/40\", GLYPH_ICON_CLASS[variant])} strokeWidth={1.5} />\n      {fill > 0 ? (\n        <span className={cn(\"absolute inset-0 text-mise\", fill === 0.5 && HALF_CLIP)}>\n          <Icon className={cn(\"block fill-current\", GLYPH_ICON_CLASS[variant])} strokeWidth={1.5} />\n        </span>\n      ) : null}\n    </span>\n  )\n}\n\n/** Remounted by a new key on each pick, which replays the pop. Nothing renders it until the first pick. */\nfunction Pop({ pulse, children }: { pulse: number; children: React.ReactNode }) {\n  return (\n    <motion.span\n      key={pulse}\n      data-slot=\"rating-pop\"\n      className=\"block\"\n      initial={{ scale: POP_FROM_SCALE }}\n      animate={{ scale: 1 }}\n      transition={SPRING_DEFAULT}\n    >\n      {children}\n    </motion.span>\n  )\n}\n\nconst ROOT_CLASS: Record<RatingVariant, string> = {\n  stars: \"inline-flex items-center\",\n  hearts: \"inline-flex items-center gap-1\",\n  numeric: \"grid w-full min-w-0 auto-cols-fr grid-flow-col divide-x rounded-md border\",\n  bar: \"flex w-full min-w-0 items-center gap-3\",\n}\n\nconst FOCUS_CLASS = \"outline-none focus-visible:z-10 focus-visible:ring-[3px] focus-visible:ring-ring/50\"\n\nexport {\n  FOCUS_CLASS,\n  GLYPH_CELL_CLASS,\n  Glyph,\n  Pop,\n  ROOT_CLASS,\n  defaultItemLabel,\n  glyphFill,\n  ratingSteps,\n  type GlyphFill,\n  type RatingStep,\n  type RatingVariant,\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/rating-rows.tsx",
      "content": "import type * as React from \"react\"\r\nimport { RadioGroup as RadioGroupPrimitive } from \"radix-ui\"\r\nimport {\r\n  FOCUS_CLASS,\r\n  GLYPH_CELL_CLASS,\r\n  Glyph,\r\n  Pop,\r\n  glyphFill,\r\n  ratingSteps,\r\n  type RatingStep,\r\n  type RatingVariant,\r\n} from \"@/registry/miseui/ui/rating-parts\"\r\nimport { cn } from \"@/registry/miseui/lib/utils\"\r\n\r\ninterface RatingShared {\r\n  variant: RatingVariant\r\n  max: number\r\n  step: RatingStep\r\n  /** What to draw: the hovered value while previewing. */\r\n  shown: number\r\n  /** What is actually chosen. The pop stays on this, or moving the pointer would replay it on every mark. */\r\n  picked: number\r\n  pulse: number\r\n  labelFor: (value: number, max: number) => string\r\n  /** Null when read-only: the marks are drawn, but there is nothing to focus or press. */\r\n  onPreview: ((value: number) => void) | null\r\n}\r\n\r\ntype ItemProps = RatingShared & { value: number; className: string; children?: React.ReactNode }\r\n\r\nfunction RatingItem({ value, max, labelFor, onPreview, className, children }: ItemProps) {\r\n  if (!onPreview) return <span className={className}>{children}</span>\r\n  return (\r\n    <RadioGroupPrimitive.Item\r\n      data-slot=\"rating-item\"\r\n      value={String(value)}\r\n      aria-label={labelFor(value, max)}\r\n      className={cn(FOCUS_CLASS, \"disabled:cursor-not-allowed\", className)}\r\n      // A touch has no hover, and a preview set by one would stick until the next touch elsewhere.\r\n      onPointerEnter={(event) => {\r\n        if (event.pointerType !== \"touch\") onPreview(value)\r\n      }}\r\n    >\r\n      {children}\r\n    </RadioGroupPrimitive.Item>\r\n  )\r\n}\r\n\r\nfunction GlyphRow(shared: RatingShared & { variant: \"stars\" | \"hearts\" }) {\r\n  const { variant, max, step, shown, picked, pulse } = shared\r\n  const poppedPosition = Math.ceil(picked)\r\n  return ratingSteps(max, 1).map((position) => {\r\n    const glyph = <Glyph variant={variant} fill={glyphFill(position, shown)} />\r\n    const interactive = shared.onPreview !== null\r\n    return (\r\n      <span key={position} className={cn(\"relative flex items-center justify-center\", GLYPH_CELL_CLASS[variant])}>\r\n        {pulse > 0 && position === poppedPosition ? <Pop pulse={pulse}>{glyph}</Pop> : glyph}\r\n        {interactive && step === 0.5 ? (\r\n          <RatingItem {...shared} value={position - 0.5} className=\"absolute inset-y-0 start-0 w-1/2 rounded-s-sm\" />\r\n        ) : null}\r\n        {interactive ? (\r\n          <RatingItem\r\n            {...shared}\r\n            value={position}\r\n            className={step === 0.5 ? \"absolute inset-y-0 end-0 w-1/2 rounded-e-sm\" : \"absolute inset-0 rounded-sm\"}\r\n          />\r\n        ) : null}\r\n      </span>\r\n    )\r\n  })\r\n}\r\n\r\nfunction NumberRow(shared: RatingShared) {\r\n  const { max, shown, picked, pulse } = shared\r\n  return ratingSteps(max, 1).map((value) => {\r\n    const active = shown === value\r\n    return (\r\n      <RatingItem\r\n        key={value}\r\n        {...shared}\r\n        value={value}\r\n        className={cn(\r\n          \"relative flex h-9 min-w-0 items-center justify-center text-sm tabular-nums pointer-coarse:h-10\",\r\n          \"transition-colors duration-(--duration-fast) ease-(--ease-mise) first:rounded-l-md last:rounded-r-md\",\r\n          active ? \"bg-mise text-mise-foreground\" : \"text-muted-foreground\",\r\n        )}\r\n      >\r\n        {value === picked && pulse > 0 ? <Pop pulse={pulse}>{value}</Pop> : value}\r\n      </RatingItem>\r\n    )\r\n  })\r\n}\r\n\r\nfunction BarRow(shared: RatingShared) {\r\n  const { max, step, shown } = shared\r\n  return (\r\n    <span className=\"grid min-w-0 flex-1 auto-cols-fr grid-flow-col gap-1\">\r\n      {ratingSteps(max, step).map((value) => (\r\n        <RatingItem key={value} {...shared} value={value} className=\"relative block h-7 min-w-0 rounded-sm pointer-coarse:h-10\">\r\n          <span\r\n            aria-hidden=\"true\"\r\n            data-slot=\"rating-segment\"\r\n            data-filled={shown >= value ? \"true\" : \"false\"}\r\n            className={cn(\r\n              \"absolute inset-x-0 top-1/2 h-1.5 -translate-y-1/2 rounded-full transition-colors duration-(--duration-fast) ease-(--ease-mise)\",\r\n              shown >= value ? \"bg-mise\" : \"bg-muted\",\r\n            )}\r\n          />\r\n        </RatingItem>\r\n      ))}\r\n    </span>\r\n  )\r\n}\r\n\r\nfunction RatingMarks(shared: RatingShared) {\r\n  const { variant } = shared\r\n  if (variant === \"numeric\") return <NumberRow {...shared} />\r\n  if (variant === \"bar\") return <BarRow {...shared} />\r\n  return <GlyphRow {...shared} variant={variant} />\r\n}\r\n\r\nexport { RatingMarks, type RatingShared }\r\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "forms"
  ],
  "type": "registry:ui"
}