Rating Group
Allows users to rate items using a set of icons.
Anatomy
<RatingGroup.Root>
<RatingGroup.Label />
<RatingGroup.Control>
<RatingGroup.Item>
<RatingGroup.ItemContext />
</RatingGroup.Item>
<RatingGroup.HiddenInput />
</RatingGroup.Control>
</RatingGroup.Root>
Examples
Controlled
Use value and onValueChange to control the value.
Root Provider
Use RootProvider with the useRatingGroup hook to access the state and methods from outside the component.
Field
Field wires up the label, helper text, error text and ARIA attributes.
Half Rating
Set allowHalf to true to allow 0.5 steps. Render a half icon when half is set in the item's render callback.
Forms
Pass name to render a hidden input that submits the value with the form.
Disabled
Set disabled to true to disable the rating group.
API Reference
Props
Root
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
allowHalf | booleanWhether to allow half stars. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
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 | ElementIdsThe 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. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
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 |
| Attribute | Description |
|---|---|
[data-rating-group-root] | <uid> |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
Control
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<RatingGroupControlState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-rating-group-control] | <uid> |
[data-readonly] | Present when read-only |
[data-disabled] | Present when disabled |
HiddenInput
Defines the <input> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Item
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
index | number | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<RatingGroupItemState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-rating-group-item] | <uid> |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
[data-checked] | Present when checked |
[data-highlighted] | Present when highlighted |
[data-half] |
Label
Defines the <label> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-rating-group-label] | <uid> |
[data-disabled] | Present when disabled |
[data-required] | Present when required |
RootProvider
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
value | UseRatingGroupReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Context
API
| Property | Type |
|---|---|
setValue | (value: number) => voidSets the value of the rating group |
clearValue | VoidFunctionClears the value of the rating group |
hovering | booleanWhether the rating group is being hovered |
value | numberThe current value of the rating group |
hoveredValue | numberThe value of the currently hovered rating |
count | numberThe total number of ratings |
items | number[]The array of rating values. Returns an array of numbers from 1 to the max value. |
getItemState | (props: ItemProps) => ItemStateReturns the state of a rating item |
getRootState | () => RootStateReturns the state of the root. The label, control and hidden input share it. |
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. |
Enter | Selects the focused star in the rating group. |