Rating
A row of stars to rate something from 1 to 5, or to any maximum. As an input it is a radio group: one press or the arrow keys set it. Read-only, it is a picture of a score and can show a fraction such as 3.6. For a choice between named options use the radio group.
Install
npx shadcn@latest add @thrum-pro/ratingThis is a Pro component. The command works once your project has a licence key: set up Pro or see pricing.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | null | – | The rating, when the parent holds it. null is no rating yet. Use it with onValueChange. A read-only rating may be a fraction. |
defaultValue | number | null | Default null | The rating at first, when the control holds it itself. |
onValueChange | (value: number) => void | – | Called with the new rating, a whole number from 1 to max, when another mark is picked. |
max | number | Default 5 | How many marks there are. |
readOnly | boolean | Default false | Shows the rating as a picture that cannot be changed or focused. A fraction fills part of a mark: 3.6 fills three marks and 60% of the fourth. |
disabled | boolean | Default false | Dimmed, skipped by Tab, and cannot be changed. |
size | "sm" | "md" | "lg" | Default "md" | Marks are 16, 20 or 28 pixels across. Each is padded to a target of at least 24 pixels. |
path | string | – | The outline to draw instead of a star: an SVG path on a 24 by 24 grid. It is drawn as a line when empty and filled when picked. |
labels | readonly string[] | – | What a screen reader calls each mark, in order. The default is "1 star", "2 stars" and so on. Set it with a custom path, or to name the steps ("Poor", "Good"). |
name | string | – | The field name when the rating sits in a form. The picked number is submitted under it. |
sound | boolean | Default true | Play a soft click when the rating changes, if the visitor has sound on. |
className | string | – | Extra classes for the row, merged last. |
...props | ComponentProps<"div"> | – | Every other attribute goes to the row: aria-label, aria-labelledby, id. |
Motion and sound
Motion
- An interactive mark scales gently to 110% on hover and shrinks to 90% on press over 150 ms.
- Marks fill and empty at once, on a pick and while a mouse pointer moves along the row to preview a rating.
- Reduced motion: marks do not scale.
Sound
- A soft click with a short tone when the rating changes. Picking the same rating again is silent, and so is the hover preview.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- As an input it follows the WAI-ARIA radio group pattern with native radio buttons: the row is a radiogroup and each mark is a radio named for its number.
- Name the row with aria-labelledby pointing at a visible label, as the example does, or with an aria-label.
- Read-only, it is one image whose name states the score, such as "Average rating: 3.6 out of 5". With no rating it says "No data".
- Picked marks are filled and the rest are outlines, so the rating never rests on colour.
- The outlines keep 3 to 1 contrast against the page in both themes.
- A rating cannot be cleared with the keyboard once set: a radio group always keeps one option. Offer a separate button when clearing matters.
- Past the width of its container the row wraps. Five marks need 120 pixels.
| Keys | Action |
|---|---|
| Tab | Focus the picked mark, or the first one when there is no rating. The row is one tab stop. |
| → / ↓ | Raise the rating by one. From the last mark it wraps to the first. |
| ← / ↑ | Lower the rating by one. From the first mark it wraps to the last. |
| Space | Pick the focused mark, when there is no rating yet. |
Source
The source comes with Pro
TypeScript source · Copy-paste ready · MIT-compatible for client projects · Includes all dependencies
Files and dependencies
- Adds
components/ui/rating.tsx- Packages
- None
- Brings along
- Sound engine
Changelog
- 1.0.0 · 2026-12-03First release.