VayuUI

Rate

A customizable rating component for user feedback and reviews.

Installation

npx vayu-ui init #one time only
npx vayu-ui add rate
npx vayu-ui add -t rate #with test case needs

Usage

Rate Examples

Current value: 3

Small
Medium
Large
XLarge
Good

Rate the product quality.

3.5 / 5

Using custom icons for empty, filled, and half states

Hover on the left or right half of a star to select half values

<Rate defaultValue={3.5} allowHalf>
  <Rate.Container>
    <div>
      <Rate.Label>Service Rating</Rate.Label>
      <Rate.Description>How would you rate our service?</Rate.Description>
    </div>
    <Rate.Value showTotal />
  </Rate.Container>
  <Rate.Stars />
  <Rate.TextLabel />
</Rate>

<Rate defaultValue={4} count={5} size="lg" error>
  <Rate.Container>
    <Rate.Label>Product Quality</Rate.Label>
  </Rate.Container>
  <Rate.Stars />
  <Rate.ErrorText>This rating is required.</Rate.ErrorText>
</Rate>

Anatomy

import { Rate } from '@/components/ui/Rate';

<Rate>
  <Rate.Container>
    <Rate.Label />
    <Rate.Description />
    <Rate.Value />
  </Rate.Container>
  <Rate.Stars />
  <Rate.TextLabel />
  <Rate.ErrorText />
</Rate>;

Accessibility

  • Keyboard Support: Full keyboard navigation support. Users can use Tab to focus on the rating component, and ArrowRight / ArrowLeft or ArrowUp / ArrowDown to adjust the rating. Home and End keys set the minimum and maximum values. Space or Enter toggles the rating.
  • ARIA Attributes: The root uses role="group" with an aria-labelledby linking to the label. The input representation uses role="slider" with aria-valuemin, aria-valuemax, and aria-valuenow communicating the state.
  • Focus Behavior: Focus is managed intelligently on the root input level, showing distinct focus rings without losing screen reader context.

Screen Reader Behavior

When focused, a screen reader will announce the group label or description. As the user navigates the component via keyboard or arrow keys, the screen reader uses the slider role to immediately read out the current numeric value (e.g., "3.5 out of 5"). Descriptive text labels associated through Rate.TextLabel are also exposed to provide semantic context.

Component Folder Structure

components/ui/Rate/
├── index.ts
├── types.ts
├── hooks.ts
├── Rate.tsx
├── RateContainer.tsx
├── RateDescription.tsx
├── RateErrorText.tsx
├── RateLabel.tsx
├── RateStars.tsx
└── RateValue.tsx

Props

Rate

The main root component that handles context and state.

NameTypeDefaultDescription
countnumber5The total number of stars to display.
valuenumber-The controlled selected value.
defaultValuenumber0The default selected value for uncontrolled usage.
onChange(value: number) => void-Callback fired when the value changes.
allowHalfbooleantrueAllows half-star ratings.
allowClearbooleantrueAllows resetting the rating by clicking the active value again.
disabledbooleanfalseDisables the rating component, preventing interactions.
readOnlybooleanfalseSets component to read-only but keeps it focusable.
size"sm" | "md" | "lg" | "xl""md"The visual size of the stars.
iconReactElement<DefaultStar />Default icon for the unselected state.
filledIconReactElement-Icon for the fully selected state. Falls back to icon.
halfIconReactElement-Icon for the half-selected state. Falls back to filledIcon.
errorbooleanfalseDisplays the component in an error state.
labelsstring[]-Formatted labels mapping to each step of the rating.

Rate.Container

A layout wrapper to easily group labels, descriptions, and value displays.

NameTypeDefaultDescription
childrenReactNode-The content to render inside the container.
classNamestring-Custom CSS classes.

Rate.Label

The accessible label for the rating component.

NameTypeDefaultDescription
childrenReactNode-The label text.
classNamestring-Custom CSS classes.

Rate.Description

Supplementary text description for the rating component.

NameTypeDefaultDescription
childrenReactNode-The description text.
classNamestring-Custom CSS classes.
idstring-Custom ID for linking with aria-describedby manually if needed.

Rate.Stars

The container rendering the actual interactive star SVGs.

NameTypeDefaultDescription
classNamestring-Custom CSS classes.
aria-labelstring-Custom aria-label for the inner group of stars.

Rate.Value

Displays the active numeric value of the rating.

NameTypeDefaultDescription
showTotalbooleanfalseIf true, formats as "current / total" (e.g. 4/5).
decimalsnumber-The number of decimal places to show.
classNamestring-Custom CSS classes.

Rate.TextLabel

Displays the string label mapping to the current active value (uses labels array from root).

NameTypeDefaultDescription
classNamestring-Custom CSS classes.

Rate.ErrorText

Displays an error message, only visible when error prop is true on the root.

NameTypeDefaultDescription
childrenReactNode-The error message text.
classNamestring-Custom CSS classes.
idstring-Custom ID for ARIA mapping.

On this page