Skip to content

React Star Rating

An animated React star rating with hover preview and half steps, built as a radio group so arrow keys and screen readers just work.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/rating.json

How was your stay?

Tap a star to rate.

Basic

Hover to preview, click to rate, click the same star again to clear (or press Backspace).

Rate your stay

Files to copy

5 folders · 9 files · 4 packages
From theme/src/
  • components/rating/
  • hooks/{use-controllable.ts, use-direction.ts, use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, form.ts, warn-once.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/rating.jsonRegistry item: rating. The styles need the Tailwind v4 glass UI setup.
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

Half stars with words

allowHalf gives every star a left and a right half (on touch screens, tap a star again for its half). valueLabels and showValue echo the hovered or chosen value.

Overall

Read-only and sizes

Read-only ratings are one image with a full label (“Rated 4.5 out of 5”), not a set of controls.

Custom icon and tone

Any SVG that uses currentColor. Labels are yours to translate.

Props

PropTypeDefaultDescription
onBlurFocusEventHandler<HTMLElement>NoneCalled when focus leaves the control (not when it moves between the control's own parts).
namestringNoneSubmits the value with a form.
requiredbooleanNoneMarks the field as required for form validation.
disabledbooleanfalseDisables the rating so it cannot be changed.
aria-invalidboolean | "true" | "false"NoneMarks the field as invalid (also set automatically when error is shown).
aria-describedbystringNoneIds of elements that describe the field, merged with the control's own description and error ids.
valuenumber | nullNoneThe rating (controlled): a number from 0 to max, or null for none.
defaultValuenumber | nullnullSets the starting rating when uncontrolled.
onValueChange((value: number | null) => void)NoneCalled with the new value, or null when cleared.
maxnumber5Number of icons the rating shows.
allowHalfbooleanfalseAllows half-star steps (0.5, 1, 1.5 …).
clearablebooleantrueClick the current value (or press Backspace / Delete) to clear it.
readOnlybooleanfalseShow the value as an image only: not focusable, no hover.
size"sm" | "md" | "lg""md"Icon size: "sm" (16px), "md" (24px, default) or "lg" (32px).
tone"accent" | "success" | "warning" | "danger""warning"Colour of the filled icons: "warning" (gold, default), "accent", "danger" or "success".
iconReactNodea starSets a custom filled icon (an SVG using currentColor).
emptyIconReactNodeicon drawn as a faint outlineSets a custom empty icon.
valueLabelsstring[]NoneWords for each whole value, shown next to the icons, e.g. ["Poor", …, "Excellent"].
showValuebooleanfalseShow the number (e.g. "4.5") next to the icons.
localestringNoneLocale used to format the number.
labelReactNodeNoneVisible text above the icons; it names the group.
descriptionReactNodeNoneMuted helper text under the icons.
aria-labelstringNoneAccessible name when there is no visible label.
aria-labelledbystringNoneId of an element that names the rating.
labelsPartial<RatingLabels>defaultRatingLabelsEvery other string, for translation: labels={{ item: "…" }}; omitted keys keep the English of defaultRatingLabels. All 3 keys of RatingLabels are listed in the key table under the props table.
classNamestringNoneAdds extra classes to the outer element.
RatingLabels keys and English defaults (3)
Pass any key in labels; the rest keep the defaults of defaultRatingLabels.
KeyEnglish default
item(value) => `${value} ${value === 1 ? "star" : "stars"}`
readOnly(value, max) => `Rated ${value} out of ${max}`
clearHintPress Backspace to clear

Keyboard

KeysAction
Tab/Shift + TabEnter or leave the group once.
Arrow keysChange the value (and select it).
Backspace/DeleteClear the value.

Accessibility

  • A radiogroup of stars named by aria-label; each item has a spoken name ("3 of 5 stars", customisable through labels).
  • readOnly shows a score as an image (role="img") whose label states the value (translate it with labels.readOnly).
  • Pointer, touch and keyboard all select; the group announces how to clear the value.

All exports on this page

  • Rating — Rating: stars (or any icon) you can pick with the pointer, touch or keyboard.
  • defaultRatingLabels — Default English strings of RatingLabels; spread it and override single keys to reword.

When to use Rating

  • Use it as a star rating for a review form where a buyer picks one to five stars, with valueLabels showing words such as Poor to Excellent while hovering and once selected.
  • Use it for a feedback prompt in the corner of a help article, with allowHalf off and clearable on so a person can take their answer back by clicking the same star.
  • Use it with readOnly to display an average on a product card, where it renders a single image labeled like Rated 4.5 out of 5 and shows half stars.
  • Skip it when you need a long scale or a bipolar choice such as a 0 to 10 satisfaction score; a Slider or a SegmentedControl reads better for that.
  • Pair it with Textarea for the written review under the stars, Field for outer labeling and errors, and Badge or DescriptionList to show review counts next to a read-only score.

Frequently asked questions

67 components pages in all: browse them on All components.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .