Cómo funciona
Each star has two stacked layers, an opaque grey and the lit colour; lighting only changes the top layer. Stars lit only by the hover preview use a lighter tint, and the star that carries the value gets a soft radial glow. Hover preview ignores touch. After a click, the lit stars remount with a new key so a CSS keyframe pop replays, staggered by index. Semantics are a standard radio group with one Tab stop. Each star sits in a 44×44 cell pulled back with negative margins, so the layout size is just the star.
Úsalo para
- Rating an order, a trip or a purchase after it finishes.
- Showing an average score in a card or list, using readOnly with a fractional value.
- Short feedback forms where one tap is enough.
Busca otra opción cuando
- Fine-grained scores such as 0 to 100: use a slider or a number field.
- Yes or no feedback: use two buttons.
- Comparing many items by rating: use a sortable number column.
Uso
import { useState } from 'react'
import { RatingStars } from './RatingStars.jsx'
export function OrderReview() {
const [score, setScore] = useState(0)
return (
<RatingStars
aria-label="How was your order?"
value={score}
onChange={setScore}
labels={['Poor', 'Fair', 'Okay', 'Good', 'Excellent']}
placeholder="Tap a star to rate"
/>
)
}
// Read-only display (fractions supported): <RatingStars readOnly value={4.5} size={14} />Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
value | number | — | Controlled: current score, 0 means not rated yet. Can be fractional when read-only. |
defaultValue | number | 0 | Uncontrolled: initial score. |
onChange | (value: number) => void | — | Called when the score changes (click or keyboard); 0 when cleared. |
onHoverChange | (value: number) => void | — | Called when the hover preview changes: the star pointed at, or 0 when the pointer leaves. |
count | number | 5 | Number of stars. |
size | number | 32 | Star side length in px. When clickable, each star's click area is at least 44×44. |
readOnly | boolean | false | Read-only: only shows the score (fractions allowed), is read as one image by screen readers, and tightens the star spacing. |
disabled | boolean | false | Disabled: dimmed, not clickable, not focusable. |
allowClear | boolean | true | Clicking the current score again clears it; Backspace or Delete clears from the keyboard. |
labels | ReactNode[] | — | Text for each score, labels[0] for 1 star. The text area reserves the width of the longest one. |
placeholder | ReactNode | — | Hint shown in the text area before anything is rated, such as “Tap a star to rate” (in text-3). |
labelPlacement | 'end' | 'bottom' | 'end' | Where the text goes: to the right of the stars, or centred below. |
precision | number | 0.5 | A fractional value is drawn to the nearest step: halves by default (4.3 draws 4½ stars, the label still says 4.3); 0 or 1 draws it exactly. A half is cut through the star's top point, so it reads as a clean half at any size. |
starLabel | (value: number, count: number) => string | (value, count) => `${value} of ${count} stars` | Screen-reader name of each star. String entries in labels are appended after it. |
name | string | — | Form field name: renders a hidden input that is submitted with the form. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root element, such as className, style and aria-label / aria-labelledby (the group's name). Without a name the group is called “Rating”, and read-only is “Rating 4.5 of 5 stars”; pass aria-label to localize. |
Accesibilidad
- The root is a radiogroup and each star a radio named “3 of 5 stars”, with the label text appended.
- The group has one Tab stop; arrow keys change the score and focus follows, Home and End jump, Backspace and Delete clear.
- Read-only is a single image named, for example, “Rating 4.5 of 5 stars”, and is not focusable.
- Every star click area is at least 44×44, and the focus ring hugs the star.
- Reduced motion drops the pop animation and keeps the colour change.
Movimiento
Lighting up grows from 0.6× with a 240ms spring, going out is a quick 100ms ease-in. Hover lifts a star to 1.15× with a spring; press dips it to 0.88×. After a click each lit star presses to 0.72× and springs back, 40ms apart by index, and the glow behind the chosen star swells in over 360ms.
Añádelo a tu proyecto
Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.
RatingStars.jsxrating-stars.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.





