Rating
A star rating input (role="radiogroup" of role="radio" stars).
- One tab stop (the radio of the chosen value, else the first). Keys choose relative to the current rating and move focus to the chosen radio, as in 0.4: Right/Up one step more (a star, or half a star), Left/Down one fewer (Left/Right mirrored under dir="rtl"), Home the smallest value (step) and End max. From an empty rating, Right/Up/Home choose step. Keys at the ends (and Left/Down while empty) emit nothing.
- The keyboard cannot clear a rating: Left/Down stop at step (APG radio group; 0.4 went down to 0). Only a controlled value={0} (or a form reset while defaultValue is 0) clears it.
- step={0.5} offers half stars. Each star stays one pointer target, which chooses by the pointer's position (its half from the inline start, else its full value) and then focuses the radio it chose; it holds two transparent radios ("2.5 stars", "3 stars") for the keyboard and screen readers, and draws the focus ring of either. A controlled value between two steps checks no radio and is drawn as the step below it; the keys move it to the next or previous step.
- onValueChange fires only when the rating changes, so choosing the current star again does not call it.
- Hovering previews a value (half stars preview by the pointer's position); the preview never outlives the hover or a disabled state.
- color colors the filled stars (marigold, the default, brand or neutral; the root carries data-color); iconFilled and iconOutline replace the star glyphs, as a pair.
- Inside a Field it is named by the Field label (instead of the default "Rating") and described by its hint and error. The stars are named "1 star", "2 stars", … (and a half value "2.5 stars"); labels localizes these names.
- With name (or required) it takes part in native forms; a form reset restores defaultValue.
- The stars are generated from max. Children that render content replace them: one Rating.Item per value from 1 to max, chosen, previewed, roved and named as the generated stars are (max still sets the key range and the star names).
Sub-component: Rating.Item (the same component as RatingDisplay.Item). React Server Components import its flat name RatingItem (dotted access needs a client file).
Examples
Default
export const Default: Story = {
args: {
defaultValue: 3,
},
};Large
export const Large: Story = {
args: {
defaultValue: 4,
size: 'large',
},
};Small
export const Small: Story = {
args: {
defaultValue: 2,
size: 'extra-small',
},
};Ten Stars
export const TenStars: Story = {
args: {
max: 10,
defaultValue: 7,
},
};Disabled
export const Disabled: Story = {
args: {
value: 3,
disabled: true,
},
};Labelled
export const Labelled: Story = {
args: {
'aria-label': 'Product quality',
defaultValue: 4,
},
};Read Only
export const ReadOnly: StoryObj<typeof RatingDisplay> = {
render: () => (
<div className="flex flex-col gap-4">
<RatingDisplay value={5} size="small" />
<RatingDisplay value={3} size="medium" />
<RatingDisplay value={4.6} size="medium" />
<RatingDisplay value={1} size="large" />
</div>
),
};Display With Value And Count
export const DisplayWithValueAndCount: StoryObj<typeof RatingDisplay> = {
render: () => (
<div className="flex flex-col gap-4">
<RatingDisplay value={4.5} showValue locale="en-US" size="small" />
<RatingDisplay value={4.5} count={1160} locale="en-US" />
<RatingDisplay value={3.7} count={1} locale="en-US" size="large" />
</div>
),
};Display Compact
export const DisplayCompact: StoryObj<typeof RatingDisplay> = {
render: () => (
<div className="flex flex-col gap-4">
<RatingDisplay value={4.2} compact locale="en-US" />
<RatingDisplay value={4.2} compact count={87} locale="en-US" />
</div>
),
};Display Localized
export const DisplayLocalized: StoryObj<typeof RatingDisplay> = {
render: () => (
<div lang="nb">
<RatingDisplay
value={4.5}
count={1160}
locale="nb-NO"
labels={{
rating: (_value, max, formattedValue) => `Vurdering: ${formattedValue} av ${max}`,
count: (count, formattedCount) =>
count === 1 ? '1 vurdering' : `${formattedCount} vurderinger`,
}}
/>
</div>
),
};Half Stars
export const HalfStars: Story = { args: { 'aria-label': 'Service', step: 0.5, defaultValue: 3.5 } };Colors
export const Colors: Story = {
render: (args) => (
<div className="grid gap-3">
{(['marigold', 'brand', 'neutral'] as const).map((color) => (
<div key={color} className="flex items-center gap-4">
<Rating {...args} color={color} defaultValue={3} aria-label={`${color} rating`} />
<RatingDisplay color={color} value={3.6} showValue />
</div>
))}
</div>
),
};Custom Icons
export const CustomIcons: Story = {
render: (args) => (
<div className="flex items-center gap-4">
<Rating
{...args}
aria-label="Love"
defaultValue={2}
iconFilled={<Heart filled />}
iconOutline={<Heart filled={false} />}
/>
<RatingDisplay
value={3.6}
iconFilled={<Heart filled />}
iconOutline={<Heart filled={false} />}
/>
</div>
),
};Custom Items
export const CustomItems: Story = {
render: (args) => (
<Rating {...args} aria-label="Mood" max={3} defaultValue={2}>
<Rating.Item value={1} iconFilled={<Heart filled />} iconOutline={<Heart filled={false} />} />
<Rating.Item value={2} />
<Rating.Item value={3} />
</Rating>
),
};Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
value | number | No | Controlled rating value ( | |
defaultValue | number | No | 0 | Initial rating for uncontrolled usage (also the value a form reset restores). |
onValueChange | (value: number) => void | No | Called with the new rating when it changes (not when the current star is chosen again). | |
max | number | No | 5 | Maximum number of stars. |
step | 0.5 | 1 | No | 1 | The grain of the rating: |
size | 'extra-small' | 'small' | 'medium' | 'large' | 'extra-large' | No | 'medium' | Size of the star icons. Every size keeps a target of at least 24×24px. |
color | 'neutral' | 'brand' | 'marigold' | No | 'marigold' | Color of the filled stars: |
iconFilled | Slot<'span'> | No | The glyph of a filled star. - Each glyph is sized by | |
iconOutline | Slot<'span'> | No | The glyph of an unfilled star, as a pair with | |
disabled | boolean | No | false | Whether the rating is disabled and non-interactive. |
name | string | No | Form field name. With a name, the chosen rating is submitted with the form (nothing while no star is chosen). | |
required | boolean | No | A star must be chosen before the form can be submitted (native validation). | |
form | string | No | Id of the form the rating belongs to, when it is rendered outside that form. | |
labels | RatingLabels | No | Names of the stars ("1 star", "2 stars", …, and "2.5 stars" for a half value), for localization. The group's own name is | |
children | React.ReactNode | No | Custom stars: one | |
ref | React.Ref<HTMLDivElement> | No | Ref to the |
Any other prop is passed to the element it renders (HTMLDivElement).
Parts
Rating.Item
Stable class names
| Part | Class |
|---|---|
root | wave-rating |
star | wave-rating__star |
icon | wave-rating__icon |