3 out of 5
Installation
npx logic2b@next add ratingUsage
import { Rating } from "@/components/ui/rating"
const [value, setValue] = React.useState(0)
<Rating value={value} onValueChange={setValue} aria-label="Rate this" />
Clicking the current value again clears the rating to 0.
Examples
Read-only
For displaying scores: the group becomes role="img" labelled
“label: n out of max”, and any icon works.
Accessibility
- Follows the radio-group pattern: each step is a
role="radio"button witharia-checkedand ann out of maxlabel. - Arrow keys move the value (selection follows focus); Home / End jump to 1 / max.
- Read-only ratings are announced as a single image with the score in the label, not as a disabled control.
Live playground
Edit the component props and inspect the exact JSX before copying it. The preview loads only when this section enters the viewport.
<Rating
defaultValue={3}
max={5}
aria-label="Product rating"
/>API reference
Generated from the public TypeScript exports in src/ui/rating.tsx.
Rating
ComponentRating — a star (or custom icon) rating following the radio-group pattern: each star is a radio, arrows move the value, hover previews it. `readOnly` renders a static, labelled image instead.
Props: Omit<React.ComponentProps<"div">, "defaultValue" | "onChange"> & { value?: number defaultValue?: number onValueChange?: (value: number) => void max?: number readOnly?: boolean disabled?: boolean icon?: React.ReactNode }| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
value | number | No | — | — |
defaultValue | number | No | — | — |
onValueChange | (value: number) => void | No | — | — |
max | number | No | 5 | — |
readOnly | boolean | No | false | — |
disabled | boolean | No | false | — |
icon | React.ReactNode | No | — | — |
aria-label | string | No | "Rating" | — |
Accessibility contract
Machine-readable behavior and consumer responsibilities for rating.
- Support
- Authored behavior
- Pattern
- radio group
| Keys | Behavior |
|---|---|
| ArrowUp ArrowDown ArrowLeft ArrowRight | Move within the composite control. |
| Home End | Move to the minimum or maximum rating. |
| Space | Select the focused rating. |
Built-in semantics
- Interactive stars expose radiogroup/radio and aria-checked; read-only mode is a labelled image.
Consumer responsibilities
- Provide a contextual label and visible text for the meaning of the scale.