Skip to content

Rating

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

NameTypeRequiredDefaultDescription
valuenumberNo

Controlled rating value (0 = no rating): a whole number of stars, or with step={0.5} a multiple of 0.5. A value between two steps (2.7) checks no radio, is drawn as the step below it (2.5) and holds the tab stop there; the keys move it to the next or previous step.

defaultValuenumberNo0

Initial rating for uncontrolled usage (also the value a form reset restores).

onValueChange(value: number) => voidNo

Called with the new rating when it changes (not when the current star is chosen again).

maxnumberNo5

Maximum number of stars.

step0.5 | 1No1

The grain of the rating: 1 (whole stars) or 0.5 (half stars). With 0.5, each star stays one pointer target of at least 24×24px (a larger size keeps a larger one): the half is chosen by the pointer's position over it (the half at the inline start, which mirrors under dir="rtl"), and two transparent radios per star, the half value then the full value, serve the keyboard and screen readers. The keys then move by half a star. A value other than 0.5 (from untyped code) counts as 1. RatingDisplay has no step: it draws any fraction.

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: marigold (default), brand or neutral; unfilled stars keep a 3:1 outline. Fluent's default is neutral. A value outside the three (from untyped code) renders as marigold; the root carries data-color with the resolved value.

iconFilledSlot<'span'>No

The glyph of a filled star. iconFilled and iconOutline are the glyphs of a filled and an unfilled star, as a pair; decorative. Fluent's RatingDisplay takes one icon; here a filled and an unfilled star must differ by shape, not by color alone.

- Each glyph is sized by size (an SVG child fills the star) and takes the star's color (paint it with currentColor). A half-filled star (step={0.5}) clips the filled glyph over the unfilled one from the inline start.
- null, undefined and a value that renders nothing draw the default star. A value that renders nothing, and a pair with only one of the two set, log a development warning.
- A <button> or Button element (or a slot object whose as is one) is not nested in the star: its children become the glyph (the default star when they render nothing), its props are dropped, and a development warning says so (in place of the warning for a value that renders nothing).
- A Rating.Item that sets its own pair draws that one instead.

iconOutlineSlot<'span'>No

The glyph of an unfilled star, as a pair with iconFilled (its rules apply); decorative. Fluent's RatingDisplay takes one icon; here a filled and an unfilled star must differ by shape, not by color alone.

disabledbooleanNofalse

Whether the rating is disabled and non-interactive.

namestringNo

Form field name. With a name, the chosen rating is submitted with the form (nothing while no star is chosen).

requiredbooleanNo

A star must be chosen before the form can be submitted (native validation).

formstringNo

Id of the form the rating belongs to, when it is rendered outside that form.

labelsRatingLabelsNo

Names of the stars ("1 star", "2 stars", …, and "2.5 stars" for a half value), for localization. The group's own name is aria-label (default "Rating"). Unset members keep their English defaults.

childrenReact.ReactNodeNo

Custom stars: one Rating.Item per value from 1 to max, in place of the generated stars (when the children render content). max still sets the key range and the star names.

refReact.Ref<HTMLDivElement>No

Ref to the role="radiogroup" element.

Any other prop is passed to the element it renders (HTMLDivElement).

Parts

  • Rating.Item

Stable class names

PartClass
rootwave-rating
starwave-rating__star
iconwave-rating__icon