# Rating

Stars for giving a score, or showing one.

```tsx
import { Rating } from '@halo-ds/react';
```

## When to use it
Rating is a row of five stars. People give a score by clicking a star or using the arrow keys; read only, it shows an average next to the number and the count of reviews. Half stars show averages like 4.5, and can be turned on for picking too.

## Do
- Show the average as a number next to the stars.
- Show how many reviews the average comes from.
- Use read only for averages; people can't change them.
- Ask one clear question above stars people give, like How was your stay?

## Don't
- Use stars for a number of things, like 3 items; use a Badge or text.
- Change the number of stars between screens.
- Use colour alone; the number tells the score to everyone.
- Put two ratings people give close together without labels.

## Props
### Rating props
| Prop | Type | What it does |
| --- | --- | --- |
| `value` | `number \| undefined` |  |
| `defaultValue` | `number \| undefined` |  |
| `onValueChange` | `((value: number) => void) \| undefined` |  |
| `max` | `number \| undefined` | Number of stars. Default 5. |
| `allowHalf` | `boolean \| undefined` | Lets people pick half stars, and shows halves when read only. |
| `readOnly` | `boolean \| undefined` | Shows the score without letting people change it. Figma: Read only. |
| `disabled` | `boolean \| undefined` |  |
| `size` | `RatingSize \| undefined` | 16, 20 or 28px stars. Figma: Size. |
| `name` | `string \| undefined` | Form field name. A hidden input carries the value. |
| `aria-label` | `string \| undefined` | Screen reader name, like "Rate this course". |
| `aria-labelledby` | `string \| undefined` |  |
| `className` | `string \| undefined` |  |
| `classNames` | `Partial<Record<RatingPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<RatingPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { Rating } from '@halo-ds/react';

export default function Example() {
  return (
    <div style={{ display: 'grid', gap: 16, justifyItems: 'center' }}>
      <Rating aria-label="Rate this course" defaultValue={3} />
      <Rating aria-label="Average rating" value={4.5} allowHalf readOnly size="sm" />
    </div>
  );
}
```

### Sizes
Small, medium and large.
```tsx
import { Rating } from '@halo-ds/react';

export default function Demo() {
  return (
    <div className="demo-stack" style={{ justifyItems: 'center' }}>
      <Rating size="sm" defaultValue={4} aria-label="Small" />
      <Rating size="md" defaultValue={4} aria-label="Medium" />
      <Rating size="lg" defaultValue={4} aria-label="Large" />
    </div>
  );
}
```

### Half stars
Half stars, with the chosen score shown next to them.
```tsx
import { useState } from 'react';
import { Rating } from '@halo-ds/react';

export default function Demo() {
  const [score, setScore] = useState(3.5);
  return (
    <div className="demo-row">
      <Rating allowHalf value={score} onValueChange={setScore} size="lg" aria-label="Rate your stay" />
      <strong>{score} / 5</strong>
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/rating
