# Star rating

> Five stars that fill as you hover to preview a score and stay filled once you click.

- Category: [Microinteractions](https://library.aimran.es/components/microinteractions)
- Built with: React + Motion
- Live editor: https://library.aimran.es/blocks/hover-star-rating
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## 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.

## Code

### React (Motion)

```tsx
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 assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Star rating” block (Microinteractions) from AIMRAN Library to my project.

## What it is
Five stars that fill as you hover to preview a score and stay filled once you click.

## Chosen settings
- Color: #c58b32

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with React + TypeScript, with framer-motion for the animation and lucide-react for the icons. If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
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>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
