Rating
A score on a small scale, given with a press on a star or with the arrows.
Installation
pnpm dlx @human-kit/design add ratingnpx @human-kit/design add ratingyarn dlx @human-kit/design add ratingbunx @human-kit/design add ratingThe command copies the files to src/lib/components/ui/rating. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Rating from '$lib/components/ui/rating';
let score = $state(0);
</script>
<Rating.Root label="How was the visit?" bind:value={score} /> Examples
Half stars
precision={0.5} takes half values: the pointer fills half a star at its near side, and each arrow key moves by a half.
Showing a score
readonly shows a score that cannot be changed, as the average of a list. It stays in the tab order, so a screen reader can still reach the value.
- Dr. Laura Gómez 128
- Dr. Andrés Pérez 64
- Dr. Sofía Ríos 19
Guidelines
- Use it to ask for, or to show, a score on a short scale where more is better: a visit, a service, a result.
- Keep the five stars of the default
count. A longer scale is hard to read at a glance, and stars do not suit a scale with a middle. - Ask whole stars when a person gives the score; half stars are for an average that you show.
- Turn
showValueon when the exact value matters, as "3.5 of 5" next to an average. - A press on the chosen star clears the score. Turn
allowClearoff when a score, once given, must stay.
Accessibility
- With whole stars it is a
role="radiogroup"and each star arole="radio"named "3 of 5". With half stars it is onerole="slider", since a radio cannot say 3.5. - The arrows move the score by one step,
HomeandEndgo to the lowest and the highest score, andDeleteorBackspaceclear it whileallowClearis on. labelnames the rating; without one, give it anaria-label. The description is read with it througharia-describedby.invalidsetsaria-invalidand turns the label red.requiredsetsaria-required.- The stars shrink a little on a press, unless motion is reduced.
API reference
| Prop | Type | Default |
|---|---|---|
value | number The score, from 0 to | 0 |
defaultValue | number The score at the start, when you give no | — |
count | number How many stars there are. | 5 |
precision | number The step between two scores. 0.5 takes half stars. | 1 |
size | 'sm' | 'default' | 'lg' The size of a star: 16px, 20px or 24px. | 'default' |
label | string The name over the stars. Without it, name the rating with | — |
description | string A line under the stars. | — |
showValue | boolean Shows the score as text at the end of the label's line. | false |
onChange | (value: number, details: RatingChangeDetails) => void Called on each change. | — |
onChangeEnd | (value: number, details: RatingChangeDetails) => void Called when a run of changes ends: on a press, or when a key is let go. For work that must not run on each key repeat. | — |
allowClear | boolean A press on the current star, or | true |
controlledValue | boolean Stops writing back to | false |
getItemLabel | (value: number, count: number) => string The name of one star, read aloud. "3 of 5" by default. | — |
getValueText | (value: number, count: number) => string The text of the score, shown and read aloud. "No rating" at 0. | — |
invalid | boolean Turns the label red and sets | false |
required | boolean Sets | false |
readonly | boolean Keeps the stars in the tab order, but the score does not change. | false |
disabled | boolean Fades the rating and takes it out of the tab order. | false |
name | string The name of the hidden input that carries the score in a form. | — |
form | string The id of the form of the hidden input, when the rating is outside it. | — |
element | HTMLDivElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>, on the rating.