# Star Rating

> Stars that fill with a springy preview on hover and send out a burst of sparks when chosen, with the word for each rating rolling in below.

Category: Inputs · Free · Updated 2026-11-12 · Live demo: https://www.motionkitui.com/components/star-rating

## When to use it

Product and app reviews, post-purchase surveys, support feedback, course and recipe ratings.

## How it works

Under each star is a visually hidden radio button in one fieldset, so keyboard and screen reader behaviour comes from the browser. The star's fill is a second SVG path whose opacity and scale are springs, previewing on hover and settling on the chosen value. Choosing a star replays a burst of eight small sparks that fly outward and fade, and the word for the rating swaps in with AnimatePresence.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/star-rating.json
```

Or copy `components/motionkit/star-rating.tsx` below. Requires react, motion and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `max` | `number` | `5` | Number of stars. |
| `defaultValue` | `number` | `0` | Starting rating when the component manages it; 0 for none. |
| `value` | `number` |  | The rating, if you control it. Pair with onChange. |
| `onChange` | `(value: number) => void` |  | Called with the chosen rating. |
| `label` | `string` | `"Rate your experience"` | The question, shown above the stars and read with them. |
| `labels` | `string[]` | `["Terrible", …, "Amazing"]` | A word for each rating, shown under the stars; an empty array shows none. |
| `color` | `string` | `"#F2C46D"` | Colour of a filled star. |
| `size` | `number` | `36` | Star size in pixels. |

## Accessibility

- **Keyboard: Pass.** Built on native radio buttons, so Tab reaches the group and the arrow keys change the rating, with a visible focus ring on the star.
- **Screen readers: Pass.** A fieldset with your question as its legend; each star is announced with its number and word, e.g. "4 stars, Good". The sparks are decoration.
- **Reduced motion: Pass.** With reduced motion on, stars fill without scaling and no sparks are shown.
- **Colour contrast: Pass.** Filled stars measure 11.8:1 against the page and empty ones keep a visible outline. The rating words measure 7.7:1.

## Source

```tsx
"use client";

import { useId, useState } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";

type StarRatingProps = {
  /** Number of stars. */
  max?: number;
  /** Starting rating (uncontrolled); 0 for none. */
  defaultValue?: number;
  /** The rating, if you control it. Pair with onChange. */
  value?: number;
  onChange?: (value: number) => void;
  /** The question being answered, read out with the stars. */
  label?: string;
  /** A word for each rating, shown under the stars; leave empty for none. */
  labels?: string[];
  /** Colour of a filled star. */
  color?: string;
  /** Star size in pixels. */
  size?: number;
  className?: string;
};

const STAR = "M12 2.6l2.9 6 6.5.9-4.7 4.6 1.1 6.5L12 17.5l-5.8 3.1 1.1-6.5L2.6 9.5l6.5-.9z";

/**
 * A star rating built on real radio buttons, so arrow keys and screen readers work as they do for any rating.
 * Hovering previews the stars with a springy fill, choosing one sends out a small burst of sparks, and the word
 * for the rating rolls in underneath.
 */
export function StarRating({
  max = 5,
  defaultValue = 0,
  value: controlled,
  onChange,
  label = "Rate your experience",
  labels = ["Terrible", "Not great", "Okay", "Good", "Amazing"],
  color = "#F2C46D",
  size = 36,
  className = "",
}: StarRatingProps) {
  const reduceMotion = useReducedMotion();
  const name = useId();
  const [own, setOwn] = useState(defaultValue);
  const value = controlled ?? own;
  const [hover, setHover] = useState(0);
  // bumped on each choice so the burst replays even when the same star is chosen again
  const [burst, setBurst] = useState(0);
  const shown = hover || value;
  const word = shown ? labels[shown - 1] : "";

  function choose(next: number) {
    if (controlled === undefined) setOwn(next);
    onChange?.(next);
    setBurst((count) => count + 1);
  }

  return (
    <fieldset className={`inline-flex flex-col items-center gap-2 ${className}`}>
      <legend className="mb-3 w-full text-center text-sm font-medium text-[#F6F1E8]">{label}</legend>
      <div className="flex gap-1" onPointerLeave={() => setHover(0)}>
        {Array.from({ length: max }, (_, index) => {
          const star = index + 1;
          const filled = star <= shown;
          return (
            <label key={star} className="relative cursor-pointer" onPointerEnter={() => setHover(star)}>
              <input
                type="radio"
                name={name}
                value={star}
                checked={value === star}
                onChange={() => choose(star)}
                aria-label={`${star} ${star === 1 ? "star" : "stars"}${labels[index] ? `, ${labels[index]}` : ""}`}
                className="peer sr-only"
              />
              <motion.svg
                aria-hidden
                viewBox="0 0 24 24"
                width={size}
                height={size}
                animate={reduceMotion ? undefined : { scale: filled ? (hover === star ? 1.18 : 1.06) : 1, rotate: filled ? (index % 2 ? 6 : -6) : 0 }}
                transition={{ type: "spring", stiffness: 500, damping: 18, delay: reduceMotion || hover ? 0 : index * 0.03 }}
                className="block rounded-md peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-[#B9C7FF]"
              >
                <path d={STAR} fill="rgb(255 255 255 / 0.1)" stroke="rgb(255 255 255 / 0.25)" strokeWidth="1" strokeLinejoin="round" />
                <motion.path
                  d={STAR}
                  fill={color}
                  initial={false}
                  animate={{ opacity: filled ? 1 : 0, scale: filled ? 1 : 0.4 }}
                  transition={reduceMotion ? { duration: 0 } : { type: "spring", stiffness: 600, damping: 26 }}
                  style={{ transformOrigin: "center", filter: `drop-shadow(0 0 6px ${color}66)` }}
                />
              </motion.svg>
              {/* sparks fly out of the chosen star */}
              {!reduceMotion && value === star && burst > 0 && (
                <span key={burst} aria-hidden className="pointer-events-none absolute inset-0">
                  {Array.from({ length: 8 }, (_, spark) => {
                    const angle = (spark / 8) * Math.PI * 2;
                    return (
                      <motion.span
                        key={spark}
                        className="absolute left-1/2 top-1/2 size-1.5 rounded-full"
                        style={{ background: color, marginLeft: -3, marginTop: -3 }}
                        initial={{ x: 0, y: 0, opacity: 1, scale: 1 }}
                        animate={{ x: Math.cos(angle) * size * 0.9, y: Math.sin(angle) * size * 0.9, opacity: 0, scale: 0.3 }}
                        transition={{ duration: 0.55, ease: [0.16, 1, 0.3, 1] }}
                      />
                    );
                  })}
                </span>
              )}
            </label>
          );
        })}
      </div>
      {/* each star's label already includes its word, so this is for sighted users only */}
      <div aria-hidden className="relative h-6 w-full overflow-hidden text-center text-sm text-[#94A9A7]">
        <AnimatePresence mode="wait" initial={false}>
          <motion.span
            key={word}
            className="absolute inset-x-0"
            initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 12 }}
            animate={{ opacity: 1, y: 0 }}
            exit={reduceMotion ? { opacity: 0 } : { opacity: 0, y: -12 }}
            transition={{ duration: 0.18 }}
          >
            {word}
          </motion.span>
        </AnimatePresence>
      </div>
    </fieldset>
  );
}
```
