Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Rating for React

A row of stars that takes a score or shows one, with half steps, any icon in place of the star and a read-only mode. For React apps on Base UI.

On this page9 sections

Base UI does not include this primitive, so this page uses the Ark UI component. The visuals match the Ark UI version.

A rating is a row of stars that takes a score, or shows one someone already gave. Half steps, a different icon, more or fewer stars and a read-only display are each one prop away.

When each step needs a word, such as poor, fair and good, use a Radio Group, and for a score from 0 to 100 a Slider. This component runs on Ark UI's rating group on every base, so its props are Ark UI's.

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

Usage

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

Rating draws its own stars from count, so there are no item parts to write. The label entry of ids points the stars at the visible question, which becomes their name for a screen reader.

<div className="flex flex-col items-center gap-2">
  <p className="font-medium text-sm" id="driver-rating">
    Rate your driver
  </p>
  <Rating ids={{ label: "driver-rating" }} name="driverRating" />
</div>

Accessibility

The stars sit in a radiogroup, and each star is a radio named by its number, "1 star" for the first and "3 stars" for the third, with aria-checked on the star that holds the score. Only that star is in the tab order, or the first star while there is no score, so Tab reaches the rating once and the arrow keys change the score. The component renders no label, so the group has no name until you give it one: pass aria-label to Rating, which sets it on the group, or point the label entry of ids at your own visible text, as in Usage.

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.
Tab
Moves focus to the star that holds the current score, or to the first star when there is none.
Space
When no score is set yet, sets it to the focused star.
Arrow Up
Lowers the score the way ArrowLeft does, by one star, or by half with allowHalf.
Arrow Down
Raises the score the way ArrowRight does, by one star, or by half with allowHalf.
Home
Sets the score to one star.
End
Sets the score to the last star, the full count.

Demos

Average score

The raw average, 4.3, goes to a read-only Rating as value, and the stars draw it at the nearest half, four and a half. Beside them, the exact number and the review count also name the group, through the label entry of ids. Use it on a product or listing page, where people compare the number and the stars give its shape.

Preview width

Controlled

The score lives in the parent's state, starting at 0, and the line under the stars reads it back as "4 of 5 stars" or "Not rated yet". Clear sets the state to 0, which empties the stars. Each change arrives as an object whose value is the new number. Hold the score yourself when it drives something else, such as a submit button that waits for a rating.

Preview width

Count

Here count is 3, down from the default of five, and defaultValue fills all three stars. Use a shorter scale when five steps would pretend to a precision the answer doesn't have, as with bad, fine and good. Clicks and keys never take the score past count.

Preview width

Custom color

Two ratings sit at four stars, one with text-info on Rating and one with text-success. The outline and the fill both take the current text color, so one class recolors the row, replacing the amber warning color it ships with. Pick a semantic color when the score stands for a status, and keep the amber for ordinary reviews.

Preview width

Custom icon

Hearts replace the stars here, with allowHalf on and text-destructive for red. The component draws the icon twice, as an outline and as a filled copy clipped to the score, so choose an icon that still reads as an outline. Use another shape when stars are the wrong metaphor, such as a favorite or a mood.

Preview width

Custom size

There is no size prop. A class on Rating targets the rating-item-indicator slot and sets it to size-8, up from the default size-6. Make the stars larger when the rating is the main control on a phone and a small star is hard to hit, and smaller beside text, as in the Testimonial demo.

Preview width

Disabled

disabled dims the three filled stars and turns them gray. Clicks and keys change nothing, and none of the stars can take focus. Use it when a score can't be given yet, such as before the trip has ended. For a final score that should look normal, use readOnly instead.

Preview width

Form

A question and five stars sit in a form, with name set to stay, and Send reads the form's data the way a server would get it. Sending before a star is picked shows "Pick a score first", because the control submits -1 until it has a score. Use this shape for a review or feedback form, and check the score on the server too.

Preview width

Half star

With allowHalf on and defaultValue set to 3.5, the fourth star is half filled. A pointer over the left half of a star picks the half step, and each arrow key moves by half a star. Use half steps for averages and reviews where people expect them, and whole stars when a smaller target only makes the choice harder.

Preview width

Readonly

The three stars here are readOnly, so they keep their full color and ignore clicks, hovers and keys. The current star still takes focus, with a visible ring, so a screen reader user can tab to it and hear the score. Use it for a published average or a product's score, and write the number in text beside the stars when they are the only place it appears.

Preview width

Testimonial

A card puts five read-only stars, drawn at size-4, above a quote and the author's avatar, name and role. The smaller stars keep the score from shouting over the sentence. Use this layout for social proof on a landing page, where the stars report a score rather than ask for one.

Preview width

FAQ

How do I control the Rating value in React?

Keep the score in state, pass it as value on Rating and update it in onValueChange. The callback receives an object, and the new score is details.value.

The score is a number, and a half star is a decimal such as 3.5, so with allowHalf even 0.5 is a real score. An empty rating holds -1, and the left arrow key can bring a score down to 0, so treat 0 or less as no score. When nothing else needs the number while people choose, set defaultValue and read it on submit.

How do I allow half stars or change the number of stars in a Rating?

Set allowHalf on Rating. A pointer over the left half of a star then picks the half step, each arrow key moves by half a star, and the score can be a value such as 3.5, as in the Half star demo. Turning allowHalf off rounds the current score to a whole star.

Set count for the number of stars, which is 5 by default, as the three-star Count demo does. Clicks and keys never take the score past count, so the scale and the value always agree.

How do I show an average rating such as 4.3 stars?

Pass it as value with allowHalf and readOnly. The stars draw only whole and half steps, so Rating shows any other decimal at the nearest half, 4.3 as four and a half stars and 4.2 as four, as in the Average score demo.

Write the exact average and the number of reviews in text beside the stars, such as 4.3 from 120 reviews, and point the label entry of ids at that text. That is the part people compare, and the stars alone can't say it.

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

A read-only rating shows its score in full color, ignores clicks, hovers and keys, and keeps the current star focusable so a screen reader user can reach it. Its group reports aria-readonly, and its value is sent with a form.

A disabled rating dims and grays its stars, takes no focus at all, and leaves its value out of a form submit. Use readOnly for a published score, as in the Readonly demo, and disabled for one that can't be given yet.

How do I give a Rating an accessible label?

Give your visible question an id and pass that id as the label entry of ids on Rating, as the Usage example does. This base's Field isn't Ark UI's, so a FieldLabel doesn't reach the stars.

With no visible question, pass aria-label to Rating, which sets it on the stars' group rather than the outer wrapper. Each star is read as its number, "1 star" or "4 stars", so pass translations with a ratingValueText function to change that wording or translate it.

How do I change the Rating color or use hearts instead of stars?

The stars take the current text color, which starts as the theme's warning amber, so a text color class on Rating recolors them, as in Custom color.

Pass an icon element to icon, as the Custom icon demo does with HeartIcon.

The component renders that icon twice, an outline behind and a filled copy clipped to the score, so it has to read as an outline on its own.

How do I submit a Rating with a form?

Rating writes its score to a hidden input, named rating unless you pass name, so it is sent with the rest of the form. Give each rating on a page its own name, or they all arrive under the same key.

An unrated control sends -1, and required doesn't stop that, since the field isn't empty. Check for a score above 0 before you accept the form, as the Form demo does, and on the server too. Resetting the form puts the rating back to its defaultValue.

Can I use a Rating from the keyboard?

Yes. Tab lands on the star that holds the score, or the first star when there is none. ArrowRight and ArrowDown raise the score and ArrowLeft and ArrowUp lower it, by one star or by half with allowHalf.

Home sets the score to one star and End to the full count. On an empty rating, Space sets the score to the focused star. The Accessibility section lists each key.

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?React.ReactNode

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?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

className?string

Merged with the part's classes.

RatingItem

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

PropType
indexnumber

The item's position in its list.

asChild?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

className?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?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

AttributeType
data-disabled?string

Present when disabled.

data-readonly?string

Present when read-only.

useRating