Rating
A row of stars, or any icon, to give a score with a press or the keyboard, or to show one read-only, with half and part-filled icons.JSX
<Rating aria-label="Playground rating" defaultValue={3} />Installation
Package
CLI (copy source)
pnpm
pnpm add @advui/corenpm
npm install @advui/coreUsage
import { Field, Rating } from '@advui/core'
<Field label="How was your stay?">
<Rating value={stars} onValueChange={setStars} allowHalf />
</Field>
<Rating aria-label="Average rating" readOnly value={4.3} size="sm" />Examples
Basic
A controlled rating with a word for each score beside it.Preview
Code
Half ratings and sizes
With `allowHalf`, each half of an icon is its own value. Small, medium and large.Preview
Code
Read-only score
A product’s average beside its review count; 4.3 fills part of the fifth star.Preview
Code
In a Field
The field names the rating, shows the error until one is picked, and submits it as `stay`.Preview
Code
Your rating is shown with your review.Pick a rating.
Other icons and counts
Hearts in the primary color, and a ten-point scale.Preview
Code
API reference
Rating
A row of icons in a View; style props go to the row.| Prop | Type | Default | Description |
|---|---|---|---|
value | number | — | The rating, from 0 (none) to `count`, when you control it. |
defaultValue | number | 0 | Starting `value` when uncontrolled. |
onValueChange | (value: number) => void | — | Called when the user picks a rating with a press, a key or a swipe. |
count | number | 5 | How many icons, and the highest rating. |
allowHalf | boolean | false | Lets the user pick half ratings (3.5): the start half of an icon picks the half, the end half the whole. Keys step by 0.5. |
readOnly | boolean | false | Shows the rating without letting anyone change it, as an image named by its value. Any fraction shows as a part-filled icon: 4.3 fills 30% of the fifth. |
size | 'sm' | 'md' | 'lg' | 'md' | Icons of 16, 24 or 32px. |
icon | IconComponent | StarIcon | The icon to repeat: any icon from `@advui/icons` (`HeartIcon`), one made with `createIcon`, or a Lucide icon. It must take `fill`. |
color | ColorTokens | 'warning', outlined in 'warningSoftForeground' | Fill and outline of the chosen icons. The default amber has a darker outline so the icons stand out on light backgrounds. |
emptyColor | ColorTokens | 'mutedForeground' | Outline of the icons that are not chosen. `error` while invalid. |
getValueText | (value: number) => string | "3 of 5" | The value screen readers read, and a read-only rating’s name. Translate it here. |
disabled | boolean | false | Dims the icons and ignores presses and keys. Also on inside a disabled Field or Form. |
invalid | boolean | false | Error outline on the empty icons and `aria-invalid`. On inside a Field with an `error`. |
name | string | — | Field name for a native form: the rating submits its value under it.Web only. |
aria-label | string | — | Names the rating. Inside a Field, the field’s label names it instead. A read-only rating reads it before the value. |
Accessibility
- An interactive rating is one
role="slider"from 0 tocount, as in the WAI-ARIA rating slider example, witharia-valuetextsuch as "3 of 5" (getValueText). The icons inside are hidden from assistive technology. - Named by
aria-labelor, inside a Field, by the field’s label (aria-labelledby); the field’s help and error text describe it. A slider has no required state, so say what is required in the help or error text. readOnlymakes itrole="img", named by its label and value ("Average rating: 4.3 of 5"), and takes it out of the Tab order.- The chosen icons are filled, not only colored, so the score does not rely on color alone. The default empty outline (
mutedForeground) has at least 3:1 contrast on the background in every preset. - On iOS and Android each icon reaches a 44pt touch target with
hitSlop.
| Key | Action |
|---|---|
| ArrowRight / ArrowUp | One step higher (half a step with `allowHalf`). |
| ArrowLeft / ArrowDown | One step lower. Left and right swap in RTL. |
| Home | Clears the rating (0). |
| End | Picks the top score. |
Platform notes
Web
A
div with role="slider" and tabindex="0". Hovering an icon previews its value until the pointer leaves. With name, a hidden input submits the value.iOS
An adjustable element: VoiceOver users swipe up or down to change it, and hear the value text. Tap an icon (or its half) to pick it.
Android
An adjustable element: TalkBack users change it with its swipe adjustments, and hear the value text. Tap an icon (or its half) to pick it.