Skip to content
Vertical Jump Designs-uiFront-End Studio
Get Pro

v3.9 is out.

Forms

Rating

Free13.3 KB gzipped

An 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

tsx
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".

Rate your experience
tsx
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.

Difficulty
tsx
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.

(128 reviews)(9 reviews)
tsx
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.

Rate your stay
Your rating is shown to other guests.
Press Space on the selected rating, or Delete, to clear it.
tsx
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).

Notez ce produit
(128 avis)
tsx
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

Rate your experience
tsx
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

Rating props
PropTypeDefault
value

Selected rating (controlled). null means no rating. Use with onValueChange.

number | null—
defaultValue

Initial rating (uncontrolled).

number | nullnull
onValueChange

Called with the new rating, or null when cleared.

(value: number | null) => void—
precision

Step between values. 0.5 renders two radios per icon (left and right halves). Each half hit area is kept at least 24px wide, so half-precision icons sit in 48px-wide cells at every size.

10.51
allowZero

Adds a "0" option before the first icon, so zero is a real rating ("0 out of 5"), distinct from no rating (null). It is the first radio, so ArrowLeft from the lowest icon reaches it.

booleanfalse
allowClear

Let the user remove their rating: clicking (or pressing Space on) the selected value again, or pressing Delete / Backspace, resets it to null.

boolean—
clearHint

Instructions announced with each option when allowClear is on. Localize it here.

string"Press Space on the selected rating, or Delete, to clear it."
hoverPreview

Highlight icons up to the one under the mouse, with a springy pop.

booleantrue
disabled

Disables every option. Falls back to a surrounding Field's state.

boolean—
readOnly

Show the value without allowing changes. Renders the RatingDisplay look (a single role="img", not focusable) plus a hidden input so the value still submits with a form when name is set.

boolean—
invalid

Marks the rating invalid: danger outlines and aria-invalid. Falls back to a surrounding Field's state.

boolean—
required

A rating is required: sets native required on the radios. Falls back to a surrounding Field's state.

boolean—
name

Shared name for the native radios (used in form submission). Generated with useId when omitted.

string—
getLabel

Accessible name of each option. Localize it here.

(value: number, max: number) => string"3 out of 5"
getReadOnlyLabel

Read-only: accessible name of the rating. Localize it here.

(value: number, max: number) => string"Rated 4.5 out of 5"
unratedLabel

Read-only with no value: accessible name, so "unrated" isn't announced as zero.

string"Not rated"
label

Visible label, used when the rating stands alone. Ignored inside a Field/Fieldset, whose label or legend names it. Without a label, pass aria-label or aria-labelledby.

ReactNode—
description

Help text under the label, linked with aria-describedby.

ReactNode—
max

Number of icons (the highest value).

number5
size

Icon size. Rating: sm 20px, md 24px, lg 32px icons (every hit target is still at least 24x24px). RatingDisplay: sm 16px, md 20px, lg 24px. Falls back to a surrounding Field's size.

"sm""md""lg""md"
tone

Color of the filled icons. Uses each tone's verified --color-{tone}-border (3:1 on bg, surface, and raised surfaces in both themes). Empty icons are outlines in --color-border-strong, so filled vs empty differs by shape too.

"neutral""primary""accent""success""warning""danger""info""warning"
icon

Filled icon (a filled shape, e.g. <StarFilledIcon />). It is sized to the icon box. Keep it visibly different in *shape* from emptyIcon (filled vs outline), not only in color.

ReactNode<StarFilledIcon />
emptyIcon

Empty icon (an outline of the same shape, e.g. <StarIcon />).

ReactNode<StarIcon />
showValue

Show 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.

RatingDisplay props
PropTypeDefault
valueRequired

The rating to show. Fractions are drawn exactly: 3.7 fills 70% of the 4th icon. Clamped to 0..max.

number—
count

Number of reviews, shown after the icons as "(128 reviews)".

number—
formatCount

Customizes (or localizes) the review count text.

(count: number) => ReactNode"(128 reviews)"
getLabel

Accessible name of the icon row (role="img"). Localize it here. An explicit aria-label prop wins.

(value: number, max: number) => string"Rated 4.5 out of 5"
max

Number of icons (the highest value).

number5
size

Icon size. Rating: sm 20px, md 24px, lg 32px icons (every hit target is still at least 24x24px). RatingDisplay: sm 16px, md 20px, lg 24px. Falls back to a surrounding Field's size.

"sm""md""lg""md"
tone

Color of the filled icons. Uses each tone's verified --color-{tone}-border (3:1 on bg, surface, and raised surfaces in both themes). Empty icons are outlines in --color-border-strong, so filled vs empty differs by shape too.

"neutral""primary""accent""success""warning""danger""info""warning"
icon

Filled icon (a filled shape, e.g. <StarFilledIcon />). It is sized to the icon box. Keep it visibly different in *shape* from emptyIcon (filled vs outline), not only in color.

ReactNode<StarFilledIcon />
emptyIcon

Empty icon (an outline of the same shape, e.g. <StarIcon />).

ReactNode<StarIcon />
showValue

Show 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)

Running accessibility audit

Checking the examples above…

Lighthouse audit

Light mode

Score 100

36 passed · 0 failed · 29 not applicable · 10 to check by hand

Dark mode

Score 100

36 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

Keyboard
KeyAction
Arrow keysChanges the rating by one star.
BackspaceDeleteClears the rating, when clearable.

Roles and states

Roles and states
ElementRoleStates and properties
Ratingradiogrouparia-labelledby, aria-describedby
Starradioaria-label ("3 stars")
RatingDisplay (read-only)imgaria-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.

Manual accessibility checks
CheckWhat to testWCAG 2.2
KeyboardEvery 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)
FocusFocus 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 readersNames, 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)
ColorMeaning 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 reflowAt 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 targetsClick 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)
MotionAnimations respect the reduced-motion setting.2.3.3 (AAA)