Skip to content
Atoms

Rating

Stars with hover preview and half steps.

Installation

  1. Run the CLI

    Run pnpm dlx asta-ui@latest init once, then this. The file lands in components/astabyte/ and the colour tokens go into your CSS.

    $ pnpm dlx asta-ui@latest add rating

    Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/rating.json

  2. Or install the package

    Don’t want the file? Import from asta-ui/rating and update with pnpm update asta-ui. Add the stylesheet once. More.

    $ pnpm add asta-ui
    import { Rating } from "asta-ui/rating";
  3. Or copy it by hand

    Copy this file. Keep the paths, or fix the @/ imports. Then add the tokens from globals.css.

    "use client";
    
    import { useState, type KeyboardEvent } from "react";
    
    const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
    
    const KEYFRAMES = `@keyframes asta-rating-pop { 0% { transform: scale(1) } 40% { transform: scale(1.3) } 100% { transform: scale(1) } }`;
    
    // A five-point star with softened corners (the stroke rounds them).
    const STAR = "m12 3.8 2.5 5.1 5.6.8-4 4 .9 5.6-5-2.7-5 2.7.9-5.6-4-4 5.6-.8Z";
    
    export interface RatingProps {
      value?: number;
      defaultValue?: number;
      onChange?: (value: number) => void;
      /** Number of stars. */
      max?: number;
      /** Allow half stars (3.5). */
      allowHalf?: boolean;
      /** Clicking the current value again clears it to 0. */
      allowClear?: boolean;
      /** Show the value without letting anyone change it. */
      readOnly?: boolean;
      size?: "sm" | "md";
      /** Accessible name, e.g. "Rate this course". */
      label?: string;
      className?: string;
    }
    
    function StarIcon({ fill, preview }: { fill: 0 | 0.5 | 1; preview: boolean }) {
      return (
        <span className="relative block h-full w-full">
          <svg viewBox="0 0 24 24" className="absolute inset-0 h-full w-full text-[color-mix(in_srgb,var(--color-ink)_13%,transparent)]" aria-hidden>
            <path d={STAR} fill="currentColor" stroke="currentColor" strokeWidth="1.75" strokeLinejoin="round" />
          </svg>
          <svg
            viewBox="0 0 24 24"
            className={cx("absolute inset-0 h-full w-full transition-opacity duration-200", preview ? "text-[color-mix(in_srgb,var(--color-brand)_62%,transparent)]" : "text-brand")}
            style={{ opacity: fill ? 1 : 0, clipPath: fill === 0.5 ? "inset(0 50% 0 0)" : undefined }}
            aria-hidden
          >
            <path d={STAR} fill="currentColor" stroke="currentColor" strokeWidth="1.75" strokeLinejoin="round" />
          </svg>
        </span>
      );
    }
    
    /**
     * A star rating. Hover to preview, click to set, and with `allowHalf` the left half of a star sets a half.
     * The filled stars pop one after another when the value changes. Arrow keys step, Home/End jump, digits set.
     */
    export function Rating({ value, defaultValue = 0, onChange, max = 5, allowHalf, allowClear = true, readOnly, size = "md", label = "Rating", className }: RatingProps) {
      const [inner, setInner] = useState(defaultValue);
      const current = value ?? inner;
      const [hover, setHover] = useState<number | null>(null);
      const [pop, setPop] = useState(0);
      const [prev, setPrev] = useState(current);
    
      // Pop on every change, controlled or not.
      if (prev !== current) {
        setPrev(current);
        setPop((p) => p + 1);
      }
    
      const step = allowHalf ? 0.5 : 1;
      const px = size === "md" ? 24 : 16;
    
      // Clearing by picking the current value again is a pointer gesture only. Keys never clear.
      const set = (v: number, click = false) => {
        let next = Math.min(max, Math.max(0, Math.round(v / step) * step));
        if (click && next === current && allowClear && v > 0) next = 0;
        if (next === current) return;
        if (value === undefined) setInner(next);
        onChange?.(next);
      };
    
      const at = (e: { currentTarget: HTMLElement; clientX: number }, i: number) => {
        const r = e.currentTarget.getBoundingClientRect();
        return allowHalf && e.clientX - r.left < r.width / 2 ? i + 0.5 : i + 1;
      };
    
      const onKey = (e: KeyboardEvent) => {
        const k = e.key;
        if (k === "ArrowRight" || k === "ArrowUp") set(Math.min(max, current + step));
        else if (k === "ArrowLeft" || k === "ArrowDown") set(Math.max(0, current - step));
        else if (k === "Home") set(0);
        else if (k === "End") set(max);
        else if (/^[0-9]$/.test(k) && Number(k) <= max) set(Number(k));
        else return;
        e.preventDefault();
      };
    
      const shown = hover ?? current;
      const filled = Math.ceil(current);
    
      const stars = Array.from({ length: max }, (_, i) => {
        const fill: 0 | 0.5 | 1 = shown >= i + 1 ? 1 : shown >= i + 0.5 ? 0.5 : 0;
        return (
          <span
            key={i}
            onPointerMove={readOnly ? undefined : (e) => setHover(at(e, i))}
            onPointerDown={readOnly ? undefined : (e) => e.preventDefault()}
            onClick={readOnly ? undefined : (e) => set(at(e, i), true)}
            className={cx("block shrink-0", !readOnly && "cursor-pointer")}
            style={{ width: px, height: px }}
          >
            <span
              key={pop}
              className={cx("block h-full w-full", pop > 0 && i < filled && "animate-[asta-rating-pop_.42s_cubic-bezier(.16,1,.3,1)_both] motion-reduce:animate-none")}
              style={{ animationDelay: `${i * 45}ms` }}
            >
              <StarIcon fill={fill} preview={hover !== null && hover !== current} />
            </span>
          </span>
        );
      });
    
      const text = `${current} out of ${max} stars`;
    
      if (readOnly) {
        return (
          <span role="img" aria-label={`${label}: ${text}`} className={cx("inline-flex items-center", size === "md" ? "gap-1" : "gap-0.5", className)}>
            <style href="asta-rating" precedence="asta">{KEYFRAMES}</style>
            {stars}
          </span>
        );
      }
    
      return (
        <div
          role="slider"
          tabIndex={0}
          aria-label={label}
          aria-valuemin={0}
          aria-valuemax={max}
          aria-valuenow={current}
          aria-valuetext={text}
          onKeyDown={onKey}
          onPointerLeave={() => setHover(null)}
          className={cx(
            "inline-flex select-none items-center rounded-md outline-none [touch-action:manipulation] focus-visible:ring-2 focus-visible:ring-brand/50 focus-visible:ring-offset-2 focus-visible:ring-offset-paper",
            size === "md" ? "gap-1" : "gap-0.5",
            className,
          )}
        >
          <style href="asta-rating" precedence="asta">{KEYFRAMES}</style>
          {stars}
        </div>
      );
    }
    
    export default Rating;
    
  4. Or ask your AI agent

    Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.

    Add the Astabyte UI Rating component to my project.
    
    Stars with hover preview and half steps.
    It includes: hover, half, readOnly, sm, md, keys.
    
    Install it:
    ```sh
    pnpm dlx asta-ui@latest add rating --radix
    ```
    Or with the shadcn CLI:
    ```sh
    pnpm dlx shadcn@latest add https://ui.astabyte.com/r/rating.json
    ```
    If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4).
    
    Import it like this:
    ```tsx
    import { Rating } from "@/components/astabyte/rating";
    ```
    
    Usage example and props: https://ui.astabyte.com/rating
    Full catalogue for context: https://ui.astabyte.com/llms.txt
    
    Rules:
    - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS.
    - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback.
    - Keep keyboard, focus and ARIA behaviour.
    - Use fake data and fake brand names in examples.
    
    Use it to: [describe where it goes and what it should do]
  5. Or use plain HTML + CSS

    Not on React? Use it from Django, Rails, Laravel, Go html/template, htmx or a static page. Load asta.css and asta.js once (the head.html tab), then paste the markup. The ui.astabyte.com/cdn link is temporary: pin a version from jsDelivr for production. More in HTML + CSS.

    <!-- Works without JS (radios); asta.js adds hover preview and the pop. -->
    <form style="display:flex;flex-direction:column;align-items:flex-start;gap:16px"><div style="display:flex;flex-wrap:wrap;align-items:flex-start;gap:24px"><div role="radiogroup" aria-label="Rate Astabyte" data-asta-rating data-value="3" data-clear class="asta-rating"><input type="radio" name="stars" value="0" aria-label="No rating" > <span class="asta-rating-star" style="width:24px;height:24px;" data-asta-star data-fill="full"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:0ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span> <label class="asta-rating-hit"><input type="radio" name="stars" value="1" aria-label="1 stars" ></label></span><span class="asta-rating-star" style="width:24px;height:24px;" data-asta-star data-fill="full"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:45ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span> <label class="asta-rating-hit"><input type="radio" name="stars" value="2" aria-label="2 stars" ></label></span><span class="asta-rating-star" style="width:24px;height:24px;" data-asta-star data-fill="full"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:90ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span> <label class="asta-rating-hit"><input type="radio" name="stars" value="3" checked aria-label="3 stars" ></label></span><span class="asta-rating-star" style="width:24px;height:24px;" data-asta-star data-fill="none"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:135ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span> <label class="asta-rating-hit"><input type="radio" name="stars" value="4" aria-label="4 stars" ></label></span><span class="asta-rating-star" style="width:24px;height:24px;" data-asta-star data-fill="none"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:180ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span> <label class="asta-rating-hit"><input type="radio" name="stars" value="5" aria-label="5 stars" ></label></span></div><div role="radiogroup" aria-label="Rate the docs" data-asta-rating data-value="3.5" data-clear class="asta-rating"><input type="radio" name="half" value="0" aria-label="No rating" > <span class="asta-rating-star" style="width:24px;height:24px;" data-asta-star data-fill="full"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:0ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span><label class="asta-rating-hit-left"><input type="radio" name="half" value="0.5" aria-label="0.5 stars" ></label> <label class="asta-rating-hit-right"><input type="radio" name="half" value="1" aria-label="1 stars" ></label></span><span class="asta-rating-star" style="width:24px;height:24px;" data-asta-star data-fill="full"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:45ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span><label class="asta-rating-hit-left"><input type="radio" name="half" value="1.5" aria-label="1.5 stars" ></label> <label class="asta-rating-hit-right"><input type="radio" name="half" value="2" aria-label="2 stars" ></label></span><span class="asta-rating-star" style="width:24px;height:24px;" data-asta-star data-fill="full"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:90ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span><label class="asta-rating-hit-left"><input type="radio" name="half" value="2.5" aria-label="2.5 stars" ></label> <label class="asta-rating-hit-right"><input type="radio" name="half" value="3" aria-label="3 stars" ></label></span><span class="asta-rating-star" style="width:24px;height:24px;" data-asta-star data-fill="half"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:135ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span><label class="asta-rating-hit-left"><input type="radio" name="half" value="3.5" checked aria-label="3.5 stars" ></label> <label class="asta-rating-hit-right"><input type="radio" name="half" value="4" aria-label="4 stars" ></label></span><span class="asta-rating-star" style="width:24px;height:24px;" data-asta-star data-fill="none"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:180ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span><label class="asta-rating-hit-left"><input type="radio" name="half" value="4.5" aria-label="4.5 stars" ></label> <label class="asta-rating-hit-right"><input type="radio" name="half" value="5" aria-label="5 stars" ></label></span></div><span role="img" aria-label="Average rating: 4.5 out of 5 stars" class="asta-rating asta-rating-static asta-rating-sm"><span class="asta-rating-star" style="width:16px;height:16px;" data-asta-star data-fill="full"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:0ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span></span><span class="asta-rating-star" style="width:16px;height:16px;" data-asta-star data-fill="full"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:45ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span></span><span class="asta-rating-star" style="width:16px;height:16px;" data-asta-star data-fill="full"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:90ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span></span><span class="asta-rating-star" style="width:16px;height:16px;" data-asta-star data-fill="full"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:135ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span></span><span class="asta-rating-star" style="width:16px;height:16px;" data-asta-star data-fill="half"><span class="asta-rating-pop" data-asta-star-pop style="animation-delay:180ms;"><svg viewBox="0 0 24 24" class="asta-rating-empty" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg> <svg viewBox="0 0 24 24" class="asta-rating-full" aria-hidden="true"><path d="M12 3.2l2.6 5.28 5.83.85-4.22 4.11 1 5.8L12 16.5l-5.21 2.74 1-5.8-4.22-4.11 5.83-.85L12 3.2z" fill="currentColor" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"></path></svg></span></span></span></div></form>

Usage

The demo above, as code.

"use client";

import { useState } from "react";
import { Rating } from "@/components/astabyte/rating";

const WORDS = ["Tap a star", "Not for me", "Could be better", "It was fine", "Really good", "Loved it"];

const REVIEWS = [
  { name: "Maya R.", stars: 5, text: "Clear lessons, and the exercises actually stick." },
  { name: "Tomás K.", stars: 4.5, text: "Great pace. The last module felt a bit rushed." },
];

export default function RatingDemo() {
  const [stars, setStars] = useState(0);

  return (
    <div className="flex min-h-[420px] w-full items-center justify-center p-6 sm:p-10">
      <div className="flex w-full max-w-sm flex-col gap-8">
        <div className="rounded-xl bg-paper-2 p-5">
          <p className="text-[12px] text-mute">Motion Design Basics · Lesson 12 of 12</p>
          <p className="mt-1 text-[15px] font-medium tracking-[-0.01em] text-ink">How was this course?</p>
          <div className="mt-4 flex items-center justify-between gap-4">
            <Rating label="Rate this course" value={stars} onChange={setStars} />
            <span className="text-[13px] text-ink-2">{WORDS[Math.ceil(stars)]}</span>
          </div>
        </div>

        <div className="flex flex-col gap-4 px-1">
          <div className="flex items-baseline gap-2">
            <span className="text-[22px] font-medium tabular-nums tracking-[-0.03em] text-ink">4.7</span>
            <Rating readOnly size="sm" value={4.5} allowHalf label="Average rating" />
            <span className="text-[12.5px] text-mute">1,284 reviews</span>
          </div>
          {REVIEWS.map((r) => (
            <div key={r.name} className="flex flex-col gap-1">
              <div className="flex items-center gap-2">
                <span className="text-[13px] font-medium text-ink">{r.name}</span>
                <Rating readOnly size="sm" value={r.stars} allowHalf label={`${r.name}'s rating`} />
              </div>
              <p className="text-[13px] leading-relaxed text-ink-2">{r.text}</p>
            </div>
          ))}
          <div className="flex items-center justify-between gap-4">
            <p className="text-[14px] text-ink-2">Half stars</p>
            <Rating label="Rate with half stars" size="sm" allowHalf defaultValue={3.5} />
          </div>
        </div>

        <p className="text-center text-[12.5px] text-mute">Hover to preview, click to rate, click again to clear. Arrow keys work too.</p>
      </div>
    </div>
  );
}