Search docs

Rating Group

A group of star or icon items for selecting a numeric rating.

Rating Group follows the Rating Radio Group WAI-ARIA design pattern.

import { component$, useStyles$ } from '@builder.io/qwik';
import { RatingGroup } from '@leve-ui/rating-group';
import { LuStar } from '@qwikest/icons/lucide';
import styles from './default.css?inline';

export default component$(() => {
  useStyles$(styles);

  return (
    <RatingGroup.Root aria-label="Product rating" defaultValue={3} class="rating-group-default">
      {[1, 2, 3, 4, 5].map((index) => (
        <RatingGroup.Item key={index} index={index} class="rating-group-default-item">
          <LuStar aria-hidden="true" class="rating-group-default-icon" />
        </RatingGroup.Item>
      ))}
    </RatingGroup.Root>
  );
});

Installation#

Install the individual package, or use @leve-ui/core to get every component at once:

pnpm add @leve-ui/rating-group

Anatomy#

import { RatingGroup } from '@leve-ui/rating-group'; // or '@leve-ui/core'

<RatingGroup.Root>
  <RatingGroup.Item index={1} />
  <RatingGroup.HiddenInput />
</RatingGroup.Root>
PartElementDescription
RatingGroup.Root<div>The group, with role="radiogroup". Holds the committed and hovered rating values.
RatingGroup.Item<span>One rating unit (such as a star), with role="radio".
RatingGroup.HiddenInput<input type="text">Optional. Sends the committed value with a native <form>.

Examples#

Controlled#

bind:value controls the rating with a signal, and allowClear lets the user click the active value again to clear it back to 0.

Rating: 2 / 5

Half Ratings#

allowHalf enables 0.5 increments via pointer position and arrow keys.

3.5 stars

Read Only#

readOnly keeps items focusable for assistive technology while preventing changes.

Disabled#

disabled prevents focus and interaction on all items.

With Field#

Inside a Field, the group receives aria-labelledby, aria-describedby, aria-invalid, aria-required, aria-disabled, and aria-readonly.

Rate your satisfaction from 1 to 5 stars.

With Form#

Add RatingGroup.HiddenInput with name to participate in native <form> submission, required validation, and reset.

API Reference#

Root#

PropDefaultType
ids–{ root?: string }

Overrides the element IDs of the parts. Inside a Field, the Field's IDs are used instead.

count5number

The total number of rating items.

defaultValue0number

The initial rating value (uncontrolled).

bind:value–Signal<number>

A signal with the current rating value (controlled).

onValueChange$–QRL<(value: number) => void>

Called when the committed rating value changes.

onHoverChange$–QRL<(value: number | null) => void>

Called when the hovered rating preview value changes, or null when pointer leaves.

allowHalffalseboolean

Allows selecting half-star increments (e.g. 2.5).

allowClearfalseboolean

Allows clearing the rating back to 0 by clicking the currently selected value.

disabledfalseboolean

Disables all rating items and prevents interaction.

readOnlyfalseboolean

Makes the rating group read-only while keeping items focusable.

requiredfalseboolean

Marks the rating group as required for form submission.

invalidfalseboolean

Marks the rating group as invalid.

Also accepts all <div> attributes.

Data attributeValue
data-leve-rating-groupAlways present
data-disabledPresent when disabled
data-readonlyPresent when read-only
data-invalidPresent when invalid

Item#

PropDefaultType
index (required)–number

1-based index of this rating item (1..count).

Also accepts all <span> attributes.

Data attributeValue
data-leve-rating-group-itemAlways present
data-state"checked" | "half" | "unchecked" (reflects hover preview or committed value)
data-checkedPresent when this item is the committed radio selection
data-highlightedPresent when the item is filled or half-filled by the display value
data-disabledPresent when disabled
data-readonlyPresent when read-only

HiddenInput#

PropDefaultType
form–string

The id of the form element to associate the input with.

Also accepts all <input> attributes.

Data attributeValue
data-leve-rating-group-hidden-inputAlways present
data-state"checked" when value > 0, otherwise "unchecked"
data-disabledPresent when disabled

The hidden input is positioned absolutely with an inline style (adr/0026), so give RatingGroup.Root position: relative when using it.

Accessibility#

Complies with the Rating Radio Group WAI-ARIA design pattern.

  • Name the group with aria-label or aria-labelledby on RatingGroup.Root, or place it inside a Field.
  • RatingGroup.Root renders role="radiogroup" along with aria-required, aria-disabled, aria-readonly, aria-invalid, and aria-describedby when applicable.
  • Each RatingGroup.Item renders role="radio", aria-checked (reflecting only the committed value, never hover preview), and a default aria-label (1 star, 2 stars, 2.5 stars, etc.) that can be overridden.
  • Only one item is in the tab sequence (Math.ceil(value) when value > 0, or item 1 when value === 0), or none when disabled. readOnly keeps items focusable.

Keyboard Interactions#

KeyDescription
TabMoves focus into or out of the rating group.
ArrowRight / ArrowUpIncreases the rating by 1 (or 0.5 when allowHalf is true). In RTL, ArrowRight decreases.
ArrowLeft / ArrowDownDecreases the rating by 1 (or 0.5 when allowHalf is true). In RTL, ArrowLeft increases.
HomeSets the rating to the lowest value (0.5 when allowHalf is true, otherwise 1).
EndSets the rating to count.
SpaceSelects the focused item's value.