Star Rating
FreeNewStars that show a rating to the exact fraction, or collect one with a hover preview and a pop.
Installation
pnpm dlx shadcn@latest add @viewport/star-rating
First time? Add the @viewport registry to components.json. Setup takes one minute.
Usage
The code behind the preview above.
"use client";
import { useState } from "react";
import { StarRating } from "@/components/viewport/star-rating";
const WORDS = ["Tap a star to rate", "Not for me", "Could be better", "It was fine", "Really good", "Loved it"];
export default function StarRatingDemo() {
const [rating, setRating] = useState(4);
return (
<div className="flex w-full max-w-sm flex-col gap-4">
<div className="rounded-2xl border border-border bg-card p-5 text-center">
<p className="text-sm font-medium">How was your order?</p>
<StarRating value={rating} onChange={setRating} size={34} label="Rate your order" className="mt-3" />
<p className="mt-2 text-sm text-muted-foreground" aria-live="polite">
{WORDS[rating]}
</p>
</div>
<div className="flex items-center justify-between rounded-2xl border border-border bg-card px-5 py-4">
<div>
<p className="text-sm font-medium">Overall rating</p>
<p className="mt-0.5 text-xs text-muted-foreground">Based on 1,234 reviews</p>
</div>
<div className="flex items-center gap-2">
<span className="text-xl font-semibold tabular-nums">4.6</span>
<StarRating value={4.6} size={20} />
</div>
</div>
</div>
);
}Props
| Prop | Type | Default |
|---|---|---|
| value* | numberThe rating. When the stars are read-only, a fraction fills part of a star. | — |
| onChange | (value: number) => voidCalled with the new rating. Passing it turns the stars into a control. | — |
| max | numberNumber of stars. | 5 |
| size | numberStar size in pixels. | 18 |
| color | stringFill color. Any CSS color. | "#f5b301" |
| label | stringAccessible name. Defaults to "Rating" for the control and "4.5 out of 5 stars" when read-only. | — |
| name | stringForm field name, for a control inside a native form. | — |
| disabled | boolean | — |
| className | string | — |
Accessibility
- As a control it is a native radio group: arrow keys change the rating and it submits with a form.
- Read-only stars are one image named “4.6 out of 5 stars”.
- No pop or stagger under reduced motion.