# Rating Group URL: https://ark-ui.com/docs/components/rating-group Source: https://raw.githubusercontent.com/chakra-ui/ark/refs/heads/main/website/src/content/pages/components/rating-group.mdx Allows users to rate items using a set of icons. --- ## Anatomy ```tsx ``` ## Examples **Example: basic** #### React ```tsx import { RatingGroup } from '@ark-ui/react/rating-group' import { StarIcon } from 'lucide-react' import styles from 'styles/rating-group.module.css' export const Basic = () => ( Label {({ items }) => items.map((item) => ( {({ highlighted }) => ( )} )) } ) ``` #### Solid ```tsx import { RatingGroup } from '@ark-ui/solid/rating-group' import { StarIcon } from 'lucide-solid' import { Index } from 'solid-js' import styles from 'styles/rating-group.module.css' export const Basic = () => ( Label {(context) => ( {(item) => ( {(itemContext) => ( )} )} )} ) ``` #### Vue ```vue ``` #### Svelte ```svelte Label {#snippet render(ratingGroup)} {#each ratingGroup().items as item} {#snippet render(itemState)} {/snippet} {/each} {/snippet} ``` ### Controlled When using the `RatingGroup` component, you can use the `value` and `onValueChange` props to control the state. **Example: controlled** #### React ```tsx import { RatingGroup } from '@ark-ui/react/rating-group' import { StarIcon } from 'lucide-react' import { useState } from 'react' import styles from 'styles/rating-group.module.css' export const Controlled = () => { const [value, setValue] = useState(0) return ( setValue(details.value)}> Label {({ items }) => items.map((item) => ( {({ half, highlighted }) => ( )} )) } ) } ``` #### Solid ```tsx import { RatingGroup } from '@ark-ui/solid/rating-group' import { StarIcon } from 'lucide-solid' import { Index, createSignal } from 'solid-js' import styles from 'styles/rating-group.module.css' export const Controlled = () => { const [value, setValue] = createSignal(0) return ( setValue(details.value)}> Label {(context) => ( {(item) => ( {(itemContext) => ( )} )} )} ) } ``` #### Vue ```vue ``` #### Svelte ```svelte Label {#snippet render(ratingGroup)} {#each ratingGroup().items as item} {#snippet render(itemState)} {/snippet} {/each} {/snippet} ``` ### Root Provider An alternative way to control the rating group is to use the `RootProvider` component and the `useRatingGroup` hook. This way you can access the state and methods from outside the component. **Example: root-provider** #### React ```tsx import { RatingGroup, useRatingGroup } from '@ark-ui/react/rating-group' import { StarIcon } from 'lucide-react' import styles from 'styles/rating-group.module.css' export const RootProvider = () => { const ratingGroup = useRatingGroup({ count: 5, defaultValue: 3 }) return (
value: {ratingGroup.value} Label {({ items }) => items.map((item) => ( {({ highlighted }) => ( )} )) }
) } ``` #### Solid ```tsx import { RatingGroup, useRatingGroup } from '@ark-ui/solid/rating-group' import { StarIcon } from 'lucide-solid' import { Index } from 'solid-js' import styles from 'styles/rating-group.module.css' export const RootProvider = () => { const ratingGroup = useRatingGroup({ defaultValue: 3 }) return (
value: {ratingGroup().value} Label {(context) => ( {(item) => ( {(itemContext) => ( )} )} )}
) } ``` #### Vue ```vue ``` #### Svelte ```svelte
value: {ratingGroup().value} Label {#snippet render(context)} {#each context().items as item} {#snippet render(itemState)} {/snippet} {/each} {/snippet}
``` ### Field The `Field` component helps manage form-related state and accessibility attributes of a rating group. It includes handling ARIA labels, helper text, and error text to ensure proper accessibility. **Example: with-field** #### React ```tsx import { Field } from '@ark-ui/react/field' import { RatingGroup } from '@ark-ui/react/rating-group' import { StarIcon } from 'lucide-react' import field from 'styles/field.module.css' import styles from 'styles/rating-group.module.css' export const WithField = () => ( Label {({ items }) => items.map((item) => ( {({ highlighted }) => ( )} )) } Additional Info Error Info ) ``` #### Solid ```tsx import { Field } from '@ark-ui/solid/field' import { RatingGroup } from '@ark-ui/solid/rating-group' import { StarIcon } from 'lucide-solid' import { Index } from 'solid-js' import field from 'styles/field.module.css' import styles from 'styles/rating-group.module.css' export const WithField = () => ( Label {(context) => ( {(item) => ( {(itemContext) => ( )} )} )} Additional Info Error Info ) ``` #### Vue ```vue ``` #### Svelte ```svelte Label {#snippet render(ratingGroup)} {#each ratingGroup().items as item} {#snippet render(itemState)} {/snippet} {/each} {/snippet} Additional Info Error Info ``` ### Half Rating Allow `0.5` value steps by setting the `allowHalf` prop to `true`. Ensure to render the correct icon if the `half` value is set in the Rating components render callback. **Example: half-star** #### React ```tsx import { RatingGroup } from '@ark-ui/react/rating-group' import { StarIcon } from 'lucide-react' import styles from 'styles/rating-group.module.css' export const HalfStar = () => ( Label {({ items }) => items.map((item) => ( {({ half, highlighted }) => ( )} )) } ) ``` #### Solid ```tsx import { RatingGroup } from '@ark-ui/solid/rating-group' import { StarIcon } from 'lucide-solid' import { Index } from 'solid-js' import styles from 'styles/rating-group.module.css' export const HalfStar = () => ( Label {(context) => ( {(item) => ( {(itemContext) => ( )} )} )} ) ``` #### Vue ```vue ``` #### Svelte ```svelte Label {#snippet render(ratingGroup)} {#each ratingGroup().items as item} {#snippet render(itemState)} {/snippet} {/each} {/snippet} ``` ### Forms To use the rating group within forms, pass the prop `name`. It will render a hidden input and ensure the value changes get propagated to the form correctly. **Example: form-usage** #### React ```tsx import { RatingGroup } from '@ark-ui/react/rating-group' import { StarIcon } from 'lucide-react' import styles from 'styles/rating-group.module.css' import button from 'styles/button.module.css' export const FormUsage = () => (
{ e.preventDefault() const formData = new FormData(e.currentTarget) alert(`Rating value: ${formData.get('review')}`) }} > Label {({ items }) => items.map((item) => ( {({ highlighted }) => ( )} )) }
) ``` #### Solid ```tsx import { RatingGroup } from '@ark-ui/solid/rating-group' import { StarIcon } from 'lucide-solid' import { Index } from 'solid-js' import styles from 'styles/rating-group.module.css' export const FormUsage = () => ( Label {(context) => ( {(item) => ( {(itemContext) => ( )} )} )} ) ``` #### Vue ```vue ``` #### Svelte ```svelte Label {#snippet render(ratingGroup)} {#each ratingGroup().items as item} {#snippet render(itemState)} {/snippet} {/each} {/snippet} ``` ### Disabled To make the rating group disabled, set the `disabled` prop to `true`. **Example: disabled** #### React ```tsx import { RatingGroup } from '@ark-ui/react/rating-group' import { StarIcon } from 'lucide-react' import styles from 'styles/rating-group.module.css' export const Disabled = () => ( Label {({ items }) => items.map((item) => ( {({ highlighted }) => ( )} )) } ) ``` #### Solid ```tsx import { RatingGroup } from '@ark-ui/solid/rating-group' import { StarIcon } from 'lucide-solid' import { Index } from 'solid-js' import styles from 'styles/rating-group.module.css' export const Disabled = () => ( Label {(context) => ( {(item) => ( {(itemContext) => ( )} )} )} ) ``` #### Vue ```vue ``` #### Svelte ```svelte Label {#snippet render(ratingGroup)} {#each ratingGroup().items as item} {#snippet render(itemState)} {/snippet} {/each} {/snippet} ``` ## API Reference ### Props **Component API Reference** #### React **Root Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `allowHalf` | `boolean` | No | Whether to allow half stars. | | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `autoFocus` | `boolean` | No | Whether to autofocus the rating. | | `count` | `number` | No | The total number of ratings. | | `defaultValue` | `number` | No | The initial value of the rating when rendered. Use when you don't need to control the value of the rating. | | `disabled` | `boolean` | No | Whether the rating is disabled. | | `form` | `string` | No | The associate form of the underlying input element. | | `id` | `string` | No | The unique identifier of the machine. | | `ids` | `Partial<{ root: string label: string hiddenInput: string control: string item: (id: string) => string }>` | No | The ids of the elements in the rating. Useful for composition. | | `name` | `string` | No | The name attribute of the rating element (used in forms). | | `onHoverChange` | `(details: HoverChangeDetails) => void` | No | Function to be called when the rating value is hovered. | | `onValueChange` | `(details: ValueChangeDetails) => void` | No | Function to be called when the rating value changes. | | `readOnly` | `boolean` | No | Whether the rating is readonly. | | `required` | `boolean` | No | Whether the rating is required. | | `translations` | `IntlTranslations` | No | Specifies the localized strings that identifies the accessibility elements and their states | | `value` | `number` | No | The controlled value of the rating | **Control Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Control Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | control | | `[data-readonly]` | Present when read-only | | `[data-disabled]` | Present when disabled | **HiddenInput Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Item Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `index` | `number` | Yes | | | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Item Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | item | | `[data-disabled]` | Present when disabled | | `[data-readonly]` | Present when read-only | | `[data-checked]` | Present when checked | | `[data-highlighted]` | Present when highlighted | | `[data-half]` | | **Label Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Label Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | label | | `[data-disabled]` | Present when disabled | | `[data-required]` | Present when required | **RootProvider Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `value` | `UseRatingGroupReturn` | Yes | | | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | #### Solid **Root Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `allowHalf` | `boolean` | No | Whether to allow half stars. | | `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `autoFocus` | `boolean` | No | Whether to autofocus the rating. | | `count` | `number` | No | The total number of ratings. | | `defaultValue` | `number` | No | The initial value of the rating when rendered. Use when you don't need to control the value of the rating. | | `disabled` | `boolean` | No | Whether the rating is disabled. | | `form` | `string` | No | The associate form of the underlying input element. | | `ids` | `Partial<{ root: string label: string hiddenInput: string control: string item: (id: string) => string }>` | No | The ids of the elements in the rating. Useful for composition. | | `name` | `string` | No | The name attribute of the rating element (used in forms). | | `onHoverChange` | `(details: HoverChangeDetails) => void` | No | Function to be called when the rating value is hovered. | | `onValueChange` | `(details: ValueChangeDetails) => void` | No | Function to be called when the rating value changes. | | `readOnly` | `boolean` | No | Whether the rating is readonly. | | `required` | `boolean` | No | Whether the rating is required. | | `translations` | `IntlTranslations` | No | Specifies the localized strings that identifies the accessibility elements and their states | | `value` | `number` | No | The controlled value of the rating | **Control Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Control Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | control | | `[data-readonly]` | Present when read-only | | `[data-disabled]` | Present when disabled | **HiddenInput Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `(props: ParentProps<'input'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Item Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `index` | `number` | Yes | | | `asChild` | `(props: ParentProps<'span'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Item Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | item | | `[data-disabled]` | Present when disabled | | `[data-readonly]` | Present when read-only | | `[data-checked]` | Present when checked | | `[data-highlighted]` | Present when highlighted | | `[data-half]` | | **Label Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `(props: ParentProps<'label'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Label Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | label | | `[data-disabled]` | Present when disabled | | `[data-required]` | Present when required | **RootProvider Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `value` | `UseRatingGroupReturn` | Yes | | | `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | #### Vue **Root Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `allowHalf` | `boolean` | No | Whether to allow half stars. | | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `autoFocus` | `boolean` | No | Whether to autofocus the rating. | | `count` | `number` | No | The total number of ratings. | | `defaultValue` | `number` | No | The initial value of the rating when rendered. Use when you don't need to control the value of the rating. | | `disabled` | `boolean` | No | Whether the rating is disabled. | | `form` | `string` | No | The associate form of the underlying input element. | | `id` | `string` | No | The unique identifier of the machine. | | `ids` | `Partial<{ root: string label: string hiddenInput: string control: string item(id: string): string }>` | No | The ids of the elements in the rating. Useful for composition. | | `modelValue` | `number` | No | The v-model value of the rating group | | `name` | `string` | No | The name attribute of the rating element (used in forms). | | `readOnly` | `boolean` | No | Whether the rating is readonly. | | `required` | `boolean` | No | Whether the rating is required. | | `translations` | `IntlTranslations` | No | Specifies the localized strings that identifies the accessibility elements and their states | **Control Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Control Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | control | | `[data-readonly]` | Present when read-only | | `[data-disabled]` | Present when disabled | **HiddenInput Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Item Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `index` | `number` | Yes | | | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Item Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | item | | `[data-disabled]` | Present when disabled | | `[data-readonly]` | Present when read-only | | `[data-checked]` | Present when checked | | `[data-highlighted]` | Present when highlighted | | `[data-half]` | | **Label Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | **Label Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | label | | `[data-disabled]` | Present when disabled | | `[data-required]` | Present when required | **RootProvider Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `value` | `RatingGroupApi` | Yes | | | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | #### Svelte **Root Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `allowHalf` | `boolean` | No | Whether to allow half stars. | | `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `autoFocus` | `boolean` | No | Whether to autofocus the rating. | | `count` | `number` | No | The total number of ratings. | | `defaultValue` | `number` | No | The initial value of the rating when rendered. Use when you don't need to control the value of the rating. | | `disabled` | `boolean` | No | Whether the rating is disabled. | | `form` | `string` | No | The associate form of the underlying input element. | | `id` | `string` | No | The unique identifier of the machine. | | `ids` | `Partial<{ root: string label: string hiddenInput: string control: string item: (id: string) => string }>` | No | The ids of the elements in the rating. Useful for composition. | | `name` | `string` | No | The name attribute of the rating element (used in forms). | | `onHoverChange` | `(details: HoverChangeDetails) => void` | No | Function to be called when the rating value is hovered. | | `onValueChange` | `(details: ValueChangeDetails) => void` | No | Function to be called when the rating value changes. | | `readOnly` | `boolean` | No | Whether the rating is readonly. | | `ref` | `Element` | No | | | `required` | `boolean` | No | Whether the rating is required. | | `translations` | `IntlTranslations` | No | Specifies the localized strings that identifies the accessibility elements and their states | | `value` | `number` | No | The controlled value of the rating | **Context Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `render` | `Snippet<[UseRatingGroupContext]>` | Yes | | **Control Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `ref` | `Element` | No | | **Control Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | control | | `[data-readonly]` | Present when read-only | | `[data-disabled]` | Present when disabled | **HiddenInput Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `Snippet<[PropsFn<'input'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `ref` | `Element` | No | | **ItemContext Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `render` | `Snippet<[UseRatingGroupItemContext]>` | Yes | | **Item Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `index` | `number` | Yes | | | `asChild` | `Snippet<[PropsFn<'span'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `ref` | `Element` | No | | **Item Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | item | | `[data-disabled]` | Present when disabled | | `[data-readonly]` | Present when read-only | | `[data-checked]` | Present when checked | | `[data-highlighted]` | Present when highlighted | | `[data-half]` | | **Label Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `Snippet<[PropsFn<'label'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `ref` | `Element` | No | | **Label Data Attributes:** | Attribute | Value | |-----------|-------| | `[data-scope]` | rating-group | | `[data-part]` | label | | `[data-disabled]` | Present when disabled | | `[data-required]` | Present when required | **RootProvider Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `value` | `UseRatingGroupReturn` | Yes | | | `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `ref` | `Element` | No | | ### Context **API:** | Property | Type | Description | |----------|------|-------------| | `setValue` | `(value: number) => void` | Sets the value of the rating group | | `clearValue` | `VoidFunction` | Clears the value of the rating group | | `hovering` | `boolean` | Whether the rating group is being hovered | | `value` | `number` | The current value of the rating group | | `hoveredValue` | `number` | The value of the currently hovered rating | | `count` | `number` | The 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) => ItemState` | Returns the state of a rating item | ## Accessibility ### Keyboard Support