shadcn.io is an independent project built on shadcn/ui
Components
Free
Rating for UI SDK
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 UI SDK.
On this page9 sections
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. Here it runs on UI SDK's own rating, and keeps the props of the other bases.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
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.
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 put the rating in a Field, whose label then names the group, or point the label entry of ids at your own text, as in Usage.
KeyAction
Tab
Moves focus to the star that holds the current score, or to the first star when there is none.
Arrow Right
Raises the score by one star, or by half with allowHalf, and stops at the last star.
Arrow Left
Lowers the score by one star, or by half with allowHalf, and stops at the first star.
Arrow Down
Raises the score the way ArrowRight does.
Arrow Up
Lowers the score the way ArrowLeft does.
Space
When no score is set yet, sets it to the focused star.
Enter
Submits the form the rating is in.
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 name the stars through the label entry of ids, with the score, 4.5 stars, read after them. Use it on a product or listing page, where people compare the number and the stars give its shape.
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.
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.
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.
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.
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.
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.
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 nothing until it has a score. Use this shape for a review or feedback form, and check the score on the server too.
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.
Readonly
The three stars here are readOnly, so they keep their full color and ignore clicks, hovers and keys. UI SDK turns them into one image that takes no focus, named from the aria-label and the score, "Product rating, 3 stars". 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.
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.
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 or cleared rating holds 0, and the arrow keys never go below the first step, so 0 always means 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.
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 and ignores clicks, hovers and keys. UI SDK makes it one image that takes no focus, named with the score, such as "Product rating, 3 stars", and its value is still 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?
Put it in a Field with a label, and UI SDK names the stars' group with that label. Outside a field, give your visible question an id and pass that id as the label entry of ids on Rating, as the Usage example does.
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?
Pass name to Rating, and its score is sent with the rest of the form through hidden native radios. Without a name it sends nothing, and two ratings with the same name share one key, so give each its own.
An unrated control sends nothing at all, and required blocks the submit with the browser's own message until there's a score. The Form demo checks for a score before it sends, and the server should check 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.
The arrows stop at the first and last star, and Enter submits the form. On an empty rating, Space sets the score to the focused star. The Accessibility section lists each key.
API Reference
Rating
<span>data-slot="rating"
PropType
value?number
The score. Pass it to control the rating; 0 is no score.
defaultValue?number
The score at first, when uncontrolled. A form reset returns to it.