shadcn.io is not affiliated with official shadcn/ui
Aggregate Score Reviews Block for React
A review score with its sum shown: a large rolling figure with stars beside a list of the arithmetic, and a two-way switch between a simple average and one that counts unverified ratings at half.
Installation
Overview
An aggregate score for React with Base UI, for the Pourline, a fictional gooseneck kettle. A compact header introduces a plain split behind a divider. At the start, the score as a large figure whose digits roll to a new value, read-only stars drawn to the nearest half, the number of ratings behind it and a two-way switch, Simple average or Weighted. At the end, the arithmetic as a list: for each star rating the ratings counted times the stars, then the sum, then the division that gives the score and its rounding to one decimal. Switch to Weighted and verified buyers count in full while everyone else counts half, so every line of the sum, the score and the stars are rewritten. Every figure is worked out from the tally of 38 ratings at the top of the file, and stars are drawn in your theme's text colour. On a phone the arithmetic sits under the score and the switch spans the width. It's built from Toggle Group, Sliding Number, Rating and Guides, with CSS transitions only.
FAQ
How do I use my own ratings?
Edit TALLY at the top of the file: for each star rating, how many ratings came from verified buyers and how many from everyone else. The modes are two small objects (SIMPLE and WEIGHTED) that give each kind of rating a weight in halves, so every sum stays a whole number until it is written. The score, the stars drawn, the exact figure to two decimals and every line of the arithmetic are worked out from them when the module loads.
What does Weighted do?
It counts a verified buyer's rating once and any other rating at half, then divides the weighted points by the weighted number of ratings. With the sample data the simple average is 161 points over 38 ratings, 4.24, shown as 4.2; weighted it is 147 points over 34.5 ratings, 4.26, shown as 4.3. The line under the switch says which rule is in force and is read out when you change it.
Why does the list show two decimals but the score one?
The score is shown to one decimal, as most shops do, and the last line of the list says how the figure was rounded. Showing the exact quotient next to the rounded score keeps the block honest: the arithmetic is the same arithmetic that made the number above it.
Why are the stars hidden from screen readers?
They're the catalog Rating, read-only, inside an inert, aria-hidden wrapper: they only draw the score, and the figure beside them says it. Rating draws in your warning colour by default; here it takes text-foreground, and half stars fill from the start in right-to-left layouts too.
Related
- Pro
Rating Breakdown Reviews
Score summariesA review summary for a carry-on suitcase whose star rows filter the reviews: the average, the count, the share that would recommend it and a five-row distribution, beside the four most helpful reviews with a Helpful vote each.
- Pro
Verified Toggle Reviews
Score summariesA review summary with a Verified purchases only switch: the average, the count, the share rated four or more and five distribution rows are worked out again, with a delta line against all reviews.
- Pro
Distribution Bars Reviews
Score summariesA rating distribution for a hiking boot: five ruled rows of bars, counts and shares, a Bars and Table switch, and a readout of mean, median, mode and the share rated four or more.
- Pro
Summary Header Reviews
Score summariesThe header of a product's reviews page: the product name, a Write a review button that opens a real form in a dialog, and a three-cell band with the score, the share that recommend it and five distribution rows.
- Pro
Star Threshold Histogram Reviews
Score summariesA star-rating threshold for a lambswool blanket: a Slider sets the lowest rating that counts, a bracket spans the rows it keeps, and a reading panel gives the share that clears it and the newest review that does.
- Pro
Satisfaction Meter Reviews
Survey scoresA support-survey score whose definition you can change: a segmented control picks which scores count as satisfied, and a rolling percentage, a five-part bar with a bracket and five counted cells follow.