Skip to main content

Rating

Accessible star rating covering: interactive star rating, half-star mode, keyboard nav, read-only display, multiple color themes.

Live Preview

Interact with the Rating component in real time. Toggle half-star precision, switch color themes, or flip into read-only mode.

Current value: 3.5

tsx
1<Rating2  value={3.5}3  size="md"4  color="default"5  allowHalf6  showValue7  label="Rate this product"8  onChange={(value) => setValue(value)}9/>

Installation

Install the Aidash Components package using your preferred package manager.

$ pnpm add @aidash/components

Import

Import the Rating component into your file.

tsx
import { Rating } from '@/components/aidash/rating';

Basic Usage

A controlled Rating with integer precision. Click any star to commit that value.

You rated: 0 stars

tsx
1const [value, setValue] = useState(0);2 3<Rating value={value} onChange={setValue} />4<p>You rated: {value} stars</p>

Half-Star Precision

Enable half-star ratings via allowHalf. Pointer position relative to the star midpoint decides the committed value; keyboard arrows step by 0.5.

You rated: 3.5

tsx
1const [value, setValue] = useState(3.5);2 3<Rating value={value} onChange={setValue} allowHalf showValue />

Read-Only

Display-only mode with role="img" and a descriptive aria-label. Not focusable and no pointer interaction.

Product rating
Average rating
tsx
1<Rating value={4} readOnly showValue label="Product rating" />2<Rating value={2.5} readOnly allowHalf showValue label="Average rating" />

Sizes

Four sizes to fit different layout densities.

tsx
1<Rating value={4} size="sm" />2<Rating value={4} size="md" />3<Rating value={4} size="lg" />4<Rating value={4} size="xl" />

Colors

Four semantic color themes for the star fill: amber default, brand, warning, and success.

default
brand
warning
success
tsx
1<Rating value={4} color="default" />2<Rating value={4} color="brand" />3<Rating value={4} color="warning" />4<Rating value={4} color="success" />

With Value

Enable showValue to render a numeric summary next to the stars.

With Value
tsx
<Rating value={4.5} allowHalf showValue />

<Rating
  value={3.5}
  allowHalf
  showValue
  formatValue={(v, m) => `${v.toFixed(1)} out of ${m}`}
/>

Custom Max

Render any number of stars via the max prop. Keyboard nav, hover preview, and aria-valuemax all follow.

You rated: 7 / 10

Custom Max
tsx
const [value, setValue] = useState(7);

<Rating value={value} onChange={setValue} max={10} showValue />

Disabled State

Disabled ratings are dimmed, non-interactive, and skipped from the tab order.

tsx
1<Rating value={0} disabled />2<Rating value={3} disabled showValue />

Accessibility

Wired to the WAI-ARIA slider pattern when interactive, and the img pattern when read-only.

Slider vs. Image role

Interactive ratings use role="slider" and a single focusable element with roving tabindex. Read-only ratings use role="img" with a descriptive aria-label.

aria-valuenow & aria-valuetext

The slider exposes aria-valuemin="0",aria-valuemax={max},aria-valuenow, and a human-friendlyaria-valuetext (customizable via formatValue).

Keyboard Navigation

Left / Right step the value (0.5 with allowHalf, 1 otherwise),Home jumps to 0, andEnd jumps to max.

Hover Preview Never Commits

Hovering over a star renders the previewed rating visually but does not fire onChange. The value is only committed on click or keyboard interaction, matching mouse and touch input expectations.

tsx
1{/* Interactive: renders <div role="slider" aria-valuemin={0} aria-valuemax={5} aria-valuenow={value} /> */}2<Rating value={value} onChange={setValue} allowHalf label="Rate this article" />3 4{/* Read-only: renders <div role="img" aria-label="Rated 4.5 out of 5 stars" /> */}5<Rating value={4.5} readOnly allowHalf />

API Reference

Complete list of props for the Rating component.

RatingProps
PropTypeDefaultDescription
valuenumber0Controlled rating value. When omitted, the component manages its own state internally.
onChange(value: number) => voidCalled when the committed value changes via click or keyboard.
maxnumber5Maximum rating (number of stars rendered).
size'sm' | 'md' | 'lg' | 'xl''md'Visual size of each star.
color'default' | 'brand' | 'warning' | 'success''default'Fill color theme for filled and hovered stars.
allowHalfbooleanfalseEnable half-star precision from pointer geometry and keyboard steps of 0.5.
allowClearbooleanfalseClicking the currently-selected value again resets the rating to 0.
readOnlybooleanfalseDisplay-only mode: renders as role="img" without focus, pointer, or keyboard handlers.
disabledbooleanfalseDisable interaction and dim the component.
labelstringaria-label when interactive, visible label rendered before the stars when read-only.
showValuebooleanfalseRender a "3.5 / 5" summary next to the stars.
formatValue(value: number, max: number) => stringCustom formatter for the value summary and aria-valuetext.
classNamestring''Additional CSS classes applied to the outer wrapper.
Note
When allowHalf is enabled, half-star hits are decided by pointer geometry: each star's bounding box is split at its horizontal midpoint. A pointer landing on the left half commits index + 0.5, and the right half commits index + 1.

Other components that work well alongside Rating.