Web · Components
Rating
Rating and showing scores with stars (or your own icon).
Live example
Preview
Code
Events
- Events appear here when you interact with the preview.
Product card: read only, fractional average and review count
Hearts, out of 10
Satisfaction: a separate face for each score (highlight=single)
Controlled: write the previewed score yourself
Puan: 3 · Preview: —
Import
Rating and showing scores with stars (or your own icon).
- Rating: By clicking, previewing by hovering with the mouse, or sliding a finger over the icons on a phone. Vertical scrolling isn't blocked; no accidental score while the page scrolls.
- Half and fractional scores: half an icon with
step={0.5}. While read-only any value like4.3is drawn at its exact ratio. - Flexible icons: heart, thumb, your own SVG with
icon/emptyIcon; face scales withicon={(i) => …}andhighlight="single". - Meaning: with
labelseach score's name becomes the tooltip, text and screen reader text. - Keyboard: ←/→ (↑/↓) step by step, digits set the score directly, Home/End, Backspace clears. Directions flip automatically in right-to-left languages.
- Form: joins
FormDatawithname,requiredworks, resets with the form. For label, description and error seeRatingField.
Examples
snapRating
functionSnaps a value to step increments and clamps it to 0 – max. Floating point error (0.1 + 0.2) is cleaned by rounding to the step's digits.
Examples
ratingFromPosition
functionProduces a value from a position inside an icon. fraction is the position 0 – 1 from the icon's start (its right side in right-to-left languages). The value always rounds up: with half steps the left half of an icon gives x.5 and the right half x+1.
Examples
ratingFill
functionHow much of icon number index is drawn filled (0 – 1).
RatingClassNames
interfaceClasses that can be passed to the inner parts of Rating.
| Name | Type | Description |
|---|---|---|
items? | string | Row wrapping the icons (the focused element). |
item? | string | Box of each icon. |
filled? | string | Filled icon layer (its color can be changed here, e.g. text-rose-500). |
empty? | string | Empty icon layer. |
value? | string | Value or label text next to the icons. |
RatingIcon
typeIcon: name, element, or a function returning an icon by the icon's index (from 0). The function is for giving each score its own icon (e.g. faces).
RatingProps
interfaceProps of the Rating component.
| Name | Type | Default | Description |
|---|---|---|---|
value? | number | Score in controlled use; 0 if there is no score. | |
defaultValue? | number | 0 | Initial score in uncontrolled use. |
onValueChange? | (value: number) => void | Called when the score changes (0 when cleared). | |
onHoverChange? | (value: number | null) => void | Score previewed while hovering with the mouse or dragging a finger; null when the preview ends. For showing "What does this score mean?" yourself. | |
max? | number | 5 | Number of icons (highest score). |
step? | number | 1 | Score step: 1 whole, 0.5 half star, 0.1 tenth. With half steps the left half of an icon gives the half score. While read-only the value isn't snapped to the step; 3.7 is drawn with 70% of the star filled. |
icon? | RatingIcon | Filled icon. If omitted, a star is drawn. Can be a name ("heart"), an element or an (index) => icon function giving each score its own icon. Outline icons are painted filled in the filled layer automatically. | |
emptyIcon? | RatingIcon | Empty icon. If omitted, a faded version of the filled icon is used. | |
highlight? | "range" | "single" | "range" | Which icons are colored? - "range": all up to the score (stars). - "single": only the selected icon; the others stay faded. For scales where each score has its own icon (😞 … 😍). The step is always 1. |
labels? | readonly ReactNode[] | Meaning of each whole score (labels[0] is 1 point): ["Bad", "Okay", "Good", "Very good", "Excellent"]. Shown as a tooltip on hovering the icon, next to it if showValue is on, and read to screen readers. | |
showValue? | boolean | ((value: number, previewing: boolean) => ReactNode) | false | Whether to write the score next to the icons. If true, writes the label if labels exist, otherwise the number (4.5); while hovering it shows the previewed score. If a function is given, the returned content is written (e.g. (v) => \${v} · 128 reviews\``). |
clearable? | boolean | true | Whether pressing the selected score again clears it (0). On the keyboard Backspace/Delete/Home and the 0 key also clear. When off, at least step is given. |
tone? | Tone | "warning"` (yellow star) | Color of filled icons. |
size? | Size | "md" | Icon size. |
readOnly? | boolean | Display only: draws a fractional score (4.3) at its exact ratio, isn't focusable, and is read to screen readers as "4.3 / 5". For product cards and reviews. | |
disabled? | boolean | Disabled. | |
name? | string | Form field name; the score is submitted under this name (empty if no score). | |
form? | string | Id of the form the field belongs to (if outside the form). | |
required? | boolean | The form can't be submitted without a score. | |
id? | string | Id of the focused element. RatingField passes it itself. | |
autoFocus? | boolean | Focuses when the page opens. | |
tooltip? | TooltipProp | Tooltip shown when hovering the component. See TooltipProp. | |
className? | string | Classes added to the container. | |
classNames? | RatingClassNames | Classes added to the inner parts. | |
effect? | EffectProp | Effect or effects of each icon (e.g. "pulse", "bounce"). If omitted, UIProvider appearance.defaults.rating.effect. See BuiltinEffect. | |
aria-label? | string | Screen reader name if there is no visible label (e.g. "Product rating"). | |
aria-labelledby? | string | Id of the element naming the rating. | |
aria-describedby? | string | Id of the element describing the rating. RatingField passes it itself. | |
aria-invalid? | boolean | "true" | "false" | Show the field as invalid. RatingField passes it itself. |