Rating

A row of stars that takes a score, or shows one to the half when it cannot change, on Radix Radio Group, named by the Label it points at.

import { Label } from "@produkto/ui/Label";
import { Rating } from "@produkto/ui/Rating";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-2">
    <Label id="rating-preview">Label</Label>
    <Rating aria-labelledby="rating-preview" defaultValue={3} />
  </div>
);

export default Example;

Usage

import { Rating } from "@produkto/ui/Rating"
<Rating value={4.5} />

<Rating aria-label="Rating" defaultValue={3} onValueChange={setValue} />

Examples

Sizes

size sets the stars: sm 16px, base 20px (the default) or lg 24px.

import { Label } from "@produkto/ui/Label";
import { Rating, type RatingSize } from "@produkto/ui/Rating";

const variants: { size: RatingSize; label: string }[] = [
  { size: "sm", label: "Small" },
  { size: "base", label: "Base" },
  { size: "lg", label: "Large" },
];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex flex-col gap-2" key={variant.size}>
        <Label id={`rating-size-${variant.size}`}>{variant.label}</Label>
        <Rating
          aria-labelledby={`rating-size-${variant.size}`}
          defaultValue={3}
          size={variant.size}
        />
      </div>
    ))}
  </div>
);

export default Example;

Max

max sets how many stars there are, five by default, here beside ten.

import { Label } from "@produkto/ui/Label";
import { Rating } from "@produkto/ui/Rating";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex flex-col gap-2">
      <Label id="rating-max-default">Label</Label>
      <Rating aria-labelledby="rating-max-default" defaultValue={3} />
    </div>
    <div className="flex flex-col gap-2">
      <Label id="rating-max">Label</Label>
      <Rating aria-labelledby="rating-max" defaultValue={3} max={10} />
    </div>
  </div>
);

export default Example;

Read only

readOnly, or a value with no onValueChange, only shows the score, halves included, as an image named 3.5 out of 5, beside one that takes a score as a rating does by default.

import { Label } from "@produkto/ui/Label";
import { Rating } from "@produkto/ui/Rating";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex flex-col gap-2">
      <Label id="rating-editable">Label</Label>
      <Rating aria-labelledby="rating-editable" defaultValue={3.5} />
    </div>
    <div className="flex flex-col gap-2">
      <Label>Label</Label>
      <Rating defaultValue={3.5} readOnly />
    </div>
  </div>
);

export default Example;

Disabled

disabled dims the stars and takes them out of the tab order, beside a rating as it is by default.

import { Label } from "@produkto/ui/Label";
import { Rating } from "@produkto/ui/Rating";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex flex-col gap-2">
      <Label id="rating-enabled">Label</Label>
      <Rating aria-labelledby="rating-enabled" defaultValue={3} />
    </div>
    <div className="flex flex-col gap-2">
      <Label id="rating-disabled">Label</Label>
      <Rating aria-labelledby="rating-disabled" defaultValue={3} disabled />
    </div>
  </div>
);

export default Example;

Invalid

error writes what is wrong under the stars, outlines the empty ones in the destructive colour and describes the rating to screen readers, beside one without an error.

import { Label } from "@produkto/ui/Label";
import { Rating } from "@produkto/ui/Rating";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex flex-col gap-2">
      <Label id="rating-valid">Label</Label>
      <Rating aria-labelledby="rating-valid" defaultValue={3} />
    </div>
    <div className="flex flex-col gap-2">
      <Label id="rating-error">Label</Label>
      <Rating
        aria-labelledby="rating-error"
        defaultValue={3}
        error="Description"
      />
    </div>
  </div>
);

export default Example;

API Reference

Rating

RatingProps · components/Rating/Rating.types.ts
PropTypeDefault
valuenumber-
defaultValuenumber-
onValueChange(value: number) => void-
maxnumber5
readOnlybooleanfalse
sizeRatingSize"sm" | "base" | "lg""base"
errorReactNode-

It also takes the props of Radix RadioGroup.Root.