GooseUI

Components

Rating Breakdown

New

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

@starting-style

Grows the bars from zero on first paint.

Your browser:

Preview

The bars grow once when the block first renders.

4.41,259 ratings
  • Score 5: 812 (64%)
  • Score 4: 264 (21%)
  • Score 3: 91 (7%)
  • Score 2: 38 (3%)
  • Score 1: 54 (4%)

Installation

npx shadcn@latest add @gooseui/rating-breakdown
pnpm dlx shadcn@latest add @gooseui/rating-breakdown
yarn dlx shadcn@latest add @gooseui/rating-breakdown
bunx --bun shadcn@latest add @gooseui/rating-breakdown

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.

4.130 ratings
  • Score 5: 14 (47%)
  • Score 4: 9 (30%)
  • Score 3: 4 (13%)
  • Score 2: 2 (7%)
  • Score 1: 1 (3%)
Add your rating
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.

7.4/1059 votes
  • Score 10: 5 (8%)
  • Score 9: 9 (15%)
  • Score 8: 18 (31%)
  • Score 7: 12 (20%)
  • Score 6: 7 (12%)
  • Score 5: 4 (7%)
  • Score 4: 2 (3%)
  • Score 3: 1 (2%)
  • Score 2: 1 (2%)
  • Score 1: 0 (0%)
<RatingBreakdown
  counts={[0, 1, 1, 2, 4, 7, 12, 18, 9, 5]}
  shape="sparkle"
  formatAverage={(avg) => `${avg.toFixed(1)}/10`}
/>

Props

PropTypeDefaultDescription
countsnumber[] | Record<number, number>-Votes per score: an array from the lowest score or an object
maxnumber5Highest score. Taken from the array length, otherwise 5
shape"star" | "sparkle""star"Star shape, passed to Rating
formatAverage(average: number) => stringtoFixed(1)Formats the average number
formatCount(count: number) => stringtoLocaleStringFormats the count at the end of each row
animatebooleantrueGrow the bars from zero once on first render
labelsPartial<RatingBreakdownLabels>-Texts: total under the average, row description, stars summary
classNamestring-Additional CSS classes