Forms
Rating
Free13.3 KB gzippedAn editable star (or custom icon) rating, built on real native radios (visually hidden but focusable, sharing one name) inside role="radiogroup". Tab lands on the selected value (or the first option), arrow keys move and select, and the value submits with a form. Supports half values, clearing, hover preview, and Field/Fieldset wiring.
Accessibility: WCAG 2.2 AA · No A or AA violations in light or dark mode · Keys: Arrow keys, Backspace, Delete · Full report
Import
import { Rating, RatingDisplay } from "@vjdesigns/vjdesigns-ui";
Importing only Rating adds about 13.3 KB of minified, gzipped JavaScript to your app (React not included). The stylesheet is shared by every component and loaded once: 75.1 KB gzipped.
Examples
Basic
A radio group under the hood: arrow keys change the rating and screen readers hear "3 of 5 stars".
import { useState } from "react";
import { Rating } from "@vjdesigns/vjdesigns-ui";
export function Basic() {
const [value, setValue] = useState<number | null>(3);
return <Rating label="Rate your experience" value={value} onValueChange={setValue} showValue />;
}
Half stars
precision={0.5} allows half stars.
import { Rating } from "@vjdesigns/vjdesigns-ui";
export function HalfStars() {
return <Rating label="Difficulty" precision={0.5} defaultValue={3.5} size="lg" />;
}
Read only
RatingDisplay shows a read-only average, with an optional review count.
import { RatingDisplay, Stack } from "@vjdesigns/vjdesigns-ui";
export function ReadOnly() {
return (
<Stack gap={3}>
<RatingDisplay value={4.6} count={128} showValue />
<RatingDisplay value={3.2} count={9} size="sm" />
</Stack>
);
}
Accessibility
getLabel names each option, so a screen reader says "4 stars, good" rather than a bare number. allowClear lets people remove a rating by pressing Delete or Backspace, or selecting it again.
import { Rating } from "@vjdesigns/vjdesigns-ui";
export function Accessibility() {
const words = ["terrible", "poor", "okay", "good", "excellent"];
return (
<Rating
label="Rate your stay"
description="Your rating is shown to other guests."
defaultValue={4}
allowClear
getLabel={(value) => `${value} ${value === 1 ? "star" : "stars"}, ${words[Math.ceil(value) - 1]}`}
/>
);
}
Translated
The default name of each option ("3 étoiles") and the read-only summary come from the locale (French here).
import {
Rating,
RatingDisplay,
Stack,
LocaleProvider,
} from "@vjdesigns/vjdesigns-ui";
import fr from "@vjdesigns/vjdesigns-ui/locales/fr";
export function Translated() {
return (
<LocaleProvider locale="fr" messages={fr}>
<Stack gap={4}>
<Rating label="Notez ce produit" defaultValue={3} />
<RatingDisplay value={4.5} count={128} />
</Stack>
</LocaleProvider>
);
}
Right to left
The first example again, inside a right-to-left LocaleProvider (Arabic). Layout, spacing and arrow icons mirror, and the components' built-in text is translated. The example's own text stays in English.
Basic
import { LocaleProvider } from "@vjdesigns/vjdesigns-ui";
import ar from "@vjdesigns/vjdesigns-ui/locales/ar";
// Basic is the "Basic" example above.
<LocaleProvider locale="ar" messages={ar}>
<Basic />
</LocaleProvider>
API reference
Rating
| Prop | Type | Default |
|---|---|---|
valueSelected rating (controlled). | number | null | — |
defaultValueInitial rating (uncontrolled). | number | null | null |
onValueChangeCalled with the new rating, or | (value: number | null) => void | — |
precisionStep between values. | 10.5 | 1 |
allowZeroAdds a "0" option before the first icon, so zero is a real rating ("0 out of 5"), distinct from no rating ( | boolean | false |
allowClearLet the user remove their rating: clicking (or pressing Space on) the selected value again, or pressing Delete / Backspace, resets it to | boolean | — |
clearHintInstructions announced with each option when | string | "Press Space on the selected rating, or Delete, to clear it." |
hoverPreviewHighlight icons up to the one under the mouse, with a springy pop. | boolean | true |
disabledDisables every option. Falls back to a surrounding Field's state. | boolean | — |
readOnlyShow the value without allowing changes. Renders the | boolean | — |
invalidMarks the rating invalid: danger outlines and | boolean | — |
requiredA rating is required: sets native | boolean | — |
nameShared | string | — |
getLabelAccessible name of each option. Localize it here. | (value: number, max: number) => string | "3 out of 5" |
getReadOnlyLabelRead-only: accessible name of the rating. Localize it here. | (value: number, max: number) => string | "Rated 4.5 out of 5" |
unratedLabelRead-only with no value: accessible name, so "unrated" isn't announced as zero. | string | "Not rated" |
labelVisible label, used when the rating stands alone. Ignored inside a | ReactNode | — |
descriptionHelp text under the label, linked with | ReactNode | — |
maxNumber of icons (the highest value). | number | 5 |
sizeIcon size. | "sm""md""lg" | "md" |
toneColor of the filled icons. Uses each tone's verified | "neutral""primary""accent""success""warning""danger""info" | "warning" |
iconFilled icon (a filled shape, e.g. | ReactNode | <StarFilledIcon /> |
emptyIconEmpty icon (an outline of the same shape, e.g. | ReactNode | <StarIcon /> |
showValueShow the numeric value next to the icons, e.g. "4.5 / 5". | boolean | — |
Also accepts native <div> attributes, plus className, style and ref.
RatingDisplay
A read-only rating: a row of icons with fractional fill, exposed to assistive tech as a single role="img" named "Rated 4.5 out of 5", optionally followed by the value ("4.5 / 5") and a review count. Not focusable. For an editable rating use Rating.
| Prop | Type | Default |
|---|---|---|
valueRequiredThe rating to show. Fractions are drawn exactly: 3.7 fills 70% of the 4th icon. Clamped to 0.. | number | — |
countNumber of reviews, shown after the icons as "(128 reviews)". | number | — |
formatCountCustomizes (or localizes) the review count text. | (count: number) => ReactNode | "(128 reviews)" |
getLabelAccessible name of the icon row ( | (value: number, max: number) => string | "Rated 4.5 out of 5" |
maxNumber of icons (the highest value). | number | 5 |
sizeIcon size. | "sm""md""lg" | "md" |
toneColor of the filled icons. Uses each tone's verified | "neutral""primary""accent""success""warning""danger""info" | "warning" |
iconFilled icon (a filled shape, e.g. | ReactNode | <StarFilledIcon /> |
emptyIconEmpty icon (an outline of the same shape, e.g. | ReactNode | <StarIcon /> |
showValueShow the numeric value next to the icons, e.g. "4.5 / 5". | boolean | — |
Also accepts native <span> attributes, plus className, style and ref.
See what changed in each version in the Changelog.
Accessibility report
Rating targets WCAG 2.2 AA. Below: a live axe-core audit of this page's examples, Lighthouse scores for the page in both modes, the accessibility behavior built into the component, and the checks only a person can do.
axe-core audit (live)
Checking the examples above…
Lighthouse audit
Light mode
Score 10036 passed · 0 failed · 29 not applicable · 10 to check by hand
Dark mode
Score 10036 passed · 0 failed · 29 not applicable · 10 to check by hand
Lighthouse 13.5.0 audits the whole docs page (header and sidebar included), not just the examples, in English. Last run 11/27/2026. Some failed audits carry no weight in the score, so a page can score 100 and still list one.
Built-in behavior
Keyboard
| Key | Action |
|---|---|
| Arrow keys | Changes the rating by one star. |
| BackspaceDelete | Clears the rating, when clearable. |
Roles and states
| Element | Role | States and properties |
|---|---|---|
| Rating | radiogroup | aria-labelledby, aria-describedby |
| Star | radio | aria-label ("3 stars") |
RatingDisplay (read-only) | img | aria-label ("Rated 4 out of 5") |
Manual checks
Automated tools find roughly a third of accessibility issues. Test these in your app, with your content.
| Check | What to test | WCAG 2.2 |
|---|---|---|
| Keyboard | Every action works with Tab, Shift+Tab, Enter, Space and the arrow keys, and focus never gets stuck. | 2.1.1 (A), 2.1.2 (A) |
| Focus | Focus moves in a logical order, is always visible, and isn't hidden behind sticky headers. | 2.4.3 (A), 2.4.7 (AA), 2.4.11 (AA), 2.4.13 (AAA) |
| Screen readers | Names, roles and states are announced correctly, and status changes are announced without moving focus. | 1.3.1 (A), 4.1.2 (A), 4.1.3 (AA) |
| Color | Meaning never depends on color alone; your own colors meet contrast in both modes. | 1.4.1 (A), 1.4.3 (AA), 1.4.11 (AA), 1.4.6 (AAA) |
| Zoom and reflow | At 200% text size and at 320px wide (400% zoom), nothing is cut off and no sideways scrolling is needed. | 1.4.4 (AA), 1.4.10 (AA), 1.4.12 (AA) |
| Pointer targets | Click and tap targets are at least 24×24px (44×44px for AAA), or spaced so they don't overlap. | 2.5.8 (AA), 2.5.5 (AAA) |
| Motion | Animations respect the reduced-motion setting. | 2.3.3 (AAA) |