Star Rating
Whole or half stars with hover preview, clear on re-click, and a read-only average.
- Category
- Forms
- Access
- Free with an account
- Source
- 184 lines · one file
- States
- 5 drawn
The decision inside it
Read-only is one sentence
A shown rating is one image named "Rated 4.5 out of 5, 1,284 ratings", not five stars a screen reader has to count.
When to use it
Use Star Rating to collect a score out of five, or show one readOnly with a count. precision={0.5} allows half stars. For a yes/no reaction use Reaction Bar.
Installation
How you get it
Paste this into Claude Code, Cursor, Codex or any coding agent. It installs the file and uses it as it is, instead of writing a new one.
Use Liro's Star Rating for this.
Install it: npx shadcn@latest add @liro/star-rating
(the @liro registry is set up in components.json, with LIRO_KEY in .env.local)
Import it from @/components/ui/star-rating.
Do not rewrite or restyle it. Pass my data through its props
and keep its built-in states.For the full brief with every prop and the mistakes assistants usually make, use Copy prompt above. Free components install through the @liro registry with your CLI token: see the Namespaced tab.
Usage
import { StarRating } from "@/components/ui/star-rating";
export function ReviewScore({ score, setScore }: { score: number; setScore: (n: number) => void }) {
return (
<div className="flex flex-col gap-3">
<StarRating aria-label="Your rating" value={score} onValueChange={setScore} />
<StarRating readOnly value={4.5} precision={0.5} showValue count={128} size="sm" />
</div>
);
}API reference
“Copy prompt” above puts a full brief on your clipboard, install command, dependencies, props, and the mistakes an assistant usually makes with this component.
Props
Read from the component’s own TypeScript interface when the site is built, so this table cannot drift from the file you install.
| Type | Notes | ||
|---|---|---|---|
value | number | No | The rating, 0 for none. Controlled. |
defaultValue | number | No | The starting rating when uncontrolled. Default 0. |
onValueChange | (value: number) => void | No | Called with the new rating, or 0 when cleared. |
max | number | No | How many stars. Default 5. |
precision | 1 | 0.5 | No | Whole stars or half stars. Default 1. |
readOnly | boolean | No | Shows the rating without letting it change. |
size | "sm" | "md" | "lg" | No | sm 16px, md 20px, lg 28px stars. Default md. |
icon | React.ReactNode | No | A different shape, drawn in currentColor and filled. Default a star. |
showValue | boolean | No | Shows the number after the stars, e.g. "4.5". |
count | number | No | How many ratings it is the average of, shown as "(128)". |
disabled | boolean | No | – |
name | string | No | The field's name inside a form. |
id | string | No | – |
className | string | No | – |
Dependencies
npm packages
clsxlucide-reacttailwind-mergeLiro foundations, installed alongside it
liro-themefieldStates
All of them are drawn by the component. None is left for you to design.
- Whole stars
- Half stars
- Hover preview
- Read-only
- Custom icon
Accessibility
- Keyboard reachable throughout, in visual order
- Focus trapped in overlays, Escape closes them
- Focus returns to whatever opened it
- Live regions announce what changed, not that something did
- Honours prefers-reduced-motion
Responsive behavior
- Sizes to its container, not the viewport
- Usable from 320px up
- Pointer-based interaction, so trackpad and touch both work
- No hover-only affordances