Skip to the content
human-kit design
12 — Form

Rating

A score on a small scale, given with a press on a star or with the arrows.

How was the visit? 4 of 5

Installation

pnpm dlx @human-kit/design add rating
npx @human-kit/design add rating
yarn dlx @human-kit/design add rating
bunx @human-kit/design add rating

The 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.

Clinic score 3.5 of 5

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 showValue on when the exact value matters, as "3.5 of 5" next to an average.
  • A press on the chosen star clears the score. Turn allowClear off when a score, once given, must stay.

Accessibility

  • With whole stars it is a role="radiogroup" and each star a role="radio" named "3 of 5". With half stars it is one role="slider", since a radio cannot say 3.5.
  • The arrows move the score by one step, Home and End go to the lowest and the highest score, and Delete or Backspace clear it while allowClear is on.
  • label names the rating; without one, give it an aria-label. The description is read with it through aria-describedby.
  • invalid sets aria-invalid and turns the label red. required sets aria-required.
  • The stars shrink a little on a press, unless motion is reduced.

API reference

PropTypeDefault
valuenumber

The score, from 0 to count. 0 is no score. Bindable.

0
defaultValuenumber

The score at the start, when you give no value.

—
countnumber

How many stars there are.

5
precisionnumber

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'
labelstring

The name over the stars. Without it, name the rating with aria-label.

—
descriptionstring

A line under the stars.

—
showValueboolean

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.

—
allowClearboolean

A press on the current star, or Delete, puts the score back to 0.

true
controlledValueboolean

Stops writing back to value: it reports through onChange only, so the parent can refuse a change.

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.

—
invalidboolean

Turns the label red and sets aria-invalid.

false
requiredboolean

Sets aria-required.

false
readonlyboolean

Keeps the stars in the tab order, but the score does not change.

false
disabledboolean

Fades the rating and takes it out of the tab order.

false
namestring

The name of the hidden input that carries the score in a form.

—
formstring

The id of the form of the hidden input, when the rating is outside it.

—
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>, on the rating.