# chatty-rating

API chuẩn của element chatty-rating, sinh từ custom-elements.json của prototype trong guide.

## Key rules

- `chatty-rating` khai báo 7 attribute, 0 property không có attribute, 2 event, 0 slot, 2 part và 4 CSS custom property.
- Import 1 module `@avada/chatty-ui/elements/rating.js` là define `chatty-rating`; chunk chỉ import element nó dùng. Prototype chuẩn:  trong guide.
- Đổi tên attribute, event, slot, part hay CSS custom property là đổi API: diff của custom-elements.json phải có trong 1 MR.

## Overview

A single-choice star rating: a radio group with one tab stop, or a read-only image. Form-associated; the form value is the chosen number. Class `ChattyRating`.

### Attributes

| Name | Property | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `value` | `number` | `0` | Chosen number; read-only also takes a fraction, drawn to the nearest half star. |
| `max` | `max` | `number` | `5` | Number of stars. |
| `name` | `name` | `string` | `''` | Form field name. |
| `label` | `label` | `string` | `''` | Group name; in read-only mode the image's accessible name. |
| `item-label` | `itemLabel` | `string` | `'Rate {rating} out of {max}'` | Star name; `{rating}` and `{max}` are replaced. |
| `disabled` | `disabled` | `boolean` | `false` | Blocks choosing; stars stay visible and are skipped by Tab. |
| `readonly` | `readonly` | `boolean` | `false` | Shows the value as one image (role img) instead of a radio group. |

### Properties

Attributes also expose the properties listed in the Property column above.

Not declared in the manifest.

### Events

| Name | Detail type | Description |
| --- | --- | --- |
| `change` | `Event` | The value changed by click or keyboard. |
| `click` | `MouseEvent` | Native activation of a star. |

### Slots

Not declared in the manifest.

### Parts

| Name | Description |
| --- | --- |
| `base` | Radio group, or the image when read-only. |
| `star` | Each star button or read-only star. |

### CSS custom properties

| Name | Default | Description |
| --- | --- | --- |
| `--chatty-rating-size` | – | Star glyph size. |
| `--chatty-rating-hit` | – | Star hit target size. |
| `--chatty-rating-color` | – | Filled star colour. |
| `--chatty-rating-muted` | – | Empty star outline colour. |

## Usage

```js
import '@avada/chatty-ui/tokens/tokens.css';
import '@avada/chatty-ui/elements/rating.js';
```

```html
<chatty-rating label="Rate your experience"></chatty-rating>
<chatty-rating label="Rate your experience" value="4"></chatty-rating>
```

## Live preview

Component thật dùng cùng story với trang component; dữ liệu mẫu nằm trong frame cục bộ.

Example (Standard) — Default: trạng thái mặc định và nội dung mẫu của element.

```html
<chatty-rating label="Rate your experience"></chatty-rating>
<chatty-rating label="Rate your experience" value="4"></chatty-rating>
```

Example (Standard) — Các variant hoặc state chính đặt cạnh nhau trong cùng ví dụ.

```html
<chatty-rating label="Rate your experience" value="3" disabled></chatty-rating>
<chatty-rating readonly value="5" label="Submitted rating 5 out of 5"></chatty-rating>
<chatty-rating
  readonly
  value="4"
  label="Rated 4 out of 5"
  style="--chatty-rating-size: 16px"
></chatty-rating>
```

## Anatomy

Số trong hình khớp bảng bên dưới. Phần chỉ hiện ở state khác được ghi Not shown; ví dụ không thêm phần tử giả.

Example (Standard) — Anatomy: một instance mặc định riêng, có khoảng trống quanh các phần được đánh số.

```html
<chatty-rating label="Rate your experience"></chatty-rating>
<chatty-rating label="Rate your experience" value="4"></chatty-rating>
```

| Part | Element | Source |
| --- | --- | --- |
| 1 | CSS part `base` | chatty-ui/custom-elements.json |
| 2 | CSS part `star` | chatty-ui/custom-elements.json |

## Keyboard

| Key | Action |
| --- | --- |
| Arrow keys | Change the rating when editable. |
| Enter / Space | Choose the focused rating. |

### Source

- chatty-ui/custom-elements.json
