Rating
NewA star rating that can be set with the pointer or the keyboard.
Preview
Installation
npx sigma-ui@latest add ratingUsage
<script setup lang="ts">
import { StarIcon } from '@lucide/vue';
import { Rating, RatingItem, RatingItemIndicator } from '@/components/ui/rating';
</script>
<template>
<Rating v-model="value">
<template #default="{ items }">
<RatingItem
v-for="item in items"
:key="item"
v-slot="{ steps }"
:item="item"
>
<RatingItemIndicator
v-for="step in steps"
:key="step"
:step="step"
>
<StarIcon />
</RatingItemIndicator>
</RatingItem>
</template>
</Rating>
</template>v-model is the selected value. length is the number of items, and step can be 1, 0.5, 0.25, or 0.1. Set clearable so choosing the current value resets it to 0.
Examples
Stars
API Reference
Rating
Props
| Prop | Type | Default |
|---|---|---|
defaultValueThe rating value when initially rendered. Use when you do not need to control the state of the rating. | number | - |
modelValueThe controlled rating value. Can be bound with | number | - |
lengthThe number of rating items to render. | number | - |
clearableWhen | boolean | - |
hoverableWhen | boolean | - |
stepThe granularity each rating item is divided into. | 1 | 0.5 | 0.25 | 0.1 | - |
disabledWhen | boolean | - |
dirThe reading direction of the combobox when applicable. <br> If omitted, inherits globally from | Direction | - |
orientationThe orientation of the component. | DataOrientation | - |
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | boolean | - |
asThe element or component this component should render as. Can be overwritten by | AsTag | Component | "div" |
nameThe name of the field. Submitted with its owning form as part of a name/value pair. | string | - |
requiredWhen | boolean | - |
loopWhen | boolean | - |
Events
| Event | Payload |
|---|---|
update:modelValueEvent handler called when the value changes | [payload: number] |
Slots
| Slot | Scope |
|---|---|
defaultRating items. Receives the current value and the item indexes. | { modelValue: number; items: number[]; } |
RatingItem
Props
| Prop | Type | Default |
|---|---|---|
itemrequiredThe 1-based index of this item within the rating (e.g. the 3rd star). | number | - |
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | boolean | - |
asThe element or component this component should render as. Can be overwritten by | AsTag | Component | "div" |
Slots
| Slot | Scope |
|---|---|
defaultIndicators for this item. Receives the step values that make up the item. | { steps: number[]; } |
RatingItemIndicator
Props
| Prop | Type | Default |
|---|---|---|
steprequiredThe step value this indicator represents. | number | - |
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | boolean | - |
asThe element or component this component should render as. Can be overwritten by | AsTag | Component | "div" |
Slots
| Slot | Scope |
|---|---|
defaultThe content of the component. | - |
Accessibility
The rating is a radio group. Each value is a radio button, and arrow keys move between values.
| Key | Description |
|---|---|
| Tab | Moves focus into the rating. |
| ArrowRight or ArrowDown | Selects the next value. |
| ArrowLeft or ArrowUp | Selects the previous value. |
| Space | Selects the focused value. |