Star Rating
UI Coding
Easy
20 min
forms
accessibility
state
events
Build a reusable StarRating input, like the one under a recipe or a product review.
Requirements
- Renders
maxstars (default 5). The value starts atdefaultValue(default 0, meaning no rating). - Click a star to set the rating. Every star up to and including the chosen one is filled.
- Hover preview: while the pointer is over a star, fill up to that star. When the pointer leaves the widget, go back to showing the actual value.
- Show the result as text below the stars: "3 out of 5" or "Not rated".
- A Clear button resets the value to 0. Hide or disable it when there's no rating.
- Accessibility: it has to work as a form control.
- Each star is a radio input whose accessible name is "1 star", "2 stars", and so on.
- The group has a visible label ("Rate this recipe") via
fieldset/legend. - Arrow keys change the rating (radios give you this) and the focused star shows a visible focus ring.
- Call an optional
onChange(value)whenever the value changes.
Follow-up
- How would you support half stars?
- How would you render a read-only rating (for example 4.3 average) accessibly?
- Why not build it from
<span>s with click handlers?
Spotted a mistake or a rights concern? Report an issue