React + MotionMicrointeractions
Star rating
Five stars that fill as you hover to preview a score and stay filled once you click.
Settings
Code
import { useState } from "react";
import { motion } from "framer-motion";
import { Star } from "lucide-react";
const spring = { type: "spring" as const, stiffness: 300, damping: 30 };
interface StarRatingProps {
color?: string;
}
export default function StarRating({ color = "#c58b32" }: StarRatingProps) {
const [rating, setRating] = useState(0);
const [hover, setHover] = useState(0);
return (
<div className="flex gap-1">
{[1, 2, 3, 4, 5].map((i) => (
<motion.button
key={i}
type="button"
aria-label={`${i} de 5 estrellas`}
aria-pressed={rating === i}
onMouseEnter={() => setHover(i)}
onMouseLeave={() => setHover(0)}
onClick={() => setRating(i)}
whileHover={{ scale: 1.18, y: -2 }}
transition={spring}
>
<Star
className="h-8 w-8"
stroke={color}
fill={i <= (hover || rating) ? color : "transparent"}
/>
</motion.button>
))}
</div>
);
}
Prompt for AI
Paste it into ChatGPT, Claude or Cursor and it will adapt the block to your project with these settings, even if you don’t use React.
More in Microinteractions
Where to use it
Stars are still the fastest way to ask for an opinion. The hover preview shows the score before you commit, and each star lifts under the cursor. Use it after a delivery, at the end of a course, under a recipe or in a review form. Few people skip a star rating that reacts this nicely to the mouse.
On phones there is no hover, so the rating is set straight on tap, which works fine. Show the chosen score in words next to it, like “4 out of 5”, and let people change their mind before they send.