Search documentation

Search pages, components, blocks, styles and colours.

Get started
Skip to content

Rating

PreviousNext

A row of stars that holds a score, in whole or half stars, yellow by default or in your primary and status colours.

Hover the stars to preview a score, then click to set it. With step={0.5} the left half of a star sets half a star. Drag across the row to scrub, or use the arrow keys. Read-only, it shows any fraction, such as 4.3.

Rating is part of the Fabricator style. It's built on HTML, so the same component ships for every base.

Installation

bunx --bun fabricator-ui@latest add rating

Usage

import { Rating } from "@/components/ui/rating"
<Rating defaultValue={4} aria-label="Your rating" />

Composition

Rating is one component. Its parts carry a data-slot for styling from outside:

Rating                    data-slot="rating"
└── span (per star)       data-slot="rating-star" data-state="full | half | partial | empty"
    ├── span              data-slot="rating-star-empty"
    └── span (clipped)    data-slot="rating-star-fill"

Examples

Variants

The default stars are yellow, the colour everyone reads as a score. primary follows your primary colour, and info, success, warning and error take the status colours, for a score that reads as good or bad.

Default
Primary
Info
Success
Warning
Error

Sizes

Read-only

readOnly shows the score as an image for screen readers, such as "Average rating: 4.3 out of 5 stars". It keeps the exact fraction: 4.3 fills a third of the fifth star.

4.31,284 reviews

Controlled

Pass value and onValueChange to hold the score in your state. onValueCommitted reports where a click, a drag or a key landed.

2.5 / 5

Field

Inside a Field, name the rating with aria-labelledby pointing at the FieldLabel, and set aria-invalid with the field's data-invalid: the empty stars turn red. name submits the score with the form.

Half stars count. Your review shows your name.

Disabled

Motion

The star under the pointer lifts to 115% on the feedback timing and gives to 95% while pressed. A star the score reaches pops in from half size on the bounce spring. Reduce Motion or motion off keeps the stars still.

API Reference

PropTypeDefault
valuenumber(none)
defaultValuenumber0
onValueChange(value: number) => void(none)
onValueCommitted(value: number) => void(none)
maxnumber5
step1 | 0.51
variant"default" | "primary" | "info" | "success" | "warning" | "error""default"
size"sm" | "default" | "lg""default"
readOnlybooleanfalse
disabledbooleanfalse
namestring(none)
getAriaValueText(value: number, max: number) => string"3 out of 5 stars"
aria-labelstring"Rating"

The root is a div and takes its props and ref. A value you set keeps to step; a read-only value keeps its fraction.

Accessibility

Rating is a slider from 0 to max: it takes focus as one control and announces its name and score, such as "Your rating, 3.5 out of 5 stars". Pass getAriaValueText to read the score in another language. Read-only, it's an image named with the score.

KeyDoes
← / ↓step less
→ / ↑step more
Page Down / Page UpOne star less or more
Home / EndNo stars / every star
0 to 9That many stars

A touch drag across the stars scrubs the score; a vertical one scrolls the page.