Rating
Interactive star-based scoring with fractions, keyboard support, and accessible states.
Rating lets users select a score on a visual scale. It supports product reviews, satisfaction scores, quality indicators, and read-only summaries.
Basic usage
import { Rating } from '@mosmox/ui';
<Rating aria-label="Product rating" defaultValue={4} />The default scale contains five stars. Change the number of options with max.
<Rating max={10} defaultValue={8} aria-label="Score out of ten" />Controlled value
Use value and onChange when the parent owns the score. The callback receives a number or null when the value is cleared.
const [rating, setRating] = useState<number | null>(null);
<Rating value={rating} onChange={setRating} aria-label="Rate this answer" />Use defaultValue for an uncontrolled component. When allowClear is enabled, clicking the selected value again clears it.
Half values and hover preview
Set allowHalf to allow values such as 3.5. Pointer position inside a star chooses the lower or upper half. onHover can be used to show a live preview or helper text.
<Rating allowHalf defaultValue={3.5} onHover={(value) => console.log('Preview:', value)} showValue />Labels and formatting
Use label for visible context and showValue to display the current score. valueFormatter customizes the displayed text.
<Rating defaultValue={4} label="Customer satisfaction" showValue valueFormatter={(value, max) => `${value} of ${max}`} />Sizes, tones, and icons
size accepts xs (extra small), sm (small), and default. tone accepts primary, warning, success, danger, and inherit.
The filled star color is controlled by tone: warning is orange, success is green, primary uses the primary theme color, and danger is red. Use inherit with a custom class or CSS variable (--mm-rating-color) when a custom color is needed; there is no separate color prop.
<Rating size="xs" tone="primary" defaultValue={4} />
<Rating size="sm" tone="success" defaultValue={5} />
<Rating icon="◆" emptyIcon="◇" max={10} defaultValue={8} />icon and emptyIcon accept any React node, including custom SVG icons, emojis, and branded shapes.
Read-only and states
Use readOnly for a score that should be visible but not changed. disabled prevents interaction and applies muted styling. loading disables the control and shows a status spinner while a save operation is in progress.
<Rating defaultValue={4.5} allowHalf readOnly showValue />
<Rating defaultValue={2} disabled />
<Rating defaultValue={3} loading label="Saving rating" />These states are mutually readable from the rendered control: readOnly keeps the score visible without interaction, while disabled and loading disable the star buttons. A controlled value={null} or an uncontrolled defaultValue={0} displays no selected stars.
Keyboard and accessibility
The control is exposed as a radio group. Each star has an accessible position label. After focusing a star, Arrow Right/Up increases the value, Arrow Left/Down decreases it, Home sets zero, End sets the maximum, and Space selects the focused value.
Always provide a meaningful aria-label when the visible label does not describe what is being rated.
API
| Prop | Type | Default | Description |
|---|---|---|---|
max | number | 5 | Number of rating options. |
value | number | null | — | Controlled score. |
defaultValue | number | 0 | Initial uncontrolled score. |
onChange | (value: number | null) => void | — | Called when the score changes. |
onHover | (value: number | null) => void | — | Called while previewing a score. |
allowHalf | boolean | false | Enables half-step values. |
allowClear | boolean | true | Allows clicking the selected value to clear it. |
readOnly | boolean | false | Displays a non-editable score. |
disabled | boolean | false | Disables interaction. |
loading | boolean | false | Disables interaction and shows loading status. |
size | xs | sm | default | default | Visual size. |
tone | primary | warning | success | danger | inherit | warning | Icon color. |
icon | ReactNode | ★ | Filled icon. |
emptyIcon | ReactNode | ☆ | Empty icon. |
label | ReactNode | — | Visible context label. |
showValue | boolean | false | Shows formatted score. |
valueFormatter | (value, max) => ReactNode | value / max | Formats the visible score. |
aria-label | string | Rating | Accessible group label. |
className | string | — | Adds a custom CSS class to the root. |
style | CSSProperties | — | Adds inline styles to the root. |