OfferLifetime Pro for $99

Star Rating

A real radio group under the stars, so arrows, Tab and screen readers work as for any radio group. Hover previews, a second press clears, half stars with precision 0.5, and a read-only average with its count.

FormsFree184 lines · one file

The decision inside it

Read-only is one sentence

A shown rating is one image named "Rated 4.5 out of 5, 1,284 ratings", not five stars a screen reader has to count.

When to use it

Use Star Rating to collect a score out of five, or show one readOnly with a count. precision={0.5} allows half stars. For a yes/no reaction use Reaction Bar.

Installation

How you get it

Paste this into Claude Code, Cursor, Codex or any coding agent. It installs the file and uses it as it is, instead of writing a new one.

Use Layro's Star Rating for this.
Install it: npx shadcn@latest add @layro/star-rating
(the @layro registry is set up in components.json, with LAYRO_KEY in .env.local)
Import it from @/components/ui/star-rating.
Do not rewrite or restyle it. Pass my data through its props
and keep its built-in states.

For the full brief with every prop and the mistakes assistants usually make, use Copy prompt above. Free components install through the @layro registry with your CLI token: see the Namespaced tab.

Usage

components/review-score.tsx
import { StarRating } from "@/components/ui/star-rating";

export function ReviewScore({ score, setScore }: { score: number; setScore: (n: number) => void }) {
  return (
    <div className="flex flex-col gap-3">
      <StarRating aria-label="Your rating" value={score} onValueChange={setScore} />
      <StarRating readOnly value={4.5} precision={0.5} showValue count={128} size="sm" />
    </div>
  );
}

API reference

“Copy prompt” above puts a full brief on your clipboard — install command, dependencies, props, and the mistakes an assistant usually makes with this component.

Props

Read from the component’s own TypeScript interface when the site is built, so this table cannot drift from the file you install.

PropTypeNotes
valuenumberThe rating, 0 for none. Controlled.
defaultValuenumberThe starting rating when uncontrolled. Default 0.
onValueChange(value: number) => voidCalled with the new rating, or 0 when cleared.
maxnumberHow many stars. Default 5.
precision1 | 0.5Whole stars or half stars. Default 1.
readOnlybooleanShows the rating without letting it change.
size"sm" | "md" | "lg"sm 16px, md 20px, lg 28px stars. Default md.
iconReact.ReactNodeA different shape, drawn in currentColor and filled. Default a star.
showValuebooleanShows the number after the stars, e.g. "4.5".
countnumberHow many ratings it is the average of, shown as "(128)".
disabledboolean—
namestringThe field's name inside a form.
idstring—
classNamestring—

Dependencies

npm packages

clsxlucide-reacttailwind-merge

Layro foundations, installed alongside it

layro-themefield

States

All of them are drawn by the component. None is left for you to design.

  • Whole stars
  • Half stars
  • Hover preview
  • Read-only
  • Custom icon

Accessibility

  • Keyboard reachable throughout, in visual order
  • Focus trapped in overlays, Escape closes them
  • Focus returns to whatever opened it
  • Live regions announce what changed, not that something did
  • Honours prefers-reduced-motion

Responsive behavior

  • Sizes to its container, not the viewport
  • Usable from 320px up
  • Pointer-based interaction, so trackpad and touch both work
  • No hover-only affordances

Lifetime access

Every component, and everything we ship next.

  • All 232 components, source included
  • Every template, and everything released after you buy
  • shadcn CLI or download — React 18/19, Tailwind v4
  • One payment. No subscription.