Built on Vercel’s UI SDK 0.1.0-beta.1. Its API may change.
Rating
A star rating on Vercel's UI SDK with whole or half stars, a hover preview, read-only averages and form support.
- Form components
- 2 parts
- 10 examples
@tabler/icons-react@ui-sdk/core@ui-sdk/reactclass-variance-authority
pnpm dlx shadcn@latest add https://hextaui.com/r/ui-sdk-rating.jsonAdds the component, the HextaUI theme tokens and any HextaUI components it depends on.
Add the theme tokens to your global CSS, if you haven’t yet.
Install the dependencies.
pnpm add @ui-sdk/react@0.1.0-beta.1 @ui-sdk/core@0.1.0-beta.1 @tabler/icons-react class-variance-authority cnCopy and paste the following code into your project.
components/ui/ui-sdk-rating.tsx Update the import paths to match your project setup.
UI SDK adds a little CSS of its own in a cascade layer named ui-sdk. Declare that layer at the top of your global stylesheet, above the Tailwind import, so your classes win over it.
Rating renders its child once per star, so put one RatingItem inside it. Each star draws an empty star with a filled one clipped to --rating-fill, so half stars and averages like 4.3 show exactly.
With step={0.5}, each half of a star is its own radio, and the arrow keys move by half a star.
A read-only rating is one image named by getValueLabel, and it shows any score, like an average.
size is sm, default or lg.
With deselectable, pressing the checked star again clears the score.
Pass value and onValueChange, where null means no score yet. A controller from createRatingController() sets the score from anywhere.
With a name, the score submits with the form. required blocks submission until there’s a score, with the browser’s own message.
Pass your own icon as the child of RatingItem and switch it with data-filled. tone sets the color.
A disabled rating can’t be focused or changed, and submits nothing.
Stars run from the right, and a half star fills from its right edge.
| Key | Action |
|---|---|
| Tab | Moves into the rating, onto the checked star or the first one. |
| ←→↑↓ | Moves to the next or previous score and checks it, stopping at the ends unless loop is set. |
| Space | Checks the focused star, or clears it when it’s checked and the rating is deselectable. |
| Enter | Submits the form. |
A rating is a radio group with one tab stop. Each star holds one radio per score it can pick, named by getValueLabel, so name scores in words people use, like “Great, 5 of 5”. The star icons are hidden from screen readers. A read-only rating is a single image instead.
Stars only grow on hover and shrink on press when motion is allowed. On touch screens each star gets extra padding, and in forced colors mode filled stars use the text color and empty ones use the system’s gray.
Each part wraps the UI SDK part of the same name and accepts all of its props, including render and a className that can be a function of the part’s state.
| Prop | Type | Default |
|---|---|---|
value | number | null | – |
defaultValue | number | null | – |
onValueChange | (value: number | null, details) => void | – |
max | number | 5 |
step | number | 1 |
size | "sm" | "default" | "lg" | "default" |
tone | "default" | "neutral" | "destructive" | "default" |
deselectable | boolean | false |
readOnly | boolean | false |
disabled | boolean | false |
required | boolean | false |
name | string | – |
loop | boolean | false |
getValueLabelNames each score for its radio, and a read-only rating. | (value: number, max: number) => string | – |
controllerFrom createRatingController(). | RatingController | – |
| Attribute | Description |
|---|---|
data-slot="rating" | Target the rating in CSS. |
data-size | The size: sm, default or lg. |
data-tone | The tone: default, neutral or destructive. |
data-readonly | Present when read-only. |
data-disabled | Present when disabled. |
--rating-color | The color of filled stars, set by tone. |
| Prop | Type | Default |
|---|---|---|
childrenThe star’s icon. Defaults to a Tabler star that fills by --rating-fill. | ReactNode | – |
render | ReactElement | (props, state) => ReactElement | – |
| Attribute | Description |
|---|---|
data-slot="rating-item" | Target a star in CSS. |
data-filled | Present on stars the shown score covers whole. |
data-partial | Present on the star the shown score covers part of. |
data-checked | Present on the star the score ends in. |
data-highlighted | Present on the stars up to the score a pointer previews. |
--rating-fill | How much of the star the shown score covers, from 0 to 1. |
- CalendarA date grid for single, range and multiple selection, with sliding months, range previews and touch-sized days.
- CheckboxA checkbox whose check draws in, with indeterminate parents, groups and labels that share its hover.
- ComboboxA filterable select with chips, groups and async results, in a popup that resizes as you type.
- Date pickerA button that opens a calendar in a popover, or a bottom sheet on phones, for single dates and ranges.
- FieldLabels, descriptions and errors wired to their control, with validation states and layouts for forms.
- InputA text input with three sizes, invalid and read-only states, native validation styling and a 16px touch font so phones never zoom in.