> For the complete documentation index, see [llms.txt](https://docs.rierino.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rierino.com/sdk-core-ui/components/rating.md). Agents can also search and read this documentation through its [MCP server](https://docs.rierino.com/~gitbook/mcp).

# Rating

Shows or collects a rating as a row of stars.

{% code title="rating/demo.tsx" %}

```tsx
import { useState } from "react";
import { Rating, type RatingValue } from "@rierino-open/core-ui";

export default function RatingDemo() {
  const [value, setValue] = useState<RatingValue>(3);

  return (
    <div style={{ display: "grid", gap: 8, justifyItems: "center", fontSize: 12 }}>
      <Rating aria-label="Product rating" value={value} onChange={setValue} />
      <span>Value: {value ?? "none"}</span>
    </div>
  );
}
```

{% endcode %}

## Usage

{% tabs %}
{% tab title="React app" %}

```tsx
import { Rating, type RatingValue } from '@rierino-open/core-ui';

const [rating, setRating] = useState<RatingValue>(3);

<Rating aria-label="Product rating" value={rating} onChange={setRating} />
```

{% endtab %}

{% tab title="Hosted editor" %}

```jsx
import React, { useState } from 'react';
import CoreUI from '@rierino-open/core-ui';
const { Rating } = CoreUI;

const [rating, setRating] = useState(3);

<Rating aria-label="Product rating" value={rating} onChange={setRating} />
```

{% endtab %}
{% endtabs %}

`value` is the current rating, or `null` for none (the `RatingValue` type), and `onChange` receives the new one when a star is clicked. By default there are five stars worth 1 each. Clicking the current rating again clears it to `null`.

## Examples

### Half stars

`fractions` splits each star into parts. With `fractions={2}`, people can pick half stars, and values like `3.5` are shown half filled.

{% code title="rating/fractions.tsx" %}

```tsx
import { useState } from "react";
import { Rating, type RatingValue } from "@rierino-open/core-ui";

export default function RatingFractions() {
  const [value, setValue] = useState<RatingValue>(3.5);

  return (
    <div style={{ display: "grid", gap: 8, justifyItems: "center", fontSize: 12 }}>
      <Rating aria-label="Quality" value={value} onChange={setValue} fractions={2} />
      <span>Value: {value ?? "none"}</span>
    </div>
  );
}
```

{% endcode %}

### Range and step

`min`, `max` and `step` set the scale. The number of stars is `(max - min) / step`: this example shows five stars from 0 to 10, each worth 2.

{% code title="rating/range.tsx" %}

```tsx
import { useState } from "react";
import { Rating, type RatingValue } from "@rierino-open/core-ui";

export default function RatingRange() {
  const [value, setValue] = useState<RatingValue>(6);

  return (
    <div style={{ display: "grid", gap: 8, justifyItems: "center", fontSize: 12 }}>
      {/* Five stars from 0 to 10, each worth 2. */}
      <Rating aria-label="Score" value={value} onChange={setValue} min={0} max={10} step={2} />
      <span>Value: {value ?? "none"}</span>
    </div>
  );
}
```

{% endcode %}

### Read-only

`readOnly` shows a rating without letting it change, such as an average from reviews. `isDisabled` does the same.

{% code title="rating/read-only.tsx" %}

```tsx
import { Rating } from "@rierino-open/core-ui";

export default function RatingReadOnly() {
  return <Rating aria-label="Average rating, 4 out of 5" value={4} readOnly />;
}
```

{% endcode %}

### Without clearing

Set `allowClear={false}` when a rating is required, so clicking the current star keeps it instead of clearing it.

{% code title="rating/no-clear.tsx" %}

```tsx
import { useState } from "react";
import { Rating, type RatingValue } from "@rierino-open/core-ui";

export default function RatingNoClear() {
  const [value, setValue] = useState<RatingValue>(2);

  return (
    <div style={{ display: "grid", gap: 8, justifyItems: "center", fontSize: 12 }}>
      <Rating aria-label="Priority" value={value} onChange={setValue} allowClear={false} />
      <span>Value: {value ?? "none"}</span>
    </div>
  );
}
```

{% endcode %}

### Custom stars

`fullClassName` and `emptyClassName` restyle the built-in stars, for example their colour. `symbols` replaces them with your own elements.

{% code title="rating/custom.tsx" %}

```tsx
import { useState } from "react";
import { Rating, type RatingValue } from "@rierino-open/core-ui";
import { Heart } from "lucide-react";

export default function RatingCustom() {
  const [value, setValue] = useState<RatingValue>(3);

  return (
    <div style={{ display: "flex", gap: 48, alignItems: "center" }}>
      {/* Restyle the built-in stars. */}
      <Rating
        aria-label="Delivery"
        value={value}
        onChange={setValue}
        fullClassName="text-[#ff7e47]"
        emptyClassName="text-[#d3d6df]"
      />

      {/* Or replace them. */}
      <Rating
        aria-label="Favourites"
        value={value}
        onChange={setValue}
        symbols={{
          empty: <Heart size={20} color="#d3d6df" />,
          full: <Heart size={20} color="#eb475a" fill="#eb475a" />,
        }}
      />
    </div>
  );
}
```

{% endcode %}

### Vertical

{% code title="rating/vertical.tsx" %}

```tsx
import { useState } from "react";
import { Rating, type RatingValue } from "@rierino-open/core-ui";

export default function RatingVertical() {
  const [value, setValue] = useState<RatingValue>(3);

  return <Rating aria-label="Level" value={value} onChange={setValue} orientation="vertical" />;
}
```

{% endcode %}

## Accessibility

* Rating is used with a mouse or touch: its stars can't be focused or changed with the keyboard, and the current value isn't exposed to screen readers.
* When the value matters, also show it as text, as the examples do, or offer another input.
* `aria-label` defaults to "Rating". Set your own, such as "Product rating".

## Styling

The stars use `--primary`, outlined when empty and filled when full. See [theme variables](https://docs.rierino.com/sdk-core-ui/foundations/colors.md#theme-variables).

| Target                       | Prop              |
| ---------------------------- | ----------------- |
| The outer wrapper            | `className`       |
| The row (or column) of stars | `ratingClassName` |
| The space around each star   | `starClassName`   |
| Empty star icons             | `emptyClassName`  |
| Filled star icons            | `fullClassName`   |

## API reference

| Prop              | Type                                           | Default        | Description                                                                      |
| ----------------- | ---------------------------------------------- | -------------- | -------------------------------------------------------------------------------- |
| `value`           | `RatingValue`                                  |                | **Required.** The current rating, or `null` for none.                            |
| `onChange`        | `((value: RatingValue) => void)`               |                | Called with the new rating when a star is clicked, or `null` when it is cleared. |
| `min`             | `number`                                       | `0`            | The lowest value.                                                                |
| `max`             | `number`                                       | `5`            | The highest value.                                                               |
| `step`            | `number`                                       | `1`            | How much each star is worth. The number of stars is `(max - min) / step`.        |
| `fractions`       | `number`                                       | `1`            | How many parts each star can be split into. `2` allows half stars.               |
| `readOnly`        | `boolean`                                      | `false`        | Shows the rating without letting it change.                                      |
| `isDisabled`      | `boolean`                                      | `false`        | The same as `readOnly`.                                                          |
| `allowClear`      | `boolean`                                      | `true`         | Clicking the current rating again clears it to `null`.                           |
| `orientation`     | `"horizontal" \| "vertical"`                   | `"horizontal"` | Lays the stars out in a row or a column.                                         |
| `className`       | `string`                                       |                | Classes added to the outer wrapper.                                              |
| `starClassName`   | `string`                                       |                | Classes added around each star.                                                  |
| `emptyClassName`  | `string`                                       |                | Classes added to the empty star icons.                                           |
| `fullClassName`   | `string`                                       |                | Classes added to the filled star icons.                                          |
| `ratingClassName` | `string`                                       |                | Classes added to the row (or column) of stars.                                   |
| `symbols`         | `{ empty?: JSX.Element; full?: JSX.Element; }` |                | Elements to use instead of the built-in empty and filled stars.                  |
| `aria-label`      | `string`                                       | `"Rating"`     | Names the rating for screen readers.                                             |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rierino.com/sdk-core-ui/components/rating.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
