Dashral Pro is hereSee pricing

Dashral

RatingNew

A star rating that works with the keyboard and in forms, and shows an average.

$ pnpm dlx shadcn@latest add @dashral/rating

Pick the same star again to clear it.

Installation

$ pnpm dlx shadcn@latest add @dashral/rating

Adds the component, the components it builds on, its npm packages and the theme tokens it uses. Or by its full URL: https://dashral.com/r/rating.json

Usage

example.tsx
import { Rating } from "@/components/dashral/rating"

<Rating label="Your rating" name="rating" onValueChange={(stars) => setStars(stars)} />

Using the npm package? Import from @dashral/ui/components/rating instead, here and in the examples; the rest is the same.

Examples

Average

readOnly shows a fraction, filling part of a star.

4.61,284 reviews
  • 572%
  • 418%
  • 36%
  • 22%
  • 12%

Feedback form

labels gives each value a word, shown with showLabel; icon swaps the star for a heart.

API reference

Rating

PropTypeDefault
labelReactNode

Shown above, linked to the control.

—
descriptionReactNode

A hint under the control.

—
errorboolean | ReactNode

true shows the error style; a message also shows under the control.

—
labelActionReactNode

At the end of the label's row, e.g. a “Forgot password?” link or the current value.

—
withAsteriskboolean

Marks the label with an asterisk, e.g. alongside required.

false
value / defaultValuenumber

The rating, controlled or initial; 0 for none. With readOnly, a fraction too (4.6).

0
onValueChange(value: number) => void

Called with the new value.

—
maxnumber

How many icons.

5
readOnlyboolean

Shows the value without taking input, a fraction filling part of an icon.

false
clearableboolean

Picking the current rating again clears it.

true
labelsstring[]

A word for each value, lowest first: each icon's name for screen readers.

—
showLabelboolean

With labels: the hovered or picked value's word beside the icons.

false
iconReactElement

Another icon, e.g. a heart; it's filled in the color.

a star
color"primary" | "neutral" | "info" | "success" | "warning" | "destructive" | "highlight"

The filled icons.

"warning"
size"sm" | "default" | "lg"

The icons' size.

"default"
namestring

The form field's name; sends the number, nothing at 0.

—
disabled / requiredboolean

As on other controls.

—
classNames{ root, label, description, error, item, valueLabel }

Classes for each part; valueLabel is showLabel's word.

—
classNamestring

Extra classes, merged with cn() so yours win.

—

Keyboard

KeysAction
TabTo the rating (the picked star, or the first)
←or→or↑or↓Pick the next or previous value (in reading order)
SpacePick the focused value; on the picked one, clear it

Accessibility

  • With label, description or error, the id, aria-describedby and aria-invalid are wired up for you.
  • A radio group: each icon is a radio named "3 stars" (or its label), so it's read as "3 stars, 3 of 5".
  • readOnly is one image named "4.6 out of 5".
  • Its words come from the kit's messages, so a LocaleProvider translates them.