Skip to content
synccn

Rating

Star rating with hover preview and a read-only mode.

Very good

Playground

Change the props and copy the code. The controls come from the component's TypeScript types.

Props

max5
size20
import { Rating } from "@/components/ui/rating";<Rating defaultValue={3} />

Installation

$ pnpm dlx shadcn@latest add @synccn/rating

First time in this project? Connect synccn with one command.

Usage

import { Rating } from "@/components/ui/rating";<Rating defaultValue={4} />

API reference

Generated from the component's TypeScript types, so it always matches the code.

  • value
    number

    Controlled rating.

  • defaultValue
    number

    Default 0

    Initial rating when uncontrolled.

  • onValueChange
    (value: number) => void

    Called with the new rating (0 when cleared).

  • max
    number

    Default 5

    Number of stars.

  • readOnly
    boolean

    Default false

    Display only.

  • size
    number

    Default 20

    Star size in pixels.

  • className
    string

    Extra classes for the root element.

Dependencies

Read from the component's imports when the site builds. The CLI installs all of these for you.

Helpers

  • lib/utilscn (merges class names)
  • lib/hooksuseControllable (works controlled or uncontrolled)

npm packages

Keyboard

  • →or↑Add a star
  • ←or↓Remove a star

Accessibility

  • Exposed as a radiogroup, one radio per star.

From the source

Read from this component's code when the site builds, so it can't drift.

  • ARIA roles: radio, radiogroup
  • ARIA attributes: aria-checked, aria-label
  • Keys handled: ArrowUp, ArrowDown, ArrowLeft, ArrowRight
  • Shows a visible focus ring for keyboard users