A summary of reviews: the average score in large type, stars from Rating in read-only mode and one bar per score from the highest to the lowest. Pass raw vote counts and the component works out the average and the total. It is a Server Component with no JavaScript: the bars are full by default and grow once on first paint with a CSS transition.
Browser Support
Grows the bars from zero on first paint.
Preview
The bars grow once when the block first renders.
Installation
The command also installs Rating, which draws the stars.
Usage
counts takes an array ordered from the lowest score, or an object keyed by score.
import { RatingBreakdown } from "@/components/ui/rating-breakdown"
export default function Example() {
return <RatingBreakdown counts={{ 5: 812, 4: 264, 3: 91, 2: 38, 1: 54 }} />
}Examples
Live counts
Feed it new counts from a client component and the average, the stars and the bars update. animate={false} turns the growth animation off.
const [counts, setCounts] = useState([1, 2, 4, 9, 14])
<RatingBreakdown counts={counts} animate={false} />
<Rating
value={null}
onValueChange={(v) =>
v && setCounts((prev) => prev.map((c, i) => (i === v - 1 ? c + 1 : c)))
}
/>Ten-point scale
With an array, max is taken from its length. formatAverage changes how the big number looks.
<RatingBreakdown
counts={[0, 1, 1, 2, 4, 7, 12, 18, 9, 5]}
shape="sparkle"
formatAverage={(avg) => `${avg.toFixed(1)}/10`}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| counts | number[] | Record<number, number> | - | Votes per score: an array from the lowest score or an object |
| max | number | 5 | Highest score. Taken from the array length, otherwise 5 |
| shape | "star" | "sparkle" | "star" | Star shape, passed to Rating |
| formatAverage | (average: number) => string | toFixed(1) | Formats the average number |
| formatCount | (count: number) => string | toLocaleString | Formats the count at the end of each row |
| animate | boolean | true | Grow the bars from zero once on first render |
| labels | Partial<RatingBreakdownLabels> | - | Texts: total under the average, row description, stars summary |
| className | string | - | Additional CSS classes |