affectUI › Components › Inputs

Rating

Stars or thumbs up and down for an AI answer – hover previews, a press sets it and a second press clears it, arrows in one tab stop, read-only with halves ("4.5 out of 5").

Free and open source (MIT). Install: npx shadcn@latest add https://www.affectui.com/r/rating.json · Import from @/components/ui/rating

When to use

  • Thumbs up or down under an AI answer – variant="thumbs"
  • Rating a run, an agent, a template or a support reply from 1 to 5 stars (or any max)
  • Showing an average next to a name or in a list – readOnly, with halves ("4.5 out of 5")

When not to use

  • Thumbs together with copy, retry and share under a chat message – use Message Actions, which has them and a feedback row
  • A scale with named steps (Low, Medium, High) – use the Segmented Control or the Toggle Group
  • Turning something on or off – use the Switch or the Checkbox

Usage

import { useState } from "react";
import { Rating } from "@/components/ui/rating";

export function RateRun({ onRate }: { onRate: (stars: number | null) => void }) {
  const [stars, setStars] = useState<number | null>(null);

  return (
    <Rating
      aria-label="Rate this run"
      value={stars}
      onValueChange={(v) => {
        setStars(v);
        onRate(v);
      }}
    />
  );
}

Props

NameTypeDefaultDescription
variant"stars" | "thumbs""stars"Stars from 1 to max, or thumbs up and down
valuenumber | null · "up" | "down" | nullStars: 1 to max, or null for none – read-only it may be a fraction, shown to the nearest half. Thumbs: "up", "down" or null
defaultValuesame as valuenullThe starting value when value isn't controlled
onValueChange(value) => voidCalled with the new value, or null when it's cleared
maxnumber5Stars only – how many (1 to 50); more than fit wrap onto a new line
allowClearbooleantruePressing the chosen one again, or Delete, clears it
readOnlybooleanfalseOnly shows the value: no buttons, read out as one picture; stars show halves
disabledbooleanfalseDims it and turns the buttons off
size"sm" | "md" | "lg""md"Stars 16, 20 or 26px; thumbs 28, 32 or 36px buttons – the hit areas grow on touch screens
getValueLabel(value: number, max: number) => string"3 out of 5"Stars only – how a star and a read-only value are read out; translate it here
labels{ up?: string; down?: string }"Good response", "Bad response"Thumbs only – the buttons' names and tooltips
namestringSends the value with a form (a hidden input; empty when nothing is chosen)
...div propsComponentProps<"div">aria-label, aria-labelledby, className, id, ref … go to the group. Without a name of your own it is "Rating" or "Rate this response"

Keyboard

  • Tab: One tab stop: lands on the chosen star (or the first) or thumb
  • → / ↑: One star more – in right-to-left pages ← is more
  • ← / ↓: One star less (never below 1 – clear with Delete)
  • Home / End: One star, or all of them
  • Arrows: Thumbs: switches between up and down
  • Space / Enter: Chooses the focused one; on the chosen one, clears it
  • Delete: Clears it (Backspace too)

Accessibility

  • A radio group: each star is a radio named "3 out of 5" (getValueLabel), each thumb "Good response" or "Bad response" – the chosen one is announced as checked
  • Read-only it is one role="img" named "4.5 out of 5" (or "No rating"), so a list of averages isn't a list of buttons
  • The arrows move and choose at once, like native radios; one tab stop for the whole group
  • Hovering previews only with a mouse – on touch a tap sets the value right away
  • The ring shows only under keyboard focus (:focus-visible), never after a click

Responsive

  • Halves are drawn by clipping a filled star over an empty one, from the start side – mirrored in right-to-left pages
  • Touch screens get a hit area of at least 32px per star and 40px per thumb
  • Many stars wrap instead of running out of their row
  • Dark mode built in: switches when an ancestor (usually <html>) has the class dark or data-theme="dark"

Requirements

  • React 19

Related

  • Message Actions – The quiet bar under an AI message – copy turns into a check, regenerate spins and unfolds a version switcher, thumbs pop and ask why, read aloud becomes a tiny equalizer
  • Segmented Control – Switch between two to six options – a raised pill or underline that glides, stretches a little and can be dragged across, with icons, counts and badges
  • Toggle Group – A row of toggle buttons – several on at once or one that can be cleared, labels or icons, a tray or separate chips, one tab stop with arrow keys
  • AI Answer – Streamed answer with source cards, citation chips that preview their source, copy, regenerate, feedback and follow-ups