Star Rating

Whole or half stars with hover preview, clear on re-click, and a read-only average.

Category
Forms
Access
Free with an account
Source
184 lines · one file
States
5 drawn

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 Liro's Star Rating for this.
Install it: npx shadcn@latest add @liro/star-rating
(the @liro registry is set up in components.json, with LIRO_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 @liro 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.

TypeNotes
valuenumber NoThe rating, 0 for none. Controlled.
defaultValuenumber NoThe starting rating when uncontrolled. Default 0.
onValueChange(value: number) => void NoCalled with the new rating, or 0 when cleared.
maxnumber NoHow many stars. Default 5.
precision1 | 0.5 NoWhole stars or half stars. Default 1.
readOnlyboolean NoShows the rating without letting it change.
size"sm" | "md" | "lg" Nosm 16px, md 20px, lg 28px stars. Default md.
iconReact.ReactNode NoA different shape, drawn in currentColor and filled. Default a star.
showValueboolean NoShows the number after the stars, e.g. "4.5".
countnumber NoHow many ratings it is the average of, shown as "(128)".
disabledboolean No–
namestring NoThe field's name inside a form.
idstring No–
classNamestring No–

Dependencies

npm packages

clsxlucide-reacttailwind-merge

Liro foundations, installed alongside it

liro-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