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.jsonHow was your stay?
Tap a star to rate.
RatingdefaultRatingLabels
Using it in a form? See the react-hook-form and zod recipe.
Basic
Hover to preview, click to rate, click the same star again to clear (or press Backspace).
import { Rating } from "@/components/sheenui/rating/rating";
// Package build (after the first npm release, or a packed tarball): import { Rating } from "@sheenui/theme";
export function Example() {
return (
<Rating label="Rate your stay" defaultValue={4} />
);
}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.tslib/{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.
import { useState } from "react";
import { Rating } from "@/components/sheenui/rating/rating";
// Package build (after the first npm release, or a packed tarball): import { Rating } from "@sheenui/theme";
export function Example() {
const [value, setValue] = useState<number | null>(3.5);
return (
<Rating
label="Overall"
allowHalf
value={value}
onValueChange={setValue}
valueLabels={["Poor", "Fair", "Good", "Great", "Excellent"]}
showValue
size="lg"
/>
);
}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.
import { Rating } from "@/components/sheenui/rating/rating";
// Package build (after the first npm release, or a packed tarball): import { Rating } from "@sheenui/theme";
export function Example() {
return (
<>
<Rating readOnly value={4.5} size="sm" showValue />
<Rating readOnly value={4.5} size="md" showValue />
<Rating readOnly value={4.5} size="lg" showValue />
</>
);
}Custom icon and tone
Any SVG that uses currentColor. Labels are yours to translate.
import { Rating } from "@/components/sheenui/rating/rating";
// Package build (after the first npm release, or a packed tarball): import { Rating } from "@sheenui/theme";
import { Heart as HeartIcon } from "lucide-react";
export function Example() {
return (
<Rating
aria-label="Favorite level"
icon={<HeartIcon fill="currentColor" />}
emptyIcon={<HeartIcon />}
tone="danger"
max={6}
defaultValue={3}
labels={{ item: (v) => `${v} ${v === 1 ? "heart" : "hearts"}` }}
/>
);
}Props
RatingLabels keys and English defaults (3)
Keyboard
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
Related pages
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .