component
Rating
Stars out of five: a score to read, or one to give. Lit stars in the warning yellow, the rest in Input's edge grey.
Try it
Every variant
DefaultHover, press or tab to one to see the other 1 states.
import { Rating } from 'rost/components/rating/Rating'
// A score to read
<Rating value={4.5} />
// A score to give
<Rating label="Rate this template" value={score} onValueChange={setScore} /> src/components/rating/Rating.tsx
import { useRef, useState, type KeyboardEvent } from 'react'
import './rating.css'
export interface RatingProps {
/** How many stars are lit, 0 to 5. Halves show when it's only for reading. */
readonly value: number
/** With it, the stars are buttons and a press gives that score. */
readonly onValueChange?: (value: number) => void
/** What's rated, for screen readers, when you can give a score. */
readonly label?: string
readonly className?: string
}
const b = 'rost-rating'
const STARS = [1, 2, 3, 4, 5]
/**
* Stars out of five. Read-only it's an image named "3 out of 5"; with
* `onValueChange` it's a `role="radiogroup"` of five radios with a roving
* Tab stop. Built from contracts/rating.contract.yaml.
*/
export function Rating({ value, onValueChange, label, className }: RatingProps) {
const [hover, setHover] = useState<number | null>(null)
const buttons = useRef<(HTMLButtonElement | null)[]>([])
const shown = Math.max(0, Math.min(5, hover ?? value))
const fill = <span className={`${b}__fill`} style={{ width: `${shown * 20}%` }} />
const cls = [b, className].filter(Boolean).join(' ')
if (!onValueChange) {
const said = `${Math.round(value * 10) / 10} out of 5`
return (
<span className={cls} role="img" aria-label={said}>
<span className={`${b}__track`} />
{fill}
</span>
)
}
const pick = (n: number) => onValueChange(n === value ? 0 : n)
const given = Math.round(value)
const move = (n: number) => {
const to = Math.max(1, Math.min(5, n))
onValueChange(to)
buttons.current[to - 1]?.focus()
}
const onKeyDown = (e: KeyboardEvent) => {
const at = given || 0
const go: Record<string, () => void> = {
ArrowRight: () => move(at + 1), ArrowUp: () => move(at + 1),
ArrowLeft: () => move(at - 1), ArrowDown: () => move(at - 1),
Home: () => move(1), End: () => move(5),
}
if (go[e.key]) { e.preventDefault(); go[e.key]() }
}
return (
<span className={cls} role="radiogroup" aria-label={label} onMouseLeave={() => setHover(null)}>
<span className={`${b}__track`} aria-hidden="true" />
{fill}
<span className={`${b}__options`} onKeyDown={onKeyDown}>
{STARS.map((n) => (
<button
key={n}
ref={(el) => { buttons.current[n - 1] = el }}
type="button"
role="radio"
aria-checked={given === n}
aria-label={n === 1 ? '1 star' : `${n} stars`}
tabIndex={given === n || (given === 0 && n === 1) ? 0 : -1}
className={`${b}__option`}
onMouseEnter={() => setHover(n)}
onClick={() => pick(n)}
/>
))}
</span>
<span className={`${b}__focus-ring`} aria-hidden="true" />
</span>
)
}
The source for everything on this page: contracts/rating.contract.yaml (opens in a new tab). The CSS, the docs and the checks in each app are built from it.
# yaml-language-server: $schema=../schema/component-contract.schema.json
schemaVersion: 2
id: rating
name: Rating
kind: component
status: review
summary: "Stars out of five: a score to read, or one to give. Lit stars in the warning yellow, the rest in Input's edge grey."
category: inputs
related: [ slider, radio ]
source:
tool: figma
fileKey: 1Zy0wiWKXQAvsfDBX3KR0d
nodeId: "773:127"
url: https://www.figma.com/design/1Zy0wiWKXQAvsfDBX3KR0d/UI-Kit-v01?node-id=773-127
extractedAt: "2026-11-30"
extractedBy: hand
frozenAt: "2026-11-30"
props:
value: { type: number, required: true, notes: "How many stars are lit, from 0 to 5. A half (3.5) lights half a star when it's only for reading." }
label: { type: string, notes: "What's rated, for screen readers, when you can give a score: Rate this invoice template." }
onValueChange: { type: node, notes: "With it, the stars are buttons and a press gives that score. Without it, Rating only shows `value`." }
states: [ Default, Focus ]
stateSelectors:
Focus: ":has(.rost-rating__option:focus-visible)"
elements:
root:
notes: "Five 24px tiles of Rost's star icon in a row, 120px by 24px."
styles:
display: { value: inline-block, assert: false }
position: { value: relative, assert: false }
vertical-align: { value: middle, assert: false }
width: { value: 120px, notes: "Five stars of spacing/size/small." }
height: { token: spacing/size/small, value: 24px }
track:
notes: "All five stars, unlit."
styles:
display: { value: block, assert: false }
position: { value: absolute, assert: false }
inset: { value: "0", assert: false }
mask: { value: url(./icon-star.svg) left center / var(--spacing-size-small) var(--spacing-size-small) repeat-x, untokenised: "The icon file, repeated at spacing/size/small." }
background: { token: rating/off, value: "#747573" }
fill:
notes: "The lit stars: the same row in yellow, as wide as `value` of the five, set inline."
styles:
display: { value: block, assert: false }
position: { value: absolute, assert: false }
left: { value: "0", assert: false }
top: { value: "0", assert: false }
height: { value: 100%, assert: false }
mask: { value: url(./icon-star.svg) left center / var(--spacing-size-small) var(--spacing-size-small) repeat-x, untokenised: "The icon file, repeated at spacing/size/small." }
background: { token: rating/on, value: "#fda50d" }
options:
optional: true
notes: "When you can give a score: five invisible radio buttons over the stars, one per star."
styles:
display: { value: flex, assert: false }
position: { value: absolute, assert: false }
inset: { value: "0", assert: false }
option:
optional: true
notes: "One star's button, 24px square, with nothing of its own to see. Pointing at it lights the stars up to it."
styles:
display: { value: block, assert: false }
flex: { value: 1 1 0, assert: false }
height: { value: 100%, assert: false }
padding: { value: "0", assert: false }
border: { value: none, assert: false }
background: { value: transparent, assert: false }
outline: { value: none, assert: false }
cursor: { value: pointer, assert: false }
focus-ring:
optional: true
notes: "Input's ring around the five stars, while a star has the keyboard's focus."
styles:
display: { value: none, assert: false }
position: { value: absolute, assert: false }
inset: { value: "calc(-1 * var(--spacing-gap-2xsmall))", untokenised: "Composed from a token: the ring sits spacing/gap/2xsmall outside the stars." }
border-style: { value: solid, assert: false }
border-width: { token: spacing/outline/medium, value: 2px }
border-radius: { token: spacing/radius/small, value: 8px }
pointer-events: { value: none, assert: false }
variants:
- when: { state: Default }
figmaNode: "773:84"
elements: {}
- when: { state: Focus }
figmaNode: "773:105"
elements:
focus-ring:
display: { value: block, assert: false }
border-color: { token: rating/focus-ring, value: "#00d669" }
guidance:
status: proposed
use:
- title: A score to read
text: "A review's stars next to its text, an average next to a count: 4.5 from 120 reviews."
show: "state=Default"
- title: A score to give
text: "Asking how something went, in one tap, at the end of a flow."
avoid:
- title: More than five steps
text: "Ten points or a percentage is a **Slider** or a number, not more stars."
- title: The only way to say it
text: "Stars say how much, not why. When the why matters, put a **Textarea** after them."
behaviour: "Read-only, it lights `value` of five stars, halves included. Giving a score, pointing at a star lights the stars up to it, and a press gives that score; pressing the score already given takes it back to 0."
accessibility:
element: "Read-only, the stars are `role=\"img\"` named 3 out of 5. Giving a score, it's `role=\"radiogroup\"` of five `role=\"radio\"` buttons named 1 star to 5 stars, the ARIA pattern for one choice in a row."
name: "`label` names the group."
keyboard: "Tab reaches the given score, or the first star when there's none. Left and Right, Up and Down move the score by one, Home and End to 1 and 5."
focus: "`focus-ring` around all five, on `:focus-visible`, while the lit stars show where the focus is."
colour: "The lit yellow is 9:1 on the dark page, the unlit grey 4.3:1, so an unlit star still reads as a star."
content: "Pair a read-only score with its number and its count, since five stars can't show 4.3."
Install
npx github:Runceanu/rost add rating It lands in src/rost/rating/, with what it needs and its contract. Install has the rest.
When to use
A score to read
A review's stars next to its text, an average next to a count: 4.5 from 120 reviews.
A score to give
Asking how something went, in one tap, at the end of a flow.
When not to use
More than five steps
Ten points or a percentage is a Slider or a number, not more stars.
The only way to say it
Stars say how much, not why. When the why matters, put a Textarea after them.
Behaviour
Read-only, it lights value of five stars, halves included. Giving a score, pointing at a star lights the stars up to it, and a press gives that score; pressing the score already given takes it back to 0.
Accessibility
- Element
Read-only, the stars are
role="img"named 3 out of 5. Giving a score, it'srole="radiogroup"of fiverole="radio"buttons named 1 star to 5 stars, the ARIA pattern for one choice in a row. - Name
labelnames the group. - Keyboard
Tab reaches the given score, or the first star when there's none. Left and Right, Up and Down move the score by one, Home and End to 1 and 5.
- Focus
focus-ringaround all five, on:focus-visible, while the lit stars show where the focus is. - Colour
The lit yellow is 9:1 on the dark page, the unlit grey 4.3:1, so an unlit star still reads as a star.
Content
Pair a read-only score with its number and its count, since five stars can't show 4.3.
Props
| Prop | Type | Values | Default |
|---|---|---|---|
value | number | | |
label | string | | |
onValueChange | node | | |
States: Default, Focus.
Values
Straight from the contract. Each value sits next to its token, because six tokens here all come out as #3a3a3a and the colour alone can't tell you which one is right.
Never changes
root
-
width120pxno token yet -
height24pxspacing/size/small
displayinline-block · positionrelative · vertical-alignmiddle
track
-
maskurl(./icon-star.svg) left center / var(--spacing-size-small) var(--spacing-size-small) repeat-xon purpose: The icon file, repeated at spacing/size/small. -
background#747573rating/off
displayblock · positionabsolute · inset0
fill
-
maskurl(./icon-star.svg) left center / var(--spacing-size-small) var(--spacing-size-small) repeat-xon purpose: The icon file, repeated at spacing/size/small. -
background#fda50drating/on
displayblock · positionabsolute · left0 · top0 · height100%
options
displayflex · positionabsolute · inset0
option
displayblock · flex1 1 0 · height100% · padding0 · bordernone · backgroundtransparent · outlinenone · cursorpointer
focus-ring
-
border-width2pxspacing/outline/medium -
border-radius8pxspacing/radius/small
displaynone · positionabsolute · insetcalc(-1 * var(--spacing-gap-2xsmall)) · border-stylesolid · pointer-eventsnone
Only in some variants
These don't change: they're just set in some variants and not others.
Focus
-
focus-ring.border-color#00d669rating/focus-ring -
focus-ring.displayblocklayout