When to use
- Asking for a quick judgement: rate an order, a recipe, a support conversation.
- Showing an average next to a count, read-only, with any fraction (4.3 fills 30% of the fifth star).
- Review rows, where each review carries its own small read-only rating.
When not to use
- Yes/no or thumbs feedback: use a Like Button or two buttons.
- Precise scores (1–10, NPS): use a Segmented control or a Slider.
- Picking one of several named options: use a Radio Group.
Installation
pnpm dlx shadcn add https://eased.dev/r/rating.jsonnpx shadcn add https://eased.dev/r/rating.jsonyarn dlx shadcn add https://eased.dev/r/rating.jsonbunx --bun shadcn add https://eased.dev/r/rating.jsonThe command adds Rating, everything it builds on and Eased’s tokens to your project.
Install the dependencies
pnpm add @base-ui/react motionnpm install @base-ui/react motionyarn add @base-ui/react motionbun add @base-ui/react motionAdd the Eased pieces it builds on
Copy each item’s files the same way, or install them with the CLI:
Copy the source into your project
components/eased/rating/rating.tsxTSX "use client";import { Radio } from "@base-ui/react/radio";import { RadioGroup } from "@base-ui/react/radio-group";import { useAnimate } from "motion/react";import { useRef, useState, type CSSProperties, type PointerEvent, type ReactNode } from "react";import { bouncy, ease, snappy, stagger, useSafeReducedMotion } from "../lib/motion";export type RatingProps = { /** The rating, 0 for none (controlled). */ value?: number; /** The starting rating (uncontrolled). Default 0. */ defaultValue?: number; /** Called when someone picks a rating (or clears it, with `clearable`). */ onValueChange?: (value: number) => void; /** Called with the rating under the pointer while previewing, and `null` when it leaves. */ onHoverChange?: (value: number | null) => void; /** How many icons. Default 5. */ max?: number; /** Allow half steps: each icon has a left and a right half. Default false. */ allowHalf?: boolean; /** Choosing the current rating again clears it to 0. Default false. */ clearable?: boolean; /** * Display only: any fraction (4.3 fills 30% of the fifth star), no focus stops, read as * one image ("Rated 4.3 out of 5"). Default false. */ readOnly?: boolean; disabled?: boolean; /** 16px (`sm`, review rows), 20px (`md`, default) or 28px (`lg`, a rating prompt). */ size?: "sm" | "md" | "lg"; /** * Your own icon: an SVG that paints with `currentColor` (a heart, a flame). It is drawn twice, * an empty layer and a filled one clipped to the rating. Default: a rounded star. */ icon?: ReactNode; /** Each step's accessible name. Default `4 stars`, `4.5 stars`. */ getLabel?: (value: number) => string; /** Submits the value with a form under this name. */ name?: string; /** Accessible name for the group, e.g. "Rate this recipe". Or point `aria-labelledby` at a visible label. */ "aria-label"?: string; "aria-labelledby"?: string; /** Appended to the root's classes. Set `--rating` (fill) and `--rating-empty` here to recolour. */ className?: string; style?: CSSProperties;};const SIZES = { sm: { icon: "size-4", pad: "px-px", ring: "rounded-control-2xs" }, md: { icon: "size-5", pad: "px-0.5", ring: "rounded-control-2xs" }, lg: { icon: "size-7", pad: "px-2", ring: "rounded-control-xs" },} as const;const Star = () => ( <svg viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round" aria-hidden> <path d="M12 2.9l2.73 5.53 6.1.89-4.41 4.3 1.04 6.07L12 16.82l-5.46 2.87 1.04-6.07-4.41-4.3 6.1-.89L12 2.9z" /> </svg>);const defaultLabel = (v: number) => `${v} ${v === 1 ? "star" : "stars"}`;// Eased's focus ring, drawn on the icon around whichever of its halves has focus.const RING = "[transition:box-shadow_var(--eased-duration-focus)_var(--eased-ease-standard)] group-has-[:focus-visible]:shadow-[0_0_0_2px_var(--eased-bg),0_0_0_4px_var(--eased-ring)]";/** * A rating on Base UI's RadioGroup: one tab stop, arrow keys step through the values (halves * too, with `allowHalf`), and it submits with a form. Pointing previews the rating without * committing it; choosing one fills the icons from the left, each popping on the bouncy spring * 35ms after the one before. `readOnly` turns it into a display of any fraction. */export function Rating({ value: controlled, defaultValue = 0, onValueChange, onHoverChange, max = 5, allowHalf = false, clearable = false, readOnly = false, disabled = false, size = "md", icon, getLabel = defaultLabel, name, className = "", style, ...aria}: RatingProps) { const [own, setOwn] = useState(defaultValue); const value = controlled ?? own; const [hover, setHover] = useState<number | null>(null); const reduced = useSafeReducedMotion(); const [scope, animate] = useAnimate<HTMLDivElement>(); const icons = useRef<(HTMLSpanElement | null)[]>([]); const s = SIZES[size]; const glyph = icon ?? <Star />; const shown = hover ?? value; const preview = (next: number | null) => { setHover(next); onHoverChange?.(next); }; // The pop: icons that gain fill rise to 1.2 and settle on the bouncy spring, left to right. // A lower rating only nudges its new last icon. Interruptible; skipped under reduced motion. const pop = (from: number, to: number) => { if (reduced) return; const els = icons.current; const run = (i: number, peak: number, delay: number) => { const el = els[i]; if (!el) return; animate(el, { scale: peak }, { duration: 0.1, ease: ease.enter, delay }).then(() => animate(el, { scale: 1 }, bouncy)); }; if (to > from) { const first = Math.floor(from); for (let i = first; i < Math.ceil(to); i++) run(i, 1.2, (i - first) * stagger.item); } else if (to > 0) { const el = els[Math.ceil(to) - 1]; if (el) animate(el, { scale: [0.9, 1] }, snappy); } }; const commit = (next: number) => { if (next === value) return; pop(value, next); if (controlled === undefined) setOwn(next); onValueChange?.(next); }; const fillOf = (i: number) => Math.min(1, Math.max(0, shown - i)); const visual = (i: number) => { const fill = fillOf(i); return ( <span className={`relative flex ${s.icon} [&_svg]:size-full`}> <span className="flex size-full text-(--rating-empty,var(--eased-line))">{glyph}</span> <span className="absolute inset-0 flex text-(--rating,var(--eased-warning)) [transition:clip-path_150ms_var(--eased-ease-standard)] reduced:transition-none" style={{ clipPath: `inset(0 ${(1 - fill) * 100}% 0 0)` }} > {glyph} </span> </span> ); }; if (readOnly) { const label = aria["aria-label"] ?? `Rated ${Math.round(value * 10) / 10} out of ${max}`; return ( <span role="img" aria-label={label} className={`inline-flex shrink-0 items-center ${className}`} style={style}> {Array.from({ length: max }, (_, i) => ( <span key={i} className={`flex ${s.pad} first:ps-0 last:pe-0`}> {visual(i)} </span> ))} </span> ); } const onPointerMove = (v: number) => (e: PointerEvent) => { if (e.pointerType === "mouse" && !disabled && hover !== v) preview(v); }; return ( <RadioGroup<number> {...aria} ref={scope} value={value} onValueChange={(next) => commit(next)} name={name} disabled={disabled} onPointerLeave={() => hover !== null && preview(null)} // Keys show the real value, not whatever the resting pointer last previewed. onKeyDown={() => hover !== null && preview(null)} className={`inline-flex shrink-0 items-center select-none ${disabled ? "cursor-not-allowed opacity-50" : ""} ${className}`} style={style} > {Array.from({ length: max }, (_, i) => { const steps = allowHalf ? [i + 0.5, i + 1] : [i + 1]; return ( // The padding (not a gap) spaces the icons, so the pointer never falls between them. <span key={i} className={`group relative flex ${s.pad} ${size === "lg" ? "py-2" : "py-1"} first:ps-0 last:pe-0`}> <span ref={(el) => { icons.current[i] = el; }} className={`flex ${s.ring} ${RING}`} > {visual(i)} </span> {steps.map((v, k) => ( <Radio.Root key={v} value={v} aria-label={getLabel(v)} onPointerMove={onPointerMove(v)} onClick={() => { if (clearable && v === value) commit(0); }} className={`absolute inset-y-0 outline-none ${ steps.length === 1 ? "inset-x-0" : k === 0 ? "start-0 w-1/2" : "end-0 w-1/2" } ${disabled ? "cursor-not-allowed" : "cursor-pointer"}`} /> ))} </span> ); })} </RadioGroup> );}Keep the folder layout
The files import each other by relative path (
../lib/motion), so keep each one at the path shown, undercomponents/eased/.
Usage
import { useState } from "react";import { Rating } from "@/components/eased/rating/rating";export function RateOrder() { const [rating, setRating] = useState(0); return <Rating aria-label="Rate your order" value={rating} onValueChange={setRating} clearable />;}Examples
Read-only average
<Rating readOnly value={4.3} size="sm" />Half steps
Each star has a left and a right half; arrow keys step by 0.5.
<Rating aria-label="Rate this film" allowHalf defaultValue={3.5} name="rating" />A custom icon and colour
<Rating aria-label="How spicy?" icon={<FlameIcon />} className="[--rating:var(--eased-danger)]" max={3}/>Show a word for the rating
onHoverChange reports the previewed value, so a label can follow the pointer.
const WORDS = ["", "Not for me", "Could be better", "Good", "Really good", "Loved it"];<Rating value={v} onValueChange={setV} onHoverChange={setHover} /><span>{WORDS[Math.round(hover ?? v)]}</span>API reference
Rating
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | The rating, 0 for none (controlled). | |
defaultValue | number | 0 | The starting rating (uncontrolled). |
onValueChange | (value: number) => void | ||
onHoverChange | (value: number | null) => void | The previewed rating, null when the pointer leaves. | |
max | number | 5 | How many icons. |
allowHalf | boolean | false | Half steps. |
clearable | boolean | false | Choosing the current rating again clears it to 0. |
readOnly | boolean | false | Display any fraction; no focus stops; read as one image. |
disabled | boolean | false | |
size | "sm" | "md" | "lg" | "md" | 16, 20 or 28px icons. |
icon | ReactNode | a rounded star | An SVG that paints with currentColor. |
getLabel | (value: number) => string | `4 stars` | Each step's accessible name. |
name | string | Submits the value with a form. | |
aria-label / aria-labelledby | string | Name the group. In readOnly, aria-label replaces "Rated 4.3 out of 5". | |
className / style | On the root. Set --rating (fill) and --rating-empty to recolour. |
Keyboard
| Key | Action |
|---|---|
Tab | Moves focus to the selected step (or the first); the rating is one tab stop. |
← / → (↑ / ↓) | Selects the previous or next step: whole stars, or halves with allowHalf. |
Space | Selects the focused step; with clearable, on the current one it clears. |
Accessibility
- Interactive: Base UI renders a
radiogroupwith oneradioper step ("3 stars", "3.5 stars"), so screen readers announce the choice and its position. Name it witharia-labeloraria-labelledby. - Read-only: one
imgnamed "Rated 4.3 out of 5", so a row of stars is read once, not five times. - The focus ring is drawn around the whole icon, whichever half has focus.
- The pointer preview is mouse-only; on touch a tap commits at once.
Motion
Pointing previews the rating: fills slide to the pointer by clip-path in 150ms. Choosing a higher rating fills from the left and each icon that gained fill rises to 1.2 in 100ms and settles on the bouncy spring, 35ms after the one before; a lower rating gives its new last icon a small snappy nudge instead. Every pop is interruptible. Reduced motion: no pops, and fills change in place.
Notes for AI
Use Rating for star-style feedback and for showing averages: interactive with value/onValueChange (or name in a form), or readOnly for display with any fraction. Pair a read-only average with a count in text; use size="lg" when the rating is the main question on the screen.
Don't build a row of star buttons yourself or add your own hover state, pop or labels: the component handles preview, keyboard, focus and announcements. Don't use a read-only Rating as an input.
Related components
- Animated Like Button: a single yes for media and posts.
- Radio Group and Radio Cards: named single choices.
- Range Slider: precise numeric scores.