Components / Inputs
Star Rating
A star rating whose stars pop and fill as you choose.
Tap a star to rate
Installation
Requires React 18+ and Tailwind CSS.
- 1
Install dependencies
$ npm install motion - 2
Add the component
Copy the code from the Code tab above into
components/ui/star-rating.tsx. - 3
Use it
example.tsx"use client"; import { useState } from "react"; import { StarRating } from "@/components/ui/star-rating"; const labels = ["", "Terrible", "Bad", "Okay", "Good", "Amazing!"]; export default function StarRatingDemo() { const [rating, setRating] = useState(0); return ( <div className="flex flex-col items-center gap-3"> <StarRating value={rating} onChange={setRating} /> <p className="h-5 text-sm font-medium text-zinc-600 dark:text-zinc-400">{labels[rating] || "Tap a star to rate"}</p> </div> ); }
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | number | — | Controlled value. Omit to let it manage itself. |
| defaultValue | number | 0 | Starting value when uncontrolled. |
| max | number | 5 | Number of stars. |
| onChange | (value: number) => void | — | Called on selection. |
| readOnly | boolean | false | Display only. |