{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "wave-rating",
  "title": "Wave Rating",
  "description": "The rating, animated: sweeping a finger, pen or mouse across the stars lifts them in a wave that crests under the pointer, a tip shows the score, and letting go pops the chosen star. Same props and radio group as rating.",
  "dependencies": [
    "@radix-ui/react-radio-group"
  ],
  "registryDependencies": [
    "@corsair-ui/utils",
    "@corsair-ui/rating",
    "@corsair-ui/use-media-query"
  ],
  "files": [
    {
      "path": "registry/default/ui/wave-rating.tsx",
      "content": "\"use client\";\n\nimport * as RadioGroupPrimitive from \"@radix-ui/react-radio-group\";\nimport { useRef, useState, type CSSProperties, type MouseEvent, type PointerEvent } from \"react\";\n\nimport { useMediaQuery } from \"@/registry/default/hooks/use-media-query\";\nimport { cn } from \"@/registry/default/lib/utils\";\nimport { Rating, RatingStar, ratingVariants, type RatingProps } from \"@/registry/default/ui/rating\";\n\ninterface WaveRatingProps extends RatingProps {\n  /** Shows a label over the star under the pointer while it sweeps or hovers. */\n  showTip?: boolean;\n  /** Text of that label. Defaults to `starLabel` (\"3 stars\"). */\n  tipLabel?: (stars: number, max: number) => string;\n  /** A short vibration as each star lights up under a finger, where the browser supports it. */\n  haptics?: boolean;\n}\n\n/** How many stars behind the crest the wave reaches. */\nconst SPREAD = 3;\n/** Delay between neighbouring stars as the wave travels, in ms. */\nconst STAGGER = 30;\n/** Overshoots a little before settling, like a spring. */\nconst SPRING = \"cubic-bezier(0.34, 1.56, 0.64, 1)\";\nconst POP: Keyframe[] = [\n  { transform: \"scale(1)\" },\n  { transform: \"scale(1.35)\" },\n  { transform: \"scale(1)\" },\n];\nconst HOP: Keyframe[] = [\n  { transform: \"translateY(0)\" },\n  { transform: \"translateY(-4px)\" },\n  { transform: \"translateY(0)\" },\n];\n\nconst defaultStarLabel = (stars: number) => `${stars} ${stars === 1 ? \"star\" : \"stars\"}`;\n\nfunction play(element: Element | null | undefined, keyframes: Keyframe[], duration: number) {\n  if (element && typeof element.animate === \"function\") {\n    element.animate(keyframes, { duration, easing: SPRING });\n  }\n}\n\n/** 0 to 1: how high a star rises, highest under the pointer and fading out behind it. */\nfunction liftOf(star: number, crest: number | null) {\n  if (crest === null || star > crest) return 0;\n  return Math.max(0, 1 - (crest - star) / SPREAD);\n}\n\n/**\n * The rating, animated: sweep a finger, pen or mouse across the stars and they\n * rise in a wave that crests under it, while a tip above shows the score; let\n * go (or click) and the chosen star pops. It takes the same props as `rating`\n * and keeps everything else about it: a radio group for the keyboard and\n * screen readers, form submission under `name`, and the read-only image (which\n * does not move). The tip is decorative, since each radio already says its\n * score. Vertical swipes still scroll the page. With `prefers-reduced-motion`\n * the stars light up without rising, popping or travelling.\n *\n * Each rising star exposes its height as `--wave-rating-lift` (0 to 1) on\n * `data-slot=\"wave-rating-lift\"`, for a glow or a colour of your own.\n *\n * @example\n * <WaveRating\n *   defaultValue={4}\n *   onValueChange={setScore}\n *   aria-label=\"Rate the court\"\n *   tipLabel={(stars) => [\"Bad\", \"Meh\", \"OK\", \"Good\", \"Great\"][stars - 1] ?? \"\"}\n * />\n */\nfunction WaveRating({ showTip, tipLabel, haptics, ...props }: WaveRatingProps) {\n  if (props.readOnly) return <Rating {...props} />;\n  return (\n    <InteractiveWaveRating showTip={showTip} tipLabel={tipLabel} haptics={haptics} {...props} />\n  );\n}\n\nfunction InteractiveWaveRating({\n  value: valueProp,\n  defaultValue = 0,\n  onValueChange,\n  max = 5,\n  disabled,\n  required,\n  size = \"default\",\n  name,\n  dir,\n  \"aria-label\": label = \"Rating\",\n  starLabel = defaultStarLabel,\n  showTip = true,\n  tipLabel,\n  haptics = true,\n  getValueLabel: _getValueLabel,\n  readOnly: _readOnly,\n  className,\n  onPointerDown,\n  onPointerMove,\n  onPointerUp,\n  onPointerCancel,\n  onPointerLeave,\n  onClickCapture,\n  ...props\n}: WaveRatingProps) {\n  const reduced = useMediaQuery(\"(prefers-reduced-motion: reduce)\");\n  const [internal, setInternal] = useState(defaultValue);\n  const [preview, setPreview] = useState<number | null>(null);\n  const [tip, setTip] = useState<{ star: number; x: number; moving: boolean } | null>(null);\n  const count = Math.max(1, Math.floor(max));\n  const value = Math.min(Math.max(valueProp ?? internal, 0), count);\n  const picked = Math.round(value);\n  const shown = preview ?? picked;\n  const stars = Array.from({ length: count }, (_, index) => index + 1);\n\n  const items = useRef<(HTMLButtonElement | null)[]>([]);\n  const pops = useRef<(HTMLSpanElement | null)[]>([]);\n  const bubble = useRef<HTMLSpanElement>(null);\n  const crest = useRef<number | null>(null);\n  const sweep = useRef<number | null>(null);\n  const swallowClick = useRef(false);\n\n  /** The star nearest to the pointer, so sweeping past either end keeps the first or last. */\n  const starAt = (clientX: number) => {\n    let nearest: number | null = null;\n    let distance = Infinity;\n    items.current.forEach((item, index) => {\n      if (!item) return;\n      const { left, right } = item.getBoundingClientRect();\n      const away = clientX < left ? left - clientX : clientX > right ? clientX - right : 0;\n      if (away < distance) {\n        distance = away;\n        nearest = index + 1;\n      }\n    });\n    return nearest;\n  };\n\n  const show = (star: number, pointerType: string) => {\n    if (crest.current === star) return;\n    const moving = crest.current !== null;\n    crest.current = star;\n    setPreview(star);\n    const item = items.current[star - 1];\n    if (item) setTip({ star, x: item.offsetLeft + item.offsetWidth / 2, moving });\n    if (moving && !reduced) play(bubble.current, HOP, 200);\n    if (haptics && pointerType === \"touch\" && typeof navigator.vibrate === \"function\") {\n      navigator.vibrate(8);\n    }\n  };\n\n  const hide = () => {\n    crest.current = null;\n    setPreview(null);\n  };\n\n  const commit = (star: number) => {\n    if (valueProp === undefined) setInternal(star);\n    if (star !== picked) onValueChange?.(star);\n    if (!reduced) play(pops.current[star - 1], POP, 360);\n  };\n\n  const begin = (event: PointerEvent<HTMLDivElement>) => {\n    onPointerDown?.(event);\n    if (event.defaultPrevented || disabled || event.button !== 0) return;\n    // The star pressed is the one under the pointer; assistive tech may not send coordinates.\n    const pressed = items.current.findIndex(\n      (item) => item !== null && item.contains(event.target as Node)\n    );\n    const star = pressed >= 0 ? pressed + 1 : starAt(event.clientX);\n    if (star === null) return;\n    sweep.current = event.pointerId;\n    event.currentTarget.setPointerCapture(event.pointerId);\n    show(star, event.pointerType);\n  };\n\n  const move = (event: PointerEvent<HTMLDivElement>) => {\n    onPointerMove?.(event);\n    if (disabled) return;\n    if (sweep.current === null ? event.pointerType === \"touch\" : sweep.current !== event.pointerId)\n      return;\n    const star = starAt(event.clientX);\n    if (star !== null) show(star, event.pointerType);\n  };\n\n  const end = (event: PointerEvent<HTMLDivElement>) => {\n    onPointerUp?.(event);\n    if (sweep.current !== event.pointerId) return;\n    sweep.current = null;\n    const star = crest.current;\n    // A mouse is still over the stars, so its preview stays until it leaves.\n    if (event.pointerType !== \"mouse\") hide();\n    if (star === null) return;\n    // The click that follows the release would pick the radio under the pointer again.\n    swallowClick.current = true;\n    setTimeout(() => {\n      swallowClick.current = false;\n    });\n    commit(star);\n    if (event.currentTarget.contains(document.activeElement)) items.current[star - 1]?.focus();\n  };\n\n  const cancel = (event: PointerEvent<HTMLDivElement>) => {\n    onPointerCancel?.(event);\n    if (sweep.current !== event.pointerId) return;\n    sweep.current = null;\n    hide();\n  };\n\n  const leave = (event: PointerEvent<HTMLDivElement>) => {\n    onPointerLeave?.(event);\n    if (sweep.current === null) hide();\n  };\n\n  const swallow = (event: MouseEvent<HTMLDivElement>) => {\n    onClickCapture?.(event);\n    if (!swallowClick.current) return;\n    swallowClick.current = false;\n    event.preventDefault();\n    event.stopPropagation();\n  };\n\n  return (\n    <RadioGroupPrimitive.Root\n      data-slot=\"wave-rating\"\n      data-size={size}\n      data-value={picked}\n      data-previewing={preview !== null ? \"\" : undefined}\n      aria-label={label}\n      value={picked > 0 ? String(picked) : \"\"}\n      onValueChange={(next) => commit(Number(next))}\n      orientation=\"horizontal\"\n      loop={false}\n      disabled={disabled}\n      required={required}\n      name={name}\n      dir={dir}\n      className={cn(ratingVariants({ size }), \"relative touch-pan-y select-none\", className)}\n      onPointerDown={begin}\n      onPointerMove={move}\n      onPointerUp={end}\n      onPointerCancel={cancel}\n      onPointerLeave={leave}\n      onClickCapture={swallow}\n      {...props}\n    >\n      {stars.map((star) => {\n        const lift = reduced ? 0 : liftOf(star, preview);\n        return (\n          <RadioGroupPrimitive.Item\n            key={star}\n            ref={(node) => {\n              items.current[star - 1] = node;\n            }}\n            value={String(star)}\n            aria-label={starLabel(star)}\n            data-slot=\"wave-rating-item\"\n            data-previewed={preview !== null && star <= preview ? \"\" : undefined}\n            className={cn(\n              \"inline-flex cursor-pointer rounded-[4px] outline-none\",\n              \"focus-visible:ring-ring/50 focus-visible:ring-[3px]\",\n              \"disabled:cursor-not-allowed disabled:opacity-50\"\n            )}\n          >\n            <span\n              data-slot=\"wave-rating-lift\"\n              className=\"inline-flex transition-transform duration-300 ease-[cubic-bezier(0.34,1.56,0.64,1)] motion-reduce:transition-none\"\n              style={\n                {\n                  \"--wave-rating-lift\": lift,\n                  transform:\n                    lift > 0 ? `translateY(${-lift * 35}%) scale(${1 + lift * 0.2})` : undefined,\n                  // The wave travels: stars further behind the crest move a moment later.\n                  transitionDelay:\n                    lift > 0 && preview !== null ? `${(preview - star) * STAGGER}ms` : \"0ms\",\n                } as CSSProperties\n              }\n            >\n              <span\n                ref={(node) => {\n                  pops.current[star - 1] = node;\n                }}\n                className=\"inline-flex\"\n              >\n                <RatingStar fill={star <= shown ? 1 : 0} />\n              </span>\n            </span>\n          </RadioGroupPrimitive.Item>\n        );\n      })}\n      {showTip && tip ? (\n        <span\n          aria-hidden=\"true\"\n          data-slot=\"wave-rating-tip\"\n          className={cn(\n            \"pointer-events-none absolute bottom-full left-0 mb-2 ease-out motion-reduce:transition-none\",\n            tip.moving\n              ? \"transition-[transform,opacity] duration-200\"\n              : \"transition-opacity duration-200\",\n            preview === null ? \"opacity-0\" : \"opacity-100\"\n          )}\n          style={{ transform: `translateX(${tip.x}px) translateX(-50%)` }}\n        >\n          <span\n            ref={bubble}\n            className=\"bg-foreground text-background block rounded-md px-2 py-1 text-xs font-medium whitespace-nowrap shadow-md\"\n          >\n            {(tipLabel ?? starLabel)(tip.star, count)}\n          </span>\n        </span>\n      ) : null}\n    </RadioGroupPrimitive.Root>\n  );\n}\n\nexport { WaveRating, type WaveRatingProps };\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "variantOf": "rating"
  },
  "categories": [
    "motion"
  ],
  "type": "registry:ui"
}