Rating works in two modes. Interactive mode is a group of native radio inputs: hover previews a score, a click or the arrow keys pick it, and the stars light up one after another with a ring around the chosen one. The fill, the preview and the flash are CSS through :has() and sibling selectors, so the component is a Server Component and ships no JavaScript. Read-only mode shows fractional values like 4.7 and gives screen readers a plain text summary.
Browser Support
Styles elements based on their contents
Styles validation after interaction
Colors unselected stars
Preview
Hover, click or tab into the stars and use the arrow keys. Works with JavaScript turned off.
Not rated yet
Installation
Usage
import { Rating } from "@/components/ui/rating"
// A Server Component: the score lives in the radio inputs
export default function Example() {
return <Rating name="score" defaultValue={4} />
}
// Inside a client component you can still listen to changes
<Rating value={value} onValueChange={setValue} />Examples
Read-only with fractions
With readOnly the top layer of each star is cropped to its share of the value, so 4.7 shows four full stars and most of the fifth.
<Rating readOnly value={4.7} />
<Rating readOnly value={3.4} />Shapes, sizes and scale
shape switches between a five-pointed star and an eight-pointed sparkle. max changes the scale, and the color follows the text color, so a text-* class recolors it.
<Rating shape="sparkle" defaultValue={3} size="lg" />
<Rating shape="sparkle" max={10} defaultValue={7} size="sm" className="text-foreground" />In a form
name becomes the name of the radio group, so the score is sent with the form, even before hydration. allowClear adds a hidden "no rating" radio and lays its label over the selected star, so a second click clears the score without JavaScript.
import { Button } from "@/components/ui/button"
<form action={submit}>
<Rating name="quality" allowClear />
<Button type="submit">Send</Button>
</form>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | number | null | - | Selected score in controlled mode, null when empty |
| defaultValue | number | null | null | Initial score in uncontrolled mode |
| onValueChange | (value: number | null) => void | - | Called with the new score or null after clearing. Only inside client components |
| max | number | 5 | Number of stars |
| size | "sm" | "md" | "lg" | "md" | Star size |
| shape | "star" | "sparkle" | "star" | star is five-pointed, sparkle is eight-pointed |
| readOnly | boolean | false | Display mode with fractional fill |
| disabled | boolean | false | Turns off interaction |
| name | string | - | Name of the radio group for form submission |
| required | boolean | false | The form is not sent until a score is picked |
| allowClear | boolean | false | A second click on the selected star clears the score |
| labels | Partial<RatingLabels> | - | Accessible texts: group name, per-star name, read-only summary, no rating option |
| className | string | - | Additional CSS classes. A text-* class sets the star color |
Features
- No JavaScript: native radio inputs and CSS, works in Server Components and before hydration
- Keyboard: native radio group: Tab enters it once, the arrow keys move the score
- Touch friendly: the hover preview sits behind @media (hover: hover), so stars do not get stuck
- Reduced motion: the ring and the flash are turned off when the system asks for less motion
- No dependencies: shapes are inline SVG, keyframes live in the component