Skip to content

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

Components

Free

Rating for Vue 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 Vue 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.

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

<script setup lang="ts">
import { Rating } from "@/components/ui/rating";
</script>

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 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.

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 aria-labelledby, 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.

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 hands over the new number first. 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 nothing 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. 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.

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

Bind a ref with v-model on Rating, which reads and writes the score, or pass :value and update it from @value-change.

The callback receives the new score first and the change's details second.

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. Without it, each star holds one radio, and clicks and keys move by whole stars.

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 a VNode of the icon to icon, such as h(HeartIcon) for hearts.

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. Two-way with v-model.

defaultValue?number

The score at first, when uncontrolled. A form reset returns to it.

allowHalf?boolean

Lets a press on the first half of a star pick a half score.

Default
false
autoFocus?boolean

Moves focus to the rating once it mounts.

count?number

How many stars there are.

Default
5
debug?boolean

Logs every state change with its reason to the console, in development builds.

deselectable?boolean

Lets a press on the checked star clear the score.

disabled?boolean

Disables every star: they can't be focused or changed, and the score isn't submitted.

form?string

The id of a form to belong to, when the rating isn't inside that form.

icon?VNode

The icon to use for the rating.

Default
<StarIcon />
ids?{ root?: string; label?: string }

label is the id of an element that names the rating.

Type
{ root?: string; label?: string }
loop?boolean

Whether arrow keys wrap from the last star to the first.

Default
false
name?string

The name the score submits under. Without one, the rating submits nothing.

readOnly?boolean

Shows the score as an image, named by getValueLabel, that can't be changed. Any score shows, like an average of 4.2.

required?boolean

Blocks form submission until there's a score, with the browser's own message.

translations?{ ratingValueText?: (index: number) → string }

Names each star's score.

Type
{ ratingValueText?: (index: number) → string }
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

EventType
@value-change(details: RatingValueChangeDetails) → void

Called when the value changes.

Type
(details: RatingValueChangeDetails) → void
Parameters

details – { event?: Event; reason: RatingValueChangeReason; source: ChangeSource; value: number }

Returns
void
AttributeType
data-disabled?string

Present when the rating is disabled, by its own prop or a <fieldset>.

data-readonly?string

Present when the rating only shows its score.

data-required?string

Present when the rating is required.

RatingItem

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

PropType
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

AttributeType
data-checked?string

Present on the star the score ends in.

data-disabled?string

Present when the rating is disabled, by its own prop or a <fieldset>.

data-filled?string

Present on stars the shown score covers whole.

data-highlighted?string

Present on the stars up to the score a pointer previews.

data-partial?string

Present on the star the shown score covers part of, like the last of 2.5.

data-readonly?string

Present when the rating only shows its score.

CSS variableType
--rating-fillstring

How much of the star the shown score covers, from 0 to 1.

useRating