Skip to content

Rating

A score, as a row of stars. Left choosable it is a radio group; set to readOnly it becomes a single picture reporting an average.

tsx
import { Rating } from 'neba';

<Rating defaultValue={4} />;

Props

PropTypeDefaultDescription
valuenumberHow much is rated. Use with onValueChange for a controlled Rating
defaultValuenumber0Where an uncontrolled Rating starts
onValueChange(value: number) => voidCalled with the new score. 0 is what a cleared Rating reports
countnumber5How many stars there are, and therefore the highest score
precisionnumber1The smallest step that can be chosen: 0.5 gives half stars. It bounds what a reader can pick and nothing else: a value of 4.3 is drawn as 4.3 at every precision
iconReactNodeThe glyph a filled star is drawn with
emptyIconReactNodeAnd the one an empty star is drawn with. Has to be the same shape
clearablebooleantrueChoosing the score that is already chosen clears it back to 0
readOnlybooleanfalseShows the score without letting it be changed: an average, somebody else’s rating. The inputs go and one role="img" is left; the one readOnly in the library that does not drain the saturation
disabledbooleanfalseUnavailable. Drops the colour family for neutral grey
namestringIdentifies the value when a form is submitted
requiredbooleanfalseA form will not submit until a star has been chosen
sizeshared'xs' | 'sm' | 'md' | 'lg' | 'xl''md'Height and type scale
colorshared'primary' | 'secondary' | 'success' | 'warning' | 'danger' | 'info''warning'Semantic colour role. warning by default(the amber a star is expected to be) which makes this the one component whose default colour is chosen by what the object is
localestring'en'Which language the accessible names are written in: a BCP 47 tag. Unsupported tags fall back to English
labelstringNames the whole control. Defaults to the locale’s word for "Rating"
valueLabel(value: number, count: number) => stringWhat one star, and the whole control once it is read only, is called. Defaults to the locale’s way of saying "3 out of 5"

Every other <div> attribute passes through to the root, except onChange: the change worth listening for is onValueChange.

The shared axes (size color) are defined in prop conventions.

Examples

count, precision

count is how many stars there are and therefore the highest score. precision is the smallest step that can be chosen: at 0.5 each star is split into two hit areas and half stars can be picked.

precision bounds what can be chosen and nothing else. A value of 4.3 is drawn as four stars and a third at every precision: an average is not a choice, and rounding it to the nearest half would be reporting a different number from the one it was handed.

readOnly

readOnly is a picture rather than a control. No inputs are rendered at all and one role="img" carries the score as a sentence, so a star display never leaves twenty tab stops on a page that was reporting a number.

It is also the one readOnly in the library that does not drain the saturation: this is not a control being held still, it is the value itself, and a row of grey stars would say the score was unavailable.

size, color

size takes the height of one star from the standalone-glyph ladder. color is the one place in the library where the default is warning: the amber a star is expected to be.

icon, emptyIcon

Both glyphs can be replaced. The filled copy is laid over the empty one and clipped to a percentage of the width, so the two have to be the same shape for a half star to land on the outline underneath it.

clearable, disabled

Choosing the star that is already chosen clears the score back to 0. clearable={false} stops that.

locale, label, valueLabel

The only words this component invents are its accessible names, and locale decides their language. It takes a BCP 47 tag such as ko, pt-BR or zh-Hant; unsupported tags fall back to English. label names the group and valueLabel writes both one star's name and the read-only sentence.

tsx
<Rating locale="ko" />
<Rating label="How was the order?" valueLabel={(value, count) => `${value}/${count} stars`} />

In a form

Give it a name and the radios are submitted under it. required stops the form until a star has been chosen.

tsx
<form>
  <Rating name="score" required />
</form>

Accessibility

  • A choosable Rating is a role="radiogroup" built out of real <input type="radio">s: one tab stop for the row, arrow keys within it, aria-checked on the one that is taken, and a value in a form submission.
  • Each star is read out as "3 out of 5" rather than as "3 stars", because a count of stars is a plural in most languages and a fraction in none of them.
  • readOnly removes every input and leaves a single role="img".
  • Set locale so the names are read in the page's language, or write them yourself with label and valueLabel.

Released under the MIT License