Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Components

Free

Rating for Solid

Set a score with a row of stars, or show a score that is already filled in, half steps included. For Solid with Ark UI, when people should read the score at a glance.

Framework
UI library

Stars that record a score, or show one that is already set.

Use them when the scale is obvious and a glance is enough. Labeled choices belong in a Radio Group. In Solid, Ark UI changes the score from the arrow keys, half steps included.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Rating anatomy: root, label, control, item
Rating
├── RatingLabel
├── RatingItem
└── RatingControl

Usage

import {
  Avatar, AvatarFallback, AvatarImage,
} from "@/components/ui/avatar";
import { Button } from "@/components/ui/button";
import {
  Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle,
} from "@/components/ui/card";
import { Rating } from "@/components/ui/rating";
<Card class="w-full max-w-xs">
  <CardHeader class="flex flex-col items-center gap-3 text-center">
    <Avatar size="lg">
      <AvatarImage alt="Driver" src="https://github.com/vinihvc.png" />
      <AvatarFallback>VV</AvatarFallback>
    </Avatar>
    <div>
      <CardTitle class="text-base">Vinicius Vicentini</CardTitle>
      <CardDescription> Tuestday morning at Mercado N.89</CardDescription>
    </div>
  </CardHeader>
  <CardContent class="space-y-2 text-center">
    <p class="font-medium text-sm">Rate your driver</p>
    <Rating />
  </CardContent>
  <CardFooter>
    <Button class="w-full">Submit</Button>
  </CardFooter>
</Card>

Accessibility

Arrow Right
Moves focus to the next star, increasing the rating value based on the allowHalf property.
Arrow Left
Moves focus to the previous star, decreasing the rating value based on the allowHalf property.
Enter
Selects the focused star in the rating group.
Space
Activate the focused control.
Arrow Up
Move left.
Arrow Down
Move right.
Home
Go to the start.
End
Go to the end.

Demos

Controlled

The score is kept in state and passed back to the rating, and the line under the stars only checks whether that number is 5. Take control when the score drives something else, such as enabling submit once it is high enough. defaultValue is enough when the widget can hold the number and you only read it on submit.

Preview width

Count

count is how many icons render, and this row is three, all selected. A shorter scale is the honest one when five stars would be false precision, as with bad, fine, and good. Leave the default of five for reviews people already know how to read. The value is still a number, and it should not be asked to mean more than count.

Preview width

Custom Color

The icons paint with the current text color, so a text utility on the root recolors the empty and filled marks together. Info and success are the two shown, against the warning color the component starts with. Change it when the score means a status, or when that gold fights the page. Prefer a semantic token to a one-off palette class.

Preview width

Custom Icon

icon replaces the star. Here it is a heart, allowHalf is on, and text-destructive keeps the marks from looking like a warning. Swap the icon when stars are the wrong metaphor, a favorite rather than a quality score. The empty and filled layers both use that icon, so it still has to read when it is only an outline.

Preview width

Custom Size

There is no size prop. A class on the root targets the rating-item-indicator slot and scales it past the default star. Make the icons larger when this is the main control on a phone and the default is a fiddly tap. Shrink them beside a quote so the sentence stays the point, which the testimonial further down does.

Preview width

Disabled

disabled blocks input and fades the icons, which fits a score that would only be a guess, such as before the trip has ended. The value can still be set. It cannot be changed, including from the keyboard. A published score that should look normal and stay fixed wants readOnly instead.

Preview width

Half Star

allowHalf lets the pointer and the arrow keys stop between icons, so the value can sit halfway instead of on a whole star. Use it for an average, where that half step is a real difference. Turn it off on a personal rating if the smaller target is harder to hit than it is worth. This row starts between stars through defaultValue.

Preview width

Readonly

readOnly shows the score and ignores pointer and keyboard changes, without fading the icons. That is a published average or a product rating, a number the reader did not just choose. disabled is the treatment that looks unavailable. If the icons are the only place the number exists, write it out in text beside them.

Preview width

Testimonial

Five stars, readOnly, drawn smaller, sitting over a quote and a name. That is a marketing card rather than a form, and the smaller indicators keep the stars from outshouting the sentence. Drop readOnly when the reader should leave their own score. The opening preview is that form, and this card is only the quote.

Preview width

FAQ

How do I control the Rating value in Solid?

Keep the score in state and pass it back to Rating, or use defaultValue when the control can manage itself. The value is a number, and a half star is a decimal such as 3.5, which the Half Star demo uses. Pass value, and onValueChange receives details.value.

How do I allow half stars on a Rating?

Set allowHalf so a pointer or the arrow keys can stop between icons. Set count to change how many icons are shown. The default count is 5, and the Count demo uses three icons. Leave allowHalf unset for whole stars only.

What is the difference between a read-only and a disabled Rating?

Set readOnly to show a score that cannot be changed, as the Readonly demo does. Pointer events are ignored, and the value stays available. Set disabled to block changes and fade the icons, as the Disabled demo does. Use readOnly for a published score and disabled when the control is not available yet.

How do I change the Rating color or icon?

The icons use the current text color, and Rating starts as a warning color. Put another text color utility on Rating, as the Custom Color demo does. Pass icon to replace the star, as the Custom Icon demo does with a heart. Both the empty and filled layers use that icon.

Can I use a Rating from the keyboard in Solid?

Yes, the arrow keys move between icons and change the value. With allowHalf, they step by half an icon. Enter selects the focused icon, and Space activates it. Home and End jump to the first and last icon, which the Accessibility section also lists.

How do I submit a Rating with a form?

Set name on Rating when the score should be included in a form submit. The control renders a hidden input under that name. Set required when a score is mandatory. You can still control value in state if the form and the page both need it.

How do I install Rating in Solid?

Run the command in the Installation section above. It adds Rating to your ui folder. Import from that folder when the command finishes, instead of copying the source by hand, and rerun the command later if you want the latest files. The command uses the shadcn CLI.

API Reference

Rating

<div> data-slot="rating"

PropType
value?number

The controlled value of the rating.

defaultValue?number

The initial value of the rating when rendered. Use when you don't need to control the value of the rating.

onValueChange?(details: ValueChangeDetails) → void

Function to be called when the rating value changes.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: number }

Returns
void
idstring

The unique identifier of the machine.

allowHalf?boolean

Whether to allow half stars.

Default
false
autoFocus?boolean

Whether to autofocus the rating.

count?number

The total number of ratings.

Default
5
dir?"ltr" | "rtl"

The document's text/writing direction.

Type
"ltr" | "rtl"
Default
"ltr"
disabled?boolean

Whether the rating is disabled.

form?string

The associate form of the underlying input element.

icon?JSX.Element

The icon to use for the rating.

Default
<StarIcon />
ids?object

The ids of the elements in the rating. Useful for composition.

Type
{ root?: string; label?: string; hiddenInput?: string; control?: string; item?: (id: string) → string }
name?string

The name attribute of the rating element (used in forms).

onHoverChange?(details: HoverChangeDetails) → void

Function to be called when the rating value is hovered.

Type
(details: HoverChangeDetails) → void
Parameters

details – { hoveredValue: number }

Returns
void
readOnly?boolean

Whether the rating is readonly.

required?boolean

Whether the rating is required.

translations?object

Specifies the localized strings that identifies the accessibility elements and their states.

Type
{ ratingValueText?: (index: number) → string }
asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
class?string

Merged with the part's classes.

RatingItem

<span> data-slot="rating-item"

PropType
indexnumber

The item's position in its list.

asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
class?string

Merged with the part's classes.

AttributeType
data-checked?string

Present when checked.

data-disabled?string

Present when disabled.

data-half?string

Present when half is true.

data-highlighted?string

Present when highlighted.

data-readonly?string

Present when read-only.

RatingControl

<div> data-slot="rating-control"

PropType
asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
AttributeType
data-disabled?string

Present when disabled.

data-readonly?string

Present when read-only.

useRating

With other components

  • Field

    Inputs

    Label the score

  • Label

    Inputs

    Name what is rated

  • Tooltip

    Overlays

    The score in words

  • Card

    Layout

    A review

  • Item

    Layout

    A rating in a row

  • Button

    Buttons

    Clear the score