A component for selecting a rating value, typically represented by stars or other symbols.
Anatomy
Usage
<script> import {RatingGroup} from 'ui-ingredients'; import {StarIcon} from '$lib/icons';</script><RatingGroup.Root> <RatingGroup.Label>Label</RatingGroup.Label> <RatingGroup.HiddenInput /> {#each context.items as index} <RatingGroup.Item {index}> {#snippet children(state)} <StarIcon class={state.highlighted ? 'text-yellow-500' : 'text-gray-500'} /> {/snippet} </RatingGroup.Item> {/each}</RatingGroup.Root>
Usage with Field component
<script> import {RatingGroup, Field} from 'ui-ingredients'; import {StarIcon} from '$lib/icons';</script><Field.Root> <RatingGroup.Root> <RatingGroup.Label>Label</RatingGroup.Label> <RatingGroup.HiddenInput /> {#each context.items as index} <RatingGroup.Item {index}> {#snippet children(state)} <StarIcon class={state.highlighted ? 'text-yellow-500' : 'text-gray-500'} /> {/snippet} </RatingGroup.Item> {/each} </RatingGroup.Root> <Field.HelperText>This is a helper text</Field.HelperText> <Field.ErrorText>This is an error text</Field.ErrorText></Field.Root>
API Reference
Root
Prop
Default
Description
allowHalf
booleanWhether to allow half stars.
autoFocus
booleanWhether to autofocus the rating.
count
5
numberThe total number of ratings.
defaultValue
numberThe initial value of the rating when rendered.
Use when you don't need to control the value of the rating.
disabled
booleanWhether the rating is disabled.
form
stringThe associate form of the underlying input element.
id
stringThe unique identifier of the machine.
ids
{
root?: string;
label?: string;
hiddenInput?: string;
control?: string;
item(id: string)?: string;
}The ids of the elements in the rating. Useful for composition.
name
stringThe name attribute of the rating element (used in forms).
onHoverChange
(details: HoverChangeDetails) => voidFunction to be called when the rating value is hovered.
onValueChange
(details: ValueChangeDetails) => voidFunction to be called when the rating value changes.
readOnly
booleanWhether the rating is readonly.
required
booleanWhether the rating is required.
translations
IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states
value
numberThe controlled value of the rating
asChild
SnippetRender a different element.
Control
Prop
Default
Description
asChild
SnippetRender a different element.
Data Attribute
Value
data-disabled
Present when disabled
data-part
control
data-readonly
Present when read-only
data-scope
rating-group
HiddenInput
Prop
Default
Description
asChild
SnippetRender a different element.
Item
Prop
Default
Description
index
numberThe index of the item.
asChild
SnippetRender a different element.
Data Attribute
Value
data-checked
Present when checked
data-disabled
Present when disabled
data-half
data-highlighted
Present when highlighted
data-part
item
data-readonly
Present when read-only
data-scope
rating-group
Label
Prop
Default
Description
asChild
SnippetRender a different element.
Data Attribute
Value
data-disabled
Present when disabled
data-part
label
data-scope
rating-group
Accessibility
Keyboard Support
Key
Description
ArrowRight
Moves focus to the next star, increasing the rating value based on the `allowHalf` property.
ArrowLeft
Moves focus to the previous star, decreasing the rating value based on the `allowHalf` property.