A range slider with give: the value bubble leans as you drag, and pulling past the ends stretches the track like rubber.
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.
Customize
Usage
import { StarRating } from "@/components/motionkit/star-rating";
<StarRating
label="How was your stay?"
onChange={(rating) => console.log(rating)}
/>Motion
The exact curves this component runs, plotted with Motion's own maths.
Star fills
Hover or choose
Sparks fly out
Choose a rating
Product and app reviews, post-purchase surveys, support feedback, course and recipe ratings.
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.
Works in the App Router as is. Requires Tailwind CSS v4.
One command adds the file and installs its dependencies. Needs a components.json (run npx shadcn@latest init once if you don't have one).
npx shadcn@latest add https://www.motionkitui.com/r/star-rating.jsonnpm install motionSave as components/motionkit/star-rating.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>
);
}
| 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. |
How we audit: the WCAG 2.2 animation checklist and respecting reduced motion in React.
Yes. The file starts with "use client", so you can import it straight into a Server Component page or layout. It works in Vite as is, and in Astro as a React island.
Only the motion package, beside React and Tailwind CSS v4. There is no MotionKit package and no Tailwind config to add.
Yes. Run npx shadcn@latest add https://www.motionkitui.com/r/star-rating.json in any project with a components.json. It saves the file to components/motionkit and installs its dependencies.
With reduced motion on, stars fill without scaling and no sparks are shown.
A range slider with give: the value bubble leans as you drag, and pulling past the ends stretches the track like rubber.
A password field with a four-bar strength meter and requirements that tick themselves off as you type.
A drop area whose dashed border flows when a file is dragged over, with a progress bar per file that ends in a drawn tick or a clear error.