Playground

Rating

Pick or show a score from 1 to a maximum, as stars.

  • Built on native elements
  • Editable: a radio group (native radios) with arrow-key selection; each star is named "N of M".
  • Read-only: role="img" with a text label such as "4 out of 5".
  • Styles in one file: rating.css

Import

import '@monoline/css';
import {Rating} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add rating

Usage

import {Rating} from '@monoline/react';

<Rating aria-label="Rate this page" defaultValue={3} />

Examples

Editable

Read only

Props

RatingProps

PropTypeDefaultDescription
aria-label requiredstring–Names the control, for example "Rate this article".
valuenumber–The current score (0 to `max`) when controlled. Pair with `onValueChange`.
defaultValuenumber0The initial score when uncontrolled. Default 0.
onValueChange((value: number) => void)–Called with the new score (1 to `max`) when the user picks a star.
maxnumber5Number of stars. Default 5.
readOnlyboolean–Shows the score without letting anyone change it.
namestring–Name of the underlying radio inputs, so the score submits with a native form. A unique name is generated if omitted.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[data-on]rating.css

CSS classes

ClassStylesheet
.ratingrating.css
.rating-starrating.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.rating {
  border-color: var(--accent);
}
.rating[data-on] {
  background: var(--wash);
}

Accessibility

  • Editable: a radio group (native radios) with arrow-key selection; each star is named "N of M".
  • Read-only: role="img" with a text label such as "4 out of 5".