Rating

Slant / Forms

A score out of five, or any maximum, drawn in slanted pixels.

Demo

Usage

@tuttn/slant
import { Field, Rating } from '@tuttn/slant'

// Controlled: pass value and onValueChange. 0 means not rated.
const [score, setScore] = useState(0)
<Rating value={score} onValueChange={setScore} label="Handoff quality" />

// Uncontrolled: it keeps its own value from defaultValue; max sets the number of marks.
<Rating defaultValue={3} max={10} size="sm" label="Priority" />

// Read-only: a picture of the score, read as "Rated 4 of 5".
<Rating value={4} readOnly />

// In a form, inside a Field: the label names the radiogroup, and a name submits the number.
<Field required invalid={score === 0}>
  <Field.Label>How was the handoff?</Field.Label>
  <Rating value={score} onValueChange={setScore} name="score" />
  <Field.Error>Pick a score.</Field.Error>
</Field>

Props

PropTypeDefaultDescription
valuenumber—The rating, from 0 (none) to `max`. Pass it to control the rating; leave it out to let it keep its own.
defaultValuenumber0The rating an uncontrolled control starts at.
onValueChange(value: number) => void—Called with the new rating when a click or an arrow key sets it; 0 when a click on the current mark clears it.
maxnumber5How many marks.
labelstring—The radiogroup's accessible name. A Field's label names it instead, through aria-labelledby.
readOnlyboolean—Shows the rating as a picture (role="img", read as "Rated 4 of 5") with no buttons.
disabledboolean—Dims the marks and ignores input. A disabled rating is not submitted with its form.
size'sm' | 'md' | 'lg''md'The marks' size: 10, 14 or 18px.
namestring—Submits the rating with a form: a hidden input with this name carries the number.
formstring—The id of the form the hidden input belongs to, when the rating sits outside it.
requiredboolean—Marks the radiogroup required (aria-required).
invalidboolean—Marks the radiogroup invalid (aria-invalid).
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref reach the root: the radiogroup, or the role="img" element when read-only.

Keyboard

The rating is a radiogroup with one tab stop: the chosen mark, or the first while nothing is chosen. The arrow keys move and set the rating as they go, as a radio group's do (Left and Right swap under right-to-left text), and Home and End jump to the lowest and highest. Space or a click on the current mark clears the rating to 0. Each mark is named from messages.ratingValue, "3 of 5"; a pointer hovering the marks previews the rating it would set.

Read-only and forms

Read-only, the rating is one element with role="img" named from messages.rated ("Rated 4 of 5"), and no buttons; a fractional value fills the whole marks below it. With a name, a hidden input carries the number (also when read-only; not when disabled). Inside a Field, the field's label names the radiogroup through aria-labelledby, its description and error describe it, and required and invalid set aria-required and aria-invalid.