Loading component…
Loading component…
Stars or thumbs up and down for an AI answer
To reuse this local React demo, copy all files from the Code tab into the same folder, then import Rating from ./rating.
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);
}}
/>
);
}| Name | Type | Default | Description |
|---|---|---|---|
| variant | "stars" \| "thumbs" | "stars" | Stars from 1 to max, or thumbs up and down |
| value | number \| null · "up" \| "down" \| null | Stars: 1 to max, or null for none – read-only it may be a fraction, shown to the nearest half. Thumbs: "up", "down" or null | |
| defaultValue | same as value | null | The starting value when value isn't controlled |
| onValueChange | (value) => void | Called with the new value, or null when it's cleared | |
| max | number | 5 | Stars only – how many (1 to 50); more than fit wrap onto a new line |
| allowClear | boolean | true | Pressing the chosen one again, or Delete, clears it |
| readOnly | boolean | false | Only shows the value: no buttons, read out as one picture; stars show halves |
| disabled | boolean | false | Dims 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 |
| name | string | Sends the value with a form (a hidden input; empty when nothing is chosen) | |
| ...div props | ComponentProps<"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" |
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"
@import "tailwindcss"; in your main CSS file.