

# Rating

Star (or custom icon) rating. An input rating is a real radio group: `max` native `<input type="radio">` elements share a `name`, the inputs are visually hidden and their labels draw the icons. Keyboard, form submission, `required` and `disabled` are the browser's own. Use `readOnly` to display a score, including fractions such as `3.5`.

## Examples

### Default

Seed the rating with `defaultValue` (`0` means no rating). Hovering previews the rating up to the hovered star with CSS only.

```tsx
import { Rating } from '@var-ui/react';

export default function Preview() {
  return <Rating label="Quality" defaultValue={3} />;
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (read-only display: see the Rating Astro component) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Controlled

Drive the rating with `value` and `onValueChange`. `clearable` lets a click on the current value reset it to `0`.

```tsx
import { Rating } from '@var-ui/react';
import { useState } from 'react';

export default function Preview() {
  const [value, setValue] = useState(4);

  return (
    <div style={{ display: 'grid', gap: '0.5rem' }}>
      <Rating label="Your rating" value={value} onValueChange={setValue} clearable />
      <span>{value === 0 ? 'No rating' : `${value} of 5`}</span>
    </div>
  );
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (read-only display: see the Rating Astro component) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Read-only with a fractional value

`readOnly` renders a non-interactive `role="img"` with an accessible name such as "3.5 out of 5". Fractions are only supported here: the next star is clipped at the fraction through the recipe's `fill` custom property.

```tsx
import { Rating } from '@var-ui/react';

export default function Preview() {
  return (
    <div style={{ display: 'grid', gap: '0.75rem' }}>
      <Rating readOnly value={3.5} label="Average rating" />
      <Rating readOnly value={4.2} tone="warning" size="sm" aria-label="Reviews" />
    </div>
  );
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (read-only display: see the Rating Astro component) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Custom icon

`icon` and `emptyIcon` take any node, or a semantic icon name from the `IconProvider` registry (the default is `star`).

```tsx
import { Rating } from '@var-ui/react';

const heart = (
  <svg viewBox="0 0 24 24" stroke="currentColor" strokeWidth="2" aria-hidden="true">
    <path d="M12 21s-8-5.2-8-11a4.5 4.5 0 0 1 8-2.8A4.5 4.5 0 0 1 20 10c0 5.8-8 11-8 11z" />
  </svg>
);

export default function Preview() {
  return <Rating label="Favourite" defaultValue={2} max={5} icon={heart} tone="danger" />;
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (read-only display: see the Rating Astro component) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Sizes

`size` is the shared control size (`sm`, `md`, `lg`) and falls back to the surrounding control size. `tone` colors the filled icons.

```tsx
import { Rating } from '@var-ui/react';

export default function Preview() {
  return (
    <div style={{ display: 'grid', gap: '1rem' }}>
      <Rating size="sm" defaultValue={2} aria-label="Small" />
      <Rating size="md" defaultValue={3} aria-label="Medium" />
      <Rating size="lg" defaultValue={4} aria-label="Large" />
    </div>
  );
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (read-only display: see the Rating Astro component) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### In a form

With a `name` the checked number is posted like any radio group. `required` blocks submission until a star is picked. Without a `name` the rating still works as a group but posts nothing.

```tsx
import { Button, Rating } from '@var-ui/react';
import { useState } from 'react';

export default function Preview() {
  const [posted, setPosted] = useState<string>('');

  return (
    <form
      onSubmit={(event) => {
        event.preventDefault();
        setPosted(String(new FormData(event.currentTarget).get('quality') ?? ''));
      }}
      style={{ display: 'grid', gap: '0.75rem' }}
    >
      <Rating
        name="quality"
        label="Quality"
        description="Required before you can submit."
        required
      />
      <Button type="submit">Submit</Button>
      {posted ? <span>Posted quality={posted}</span> : null}
    </form>
  );
}
```

```astro
<!-- No Astro binding for this demo — use @var-ui/react (read-only display: see the Rating Astro component) -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

## Props

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| aria-describedby | string \| undefined | no |  |  |
| aria-label | string \| undefined | no |  |  |
| aria-labelledby | string \| undefined | no |  |  |
| className | string \| undefined | no |  | Classes for the root element. |
| clearable | boolean \| undefined | no |  | Clicking (or pressing Space on) the current value clears the rating to `0`. |
| defaultValue | number \| undefined | no | 0 | Initial rating when uncontrolled. |
| description | ReactNode | no |  | Help text under the rating. |
| disabled | boolean \| undefined | no |  | Disables every item and excludes the rating from form submission. |
| emptyIcon | ReactNode | no | the `icon` | Empty glyph, same forms as `icon`. |
| errorMessage | ReactNode | no |  | Validation message under the rating; implies `invalid`. |
| getItemLabel | ((value: number, max: number) => string) \| undefined | no | (n, max) => `${n} of ${max}` | Accessible name of each item. |
| getValueLabel | ((value: number, max: number) => string) \| undefined | no | (value, max) => `${value} out of ${max}` | Accessible name of the read-only display. |
| icon | ReactNode | no | 'star' | Filled glyph: a semantic icon name from the `IconProvider` registry or any node. |
| id | string \| undefined | no |  | `id` of the `radiogroup` (or the read-only `img`). |
| invalid | boolean \| undefined | no |  | Shows the error state. Defaults to `Boolean(errorMessage)`. |
| label | ReactNode | no |  | Visible label above the rating; names the `radiogroup`. |
| max | number \| undefined | no | 5 | Number of items. |
| onChange | ((event: ChangeEvent<HTMLInputElement, Element>) => void) \| undefined | no |  | Native change event of the radio that became checked (`event.target.value` is its number). It does not fire when `clearable` clears the rating: use `onValueChange(0)` for that. |
| onValueChange | ((value: number) => void) \| undefined | no |  | Called with the new rating (`0` when cleared) after a pick. |
| readOnly | boolean \| undefined | no |  | Display only: no radios, not focusable. Renders `role="img"` with an `aria-label` such as "3 out of 5". With a `name` the value is still submitted through a hidden input. |
| ref | Ref<HTMLDivElement \| HTMLInputElement> \| undefined | no |  | Ref to the first radio `<input>`, or to the `role="img"` group when `readOnly`. |
| required | boolean \| undefined | no |  | Requires a pick for form validation. |
| size | "lg" \| "md" \| "sm" \| undefined | no | "md" | Item density. Falls back to the surrounding control size, then `md`. |
| style | CSSProperties \| undefined | no |  | Inline style for the root element. |
| tone | "accent" \| "danger" \| "info" \| "neutral" \| "success" \| "warning" \| undefined | no | 'accent' | Color of the filled items. |
| value | number \| undefined | no |  | Controlled rating, `0` = none. Fractions (e.g. `3.5`) are shown only when `readOnly`; otherwise the value is rounded. |

## Accessibility

The input rating is a `role="radiogroup"` of native radios, so assistive technology announces each star as "n of max" (override with `getItemLabel`). Name the group with `label` or `aria-label`. `description` and `errorMessage` are wired into `aria-describedby`; `errorMessage` implies `aria-invalid`. The focus ring appears on the star of the focused radio.

Filled and empty icons differ by fill, not only color, so they stay distinguishable in forced-colors mode. Transitions follow the reduced-motion tokens.

The read-only display is not focusable: it is a single image named by `getValueLabel` (default "3 out of 5") and keeps its visible `label`. With a `name` it still posts its value through a hidden input. Astro has a `Rating` for this read-only display (`value`, `max`, `size`, `tone`, `label`).

## Keyboard

| Key                        | Behavior                                                                   |
| -------------------------- | -------------------------------------------------------------------------- |
| `Tab` / `Shift+Tab`        | Enters and leaves the group once, landing on the checked star (or first).  |
| `ArrowRight` / `ArrowDown` | Selects the next star (the one visually after it; mirrored in RTL).        |
| `ArrowLeft` / `ArrowUp`    | Selects the previous star.                                                 |
| `Space`                    | Selects the focused star; with `clearable`, clears it when already chosen. |


> Also accepts all standard HTML attributes (ARIA attributes, event handlers, styles, …). Run `var-ui component rating --all-props` for the complete list.
