//forms
Rating
Star rating input for reviews and feedback, keyboard accessible, plus a read-only display that shows fractional averages. Copy-paste ready.
// title: Product review form with star rating
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Label } from "@/components/ui/label";
import { Rating } from "@/components/ui/rating";
import { Textarea } from "@/components/ui/textarea";
const LABELS = ["", "Poor", "Fair", "Good", "Great", "Excellent"];
export default function Particle() {
const [rating, setRating] = useState(0);
const [sent, setSent] = useState(false);
if (sent) {
return (
<div className="flex w-full max-w-sm flex-col items-center gap-2 rounded-xl border p-6 text-center">
<p className="font-medium text-sm">Thanks for your review</p>
<p className="text-muted-foreground text-sm">
You rated the Aero Wireless Headphones {rating} out of 5.
</p>
<Button
className="mt-2"
onClick={() => {
setRating(0);
setSent(false);
}}
size="sm"
variant="outline"
>
Edit review
</Button>
</div>
);
}
return (
<form
className="flex w-full max-w-sm flex-col gap-4 rounded-xl border p-5"
onSubmit={(e) => {
e.preventDefault();
setSent(true);
}}
>
<div>
<p className="font-medium text-sm">Rate your purchase</p>
<p className="text-muted-foreground text-xs">
Aero Wireless Headphones · delivered Oct 14
</p>
</div>
<div className="flex items-center gap-3">
<Rating
aria-label="Your rating"
onValueChange={setRating}
size="lg"
value={rating}
/>
<span className="text-muted-foreground text-sm">
{LABELS[rating] || "Tap a star"}
</span>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="review-body">Your review (optional)</Label>
<Textarea
id="review-body"
placeholder="Battery life, comfort, sound…"
rows={3}
/>
</div>
<Button disabled={!rating} type="submit">
Post review
</Button>
</form>
);
}
Installation
pnpm dlx shadcn@latest add @cnippet/rating
Usage
import { Rating, RatingDisplay } from "@/components/ui/rating"const [rating, setRating] = useState(0)
<Rating aria-label="Your rating" value={rating} onValueChange={setRating} />
<RatingDisplay value={4.6} />Good to know
Ratingis a radio group: Tab moves into it, the arrow keys change the value, and each star is announced as "3 stars" and so on. Give the group a name witharia-labeloraria-labelledby.- Hovering previews the value; moving the pointer away shows the selected value again.
- A value of
0means nothing is selected yet. Once a star is picked it can't be cleared by clicking, since radios don't uncheck. Add a "Clear" button that sets the value back to0if you need one. - Pass
nameto submit the value with a native form. RatingDisplayis for showing scores, not editing them. It fills stars partially, so3.7shows 70% of the fourth star, and screen readers hear "3.7 out of 5 stars".- The stars are amber in every theme on purpose, because that's what people expect from reviews.
API Reference
Rating
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | — | Controlled rating, from 1 to max; 0 is none |
defaultValue | number | 0 | Initial rating when uncontrolled |
onValueChange | (value: number) => void | — | Called when a star is picked |
max | number | 5 | Number of stars |
size | "sm" | "default" | "lg" | "default" | Star size |
disabled | boolean | false | Disables the input |
readOnly | boolean | false | Shows the value without allowing changes |
name | string | — | Field name for form submission |
RatingDisplay
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | — | Score to show; fractions fill part of a star |
max | number | 5 | Number of stars |
size | "sm" | "default" | "lg" | "default" | Star size |
Examples
Review summary
The average with a fractional RatingDisplay and a breakdown per star.
// title: Average rating with star breakdown bars
import {
Progress,
ProgressIndicator,
ProgressTrack,
} from "@/components/ui/progress";
import { RatingDisplay } from "@/components/ui/rating";
const COUNTS = [
{ count: 812, stars: 5 },
{ count: 264, stars: 4 },
{ count: 71, stars: 3 },
{ count: 22, stars: 2 },
{ count: 31, stars: 1 },
];
const TOTAL = COUNTS.reduce((sum, row) => sum + row.count, 0);
const AVERAGE =
COUNTS.reduce((sum, row) => sum + row.stars * row.count, 0) / TOTAL;
export default function Particle() {
return (
<div className="flex w-full max-w-sm flex-col gap-4 rounded-xl border p-5">
<div className="flex items-end gap-3">
<span className="font-semibold text-4xl tabular-nums leading-none">
{AVERAGE.toFixed(1)}
</span>
<div className="flex flex-col gap-1">
<RatingDisplay value={Math.round(AVERAGE * 10) / 10} />
<span className="text-muted-foreground text-xs">
{TOTAL.toLocaleString("en-US")} reviews
</span>
</div>
</div>
<ul className="flex flex-col gap-1.5">
{COUNTS.map((row) => (
<li
className="grid grid-cols-[2.5rem_1fr_2.5rem] items-center gap-2 text-xs"
key={row.stars}
>
<span className="text-muted-foreground">{row.stars} star</span>
<Progress
aria-label={`${row.stars} star reviews`}
value={(row.count / TOTAL) * 100}
>
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</Progress>
<span className="text-end text-muted-foreground tabular-nums">
{Math.round((row.count / TOTAL) * 100)}%
</span>
</li>
))}
</ul>
</div>
);
}
Sizes and states
// title: Rating sizes, disabled and fractional read-only
import { Rating, RatingDisplay } from "@/components/ui/rating";
export default function Particle() {
return (
<div className="flex flex-col gap-4 text-sm">
<div className="flex items-center gap-3">
<Rating aria-label="Small" defaultValue={3} size="sm" />
<span className="text-muted-foreground">Small</span>
</div>
<div className="flex items-center gap-3">
<Rating aria-label="Default" defaultValue={4} />
<span className="text-muted-foreground">Default</span>
</div>
<div className="flex items-center gap-3">
<Rating aria-label="Large" defaultValue={5} size="lg" />
<span className="text-muted-foreground">Large</span>
</div>
<div className="flex items-center gap-3">
<Rating aria-label="Disabled" defaultValue={2} disabled />
<span className="text-muted-foreground">Disabled</span>
</div>
<div className="flex items-center gap-3">
<RatingDisplay value={3.7} />
<span className="text-muted-foreground">3.7, read-only</span>
</div>
</div>
);
}
Ten-point scale
Use max for longer scales, such as a survey.
// title: Ten-point survey ratings
"use client";
import { useState } from "react";
import { Rating } from "@/components/ui/rating";
const QUESTIONS = [
"How easy was it to set up?",
"How fast did support reply?",
"How likely are you to renew?",
];
export default function Particle() {
const [scores, setScores] = useState<number[]>([4, 0, 0]);
const answered = scores.filter(Boolean).length;
return (
<div className="flex w-full max-w-sm flex-col gap-4 rounded-xl border p-5">
<div className="flex items-baseline justify-between">
<p className="font-medium text-sm">Quarterly check-in</p>
<span className="text-muted-foreground text-xs tabular-nums">
{answered} of {QUESTIONS.length} answered
</span>
</div>
{QUESTIONS.map((question, i) => (
<div className="flex flex-col gap-1.5" key={question}>
<p className="text-sm" id={`q-${i}`}>
{question}
</p>
<Rating
aria-labelledby={`q-${i}`}
max={10}
onValueChange={(value) =>
setScores((prev) => prev.map((s, j) => (j === i ? value : s)))
}
size="sm"
value={scores[i]}
/>
</div>
))}
</div>
);
}