Skip to content
All components
Forms

Rating

Display a star rating, or let people choose one, with the value always available as text to assistive technology.

A star rating to display, and a radio-based input to choose one.

Installation

tdpxl is not an npm dependency of your app. The @tdpxl/cli command (not published to npm yet) copies the source into your project, with the registry items and npm packages it imports.

npx @tdpxl/cli add rating

Then import it from @/components/ui/rating. New project? Start with the installation guide.

Examples

Each example is one branch of the official demo, rendered in its own frame. The code is that branch as you would write it, with imports pointing at the default install paths.

Input

Choose a whole-star rating with keyboard or pointer. The group is named by the question and the parent owns the value. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { RatingInput } from "@/components/ui/rating"

export function RatingInputExample() {
  const [score, setScore] = React.useState(0)

  return (
    <div className="flex w-full max-w-md flex-col gap-6">
      <div className="flex flex-col gap-2">
        <p id="rating-demo-label" className="text-label-sm text-strong-950">
          How was your experience?
        </p>
        <RatingInput aria-labelledby="rating-demo-label" value={score} onValueChange={setScore} />
        <p className="text-paragraph-sm text-sub-600" aria-live="polite">
          {score ? `You chose ${score} out of 5.` : "No rating yet."}
        </p>
      </div>
    </div>
  )
}

Read only

Display a rating as one image named '4 out of 5'; the stars are hidden from assistive technology. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { Rating } from "@/components/ui/rating"

export function RatingReadOnly() {
  

  return (
    <div className="flex w-full max-w-md flex-col gap-6">
      <div className="flex items-center gap-2">
        <Rating value={4} />
        <span className="text-paragraph-sm text-sub-600" aria-hidden="true">
          4.0
        </span>
      </div>
    </div>
  )
}

Custom label

Announce a fractional rating with context. getValueLabel adds the word stars; partial stars draw the fraction. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { Rating } from "@/components/ui/rating"

export function RatingCustomLabel() {
  

  return (
    <div className="flex w-full max-w-md flex-col gap-6">
      <div className="flex items-center gap-2">
        <Rating value={3.5} getValueLabel={(value, max) => `Rated ${value} out of ${max} stars`} />
        <span className="text-paragraph-sm text-sub-600">from 128 reviews</span>
      </div>
    </div>
  )
}

Sizes

Compare the small, medium and large star sizes. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { Rating } from "@/components/ui/rating"

export function RatingSizes() {
  

  return (
    <div className="flex w-full max-w-md flex-col gap-6">
      <div className="flex flex-col gap-2">
        <Rating size="sm" value={4} />
        <Rating size="md" value={4} />
        <Rating size="lg" value={4} />
      </div>
    </div>
  )
}

Disabled

Show a rating input that cannot be changed. Native disabled radios leave the Tab order and keep the chosen value visible. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { Rating, RatingInput } from "@/components/ui/rating"

export function RatingDisabled() {
  

  return (
    <div className="flex w-full max-w-md flex-col gap-6">
      <div className="flex flex-col gap-2">
        <p id="rating-demo-disabled" className="text-label-sm text-strong-950">
          Rating closed
        </p>
        <RatingInput aria-labelledby="rating-demo-disabled" defaultValue={3} disabled size="sm" />
      </div>
    </div>
  )
}

API reference

Generated from the component's TypeScript signatures. Inherited DOM and primitive props are listed separately.

Rating

Display a read-only star rating. It is one image named "4 out of 5" (or your getValueLabel text); the stars themselves are hidden from assistive technology.

Rating props
PropTypeDefaultDescription
inputMode"search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal" | undefined—Hints at the type of data that might be entered by the user while editing the element or its contents
aria-describedbystring | undefined—Identifies the element (or elements) that describes the object.
aria-invalidboolean | "false" | "true" | "grammar" | "spelling" | undefined—Indicates the entered value does not conform to the format expected by the application.
aria-labelstring | undefined—Defines a string value that labels the current element.
aria-labelledbystring | undefined—Identifies the element (or elements) that labels the current element.
value(required)number—Rating to display, from 0 to max. Fractions draw partial stars, such as 3.5.
maxnumber | undefined5Number of stars. Defaults to 5.
size"sm" | "md" | "lg" | null | undefined"md"Star size: sm 16px, md 20px, lg 24px.
getValueLabel((value: number, max: number) => string) | undefined(current, maximum) => `${current} out of ${maximum}`Text announced for the rating. Defaults to "4 out of 5"; add context such as "Rated 4 out of 5 stars" when nearby text does not.
Inherited props (13)
Rating inherited props
PropTypeDefaultDescription
classNamestring | undefined—Additional classes for this part. Preserve contrast, visible focus and responsive content when adapting styles.
refReact.Ref<HTMLDivElement> | undefined—Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref).
defaultCheckedboolean | undefined—Initial checked state for a control supporting uncontrolled selection. Inherited DOM typing does not give a layout wrapper checkbox behavior.
defaultValuestring | number | readonly string[] | undefined—Initial value for a control supporting uncontrolled state. Inherited DOM typing alone does not give a layout wrapper input behavior.
autoFocusboolean | undefined—Request automatic focus when the element mounts or the primitive opens, according to its source behavior. Choose initial focus deliberately in context.
idstring | undefined—Stable DOM identifier used by labels, descriptions or other explicit associations.
roleReact.AriaRole | undefined—Semantic role appropriate to the rendered element. Preserve required relationships and keyboard behavior.
onFocusReact.FocusEventHandler<HTMLDivElement> | undefined—Receive the native focus event. Preserve the component's existing keyboard and focus behavior.
onBlurReact.FocusEventHandler<HTMLDivElement> | undefined—Receive the native blur event when focus leaves this control.
onChangeReact.ChangeEventHandler<HTMLDivElement, Element> | undefined—Receive the native change event; read the actual control value or checked state from its event target.
onKeyDownReact.KeyboardEventHandler<HTMLDivElement> | undefined—Receive keyboard events. Preserve built-in keyboard behavior unless deliberately implementing an accessible alternative.
onClickReact.MouseEventHandler<HTMLDivElement> | undefined—Receive activation events. The consumer owns the action performed and its result.
onSelectReact.ReactEventHandler<HTMLDivElement> | undefined—Handle the selection event defined by this part's source signature. DOM text-selection events differ from menu or command activation callbacks.

RatingInput

Let people choose a whole-star rating. Built on native radio buttons, so arrow keys move and select, Tab enters and leaves the group, and the value submits with a form under name. Name the group with aria-label or aria-labelledby.

RatingInput props
PropTypeDefaultDescription
inputMode"search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal" | undefined—Hints at the type of data that might be entered by the user while editing the element or its contents
aria-describedbystring | undefined—Identifies the element (or elements) that describes the object.
aria-invalidboolean | "false" | "true" | "grammar" | "spelling" | undefined—Indicates the entered value does not conform to the format expected by the application.
aria-labelstring | undefined—Defines a string value that labels the current element.
aria-labelledbystring | undefined—Identifies the element (or elements) that labels the current element.
valuenumber | undefined—Selected rating (1 to max), or 0 for none. Pair with onValueChange when the parent owns the value.
defaultValuenumber | undefined—Initial rating for uncontrolled use. Defaults to 0 (none selected).
onValueChange((value: number) => void) | undefined—Receive the newly selected rating. A controlled parent must apply it to value.
maxnumber | undefined5Number of stars. Defaults to 5.
size"sm" | "md" | "lg" | null | undefined"md"Star size: sm 16px, md 20px, lg 24px. Every star keeps at least a 24x24 target.
namestring | undefined—Form field name for the native radios. Generated when omitted.
disabledboolean | undefined—Make every star unavailable through native disabled radios.
requiredboolean | undefined—Require a choice before the enclosing form submits.
getItemLabel((value: number, max: number) => string) | undefined(item) => `${item} ${item === 1 ? "star" : "stars"}`Accessible name of each star option. Defaults to "1 star", "2 stars" and so on.
Inherited props (11)
RatingInput inherited props
PropTypeDefaultDescription
classNamestring | undefined—Additional classes for this part. Preserve contrast, visible focus and responsive content when adapting styles.
refReact.Ref<HTMLDivElement> | undefined—Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref).
defaultCheckedboolean | undefined—Initial checked state for a control supporting uncontrolled selection. Inherited DOM typing does not give a layout wrapper checkbox behavior.
autoFocusboolean | undefined—Request automatic focus when the element mounts or the primitive opens, according to its source behavior. Choose initial focus deliberately in context.
idstring | undefined—Stable DOM identifier used by labels, descriptions or other explicit associations.
roleReact.AriaRole | undefined—Semantic role appropriate to the rendered element. Preserve required relationships and keyboard behavior.
onFocusReact.FocusEventHandler<HTMLDivElement> | undefined—Receive the native focus event. Preserve the component's existing keyboard and focus behavior.
onBlurReact.FocusEventHandler<HTMLDivElement> | undefined—Receive the native blur event when focus leaves this control.
onKeyDownReact.KeyboardEventHandler<HTMLDivElement> | undefined—Receive keyboard events. Preserve built-in keyboard behavior unless deliberately implementing an accessible alternative.
onClickReact.MouseEventHandler<HTMLDivElement> | undefined—Receive activation events. The consumer owns the action performed and its result.
onSelectReact.ReactEventHandler<HTMLDivElement> | undefined—Handle the selection event defined by this part's source signature. DOM text-selection events differ from menu or command activation callbacks.

Accessibility & usage

From registry/ui/rating.meta.json (authored guidance; human review may still be pending). Behavior is checked by Storybook tests, not certified here.

When to use

  • Show an average review score next to a product or place.
  • Ask for a quick satisfaction score in a feedback form.
  • Compare items by a rating people already understand.

When not to use

  • Precise numeric input; use an input or a slider.
  • Progress toward completion; use progress.
  • Choices with named meanings ('Poor', 'Good'); use a radio group with labels.

Anatomy

Rating
Read-only role=img with value, max, size and getValueLabel.
RatingInput
role=radiogroup of native radios with value, defaultValue, onValueChange, max, size, name, disabled, required and getItemLabel.
Star
Decorative SVG; empty stars are outlines and filled stars are solid, so state does not rely on color.

Keyboard & interaction

  • Tab enters the group on the checked star (or the first) and Tab leaves it.
  • Arrow keys move and select, following the reading direction.
  • Hovering previews the rating; leaving restores the chosen value.
  • Disabled stars are skipped by Tab and ignore pointer input.
  • The value submits with forms under name.

Responsive behavior

  • Stars keep at least a 24x24 target at every size.
  • The row does not wrap; at very narrow widths use the sm size.

Content guidelines

  • Name the group with a question: 'How was your experience?'.
  • Show the numeric value or review count next to read-only stars.
  • Use getValueLabel to add context such as 'Rated 4 out of 5 stars'.

Alternatives

  • Radio group — Choices with visible text labels.
  • Slider — A continuous value within a range.
  • Progress — Completion rather than a judgement.

Resources