Skip to content
WANCRAFT

F.55Forms & Inputs

Rating Input

An accessible star rating built on native radios, with hover preview, half steps and a particle pop.

NewReactMotionTailwindintermediate

Overview

Rating Input renders one native radio per step inside role="radiogroup" — two per icon with half precision — so arrow keys, a single tab stop and form submission come straight from the platform. Each radio is named "3 of 5 stars" (plus your label, e.g. "Good"). Hovering previews the value, choosing the current value again clears it (Delete/Backspace too), and Home/End jump to the ends. A selection pops the icon on a spring and bursts a few particles; reduced motion removes both. Read-only mode renders an image with an accessible name and shows fractional averages like 4.3. Sizes, accent colour, custom icons and per-value labels are props.

01Installation

Method 01 · Registry

CLI verified

Uses the shadcn CLI in a project initialised with shadcn init. Installs the file to components/ui/rating-input.tsx and adds its dependencies.

npx shadcn@latest add https://wancraft.my/r/rating-input.json

Method 02 · Copy and paste

  1. 1. Install the dependencies below.
  2. 2. Copy the source from the Code tab into components/ui/rating-input.tsx.
  3. 3. Import it and adjust props.
npm install motion
Dependencies
motion
Peer
react ≥ 18, tailwindcss ≥ 4
Added
13 Oct 2026
Updated
—

02Usage

The default configuration. Change properties above to generate a tailored example in the Usage tab.

example.tsx
import { RatingInput } from "@/components/ui/rating-input";

export function RatingInputExample() {
  return (
    <RatingInput
      label="Your rating"
      name="rating"
      labels={["Terrible", "Poor", "Okay", "Good", "Excellent"]}
      onChange={(value) => console.log(value)}
    />
  );
}

03Props

PropTypeDefaultDescription
value / defaultValuenumber0Controlled value, or initial value when uncontrolled (0 = none).
onChange(value: number) => void—Called with the new value; 0 when cleared.
maxnumber5Number of icons (1–20).
allowHalfbooleanfalseHalf-step precision: two radios per icon.
allowClearbooleantrueRe-selecting the current value (click or Space), Delete or Backspace clears.
readOnlybooleanfalseDisplay only (role="img"); keeps fractional values.
size"sm" | "md" | "lg""md"Icon size: 20, 28 or 36px.
accentstring"#FF7448"Fill, focus outline and particle colour.
labelsstring[]—Text per whole value, shown beside the icons and added to radio names.
showValuebooleantrueShow the numeric value and label.
iconReactNode—Custom glyph SVG using fill="currentColor"; defaults to a star.
label / hideLabelstring / boolean"Rating" / falseGroup label (aria-labelledby), optionally visually hidden.
unitstring"stars"Noun in accessible names: "3 of 5 stars".
namestring—Radio name so the value submits with native forms.
disabledbooleanfalseDisables every radio.

04Features

  • 01Native radios: arrow keys, one tab stop and form submission from the platform
  • 02Hover preview, half-step precision and clear by re-selecting
  • 03Spring pop and particle burst on selection
  • 04Read-only mode with fractional fills for averages
  • 05Custom icons, sizes, accent colour and per-value labels
motion · springmotion · tweeninput · hoverinput · clickinput · keyboardinput · focus

05Accessibility

  • role="radiogroup" labelled by a visible (or visually hidden) label; each step is a real radio named "3 of 5 stars" (plus its label).
  • Arrow keys move and select natively; Home/End jump to the ends; with allowClear, Space on the checked icon, Delete or Backspace clear the rating.
  • Focus is shown with an outline around the focused icon; clearing is announced in a polite live region.
  • Read-only mode is a single role="img" with a full accessible name ("Rating: 4.3 out of 5 stars").
  • The visible value/label text is aria-hidden because the radios already convey it; prefers-reduced-motion removes the pop and particles.