# Skeleton / loading

Giữ chỗ đúng hình dạng nội dung cuối khi chờ dữ liệu; chỉ Classic dùng Skeleton của kit.

## Key rules

- Dùng `Skeleton` khi biết hình dạng cuối; truyền `width` và `height` bằng kích thước cuối để không đổi bố cục.
- Animation mặc định là `pulse`; `animation={false}` cho bản tĩnh, `wave` thực chất là bounce.
- Component không tự đặt aria-hidden, vùng status hay xử lý reduced motion: caller phải thêm.
- AI Mode có class CSS riêng và admin dùng skeleton của Polaris; chỉ Classic import `Skeleton` của kit (xem Use instead).
- Element `chatty-skeleton` ở mục Standard là chuẩn chatty-ui port theo; các mục theo surface mô tả component React đang chạy.

## Use when / Avoid

Dùng khi biết kích thước nội dung cuối, ví dụ lưới GIF đang tải. Không dùng thay empty state hay chỉ báo AI đang xử lý. Surface khác dùng owner riêng của surface đó.

### Use instead

| Component | Use when |
| --- | --- |
| `Skeleton` | Classic: bone giữ chỗ cho lưới GIF khi loading. |
| `chatty-skeleton` | AI Mode: class CSS cho bone, không import `Skeleton`. |
| `SkeletonBodyText` | Admin · Knowledge files: dùng Polaris, không dùng `Skeleton`. |

## Live preview

Example (Standard) — Element chatty-skeleton: circle, text, rect và bản tĩnh; bone aria-hidden.

```html
<chatty-skeleton shape="circle"></chatty-skeleton>
<chatty-skeleton shape="text" style="flex-basis:100%;max-inline-size:240px"></chatty-skeleton>
<chatty-skeleton shape="text" style="flex-basis:100%;max-inline-size:144px"></chatty-skeleton>
<chatty-skeleton style="flex-basis:100%;max-inline-size:240px;--chatty-skeleton-height:96px"></chatty-skeleton>
<chatty-skeleton animation="none" style="flex-basis:100%;max-inline-size:240px;--chatty-skeleton-height:24px"></chatty-skeleton>
```

Example (Current (React), being replaced) — Skeleton circular, text và rectangular static dùng props thật.

```jsx
import React from 'react';
import {Skeleton} from '@avada/chatty-ui/components/skeleton.js';
export function Shapes() {
  return <div aria-busy="true">
    <span role="status" className="sr-only">Loading content</span>
    <Skeleton variant="circular" width={24} height={24} aria-hidden="true" />
    <Skeleton variant="text" width="100%" height={20} aria-hidden="true" />
    <Skeleton variant="rectangular" width="100%" height={80} animation={false} aria-hidden="true" />
    <p>Avatar, text and static card</p>
  </div>;
}
```

## Standard

Example (Standard) — Element chatty-skeleton: circle, text, rect và bản tĩnh; bone aria-hidden.

```html
<chatty-skeleton shape="circle"></chatty-skeleton>
<chatty-skeleton shape="text" style="flex-basis:100%;max-inline-size:240px"></chatty-skeleton>
<chatty-skeleton shape="text" style="flex-basis:100%;max-inline-size:144px"></chatty-skeleton>
<chatty-skeleton style="flex-basis:100%;max-inline-size:240px;--chatty-skeleton-height:96px"></chatty-skeleton>
<chatty-skeleton animation="none" style="flex-basis:100%;max-inline-size:240px;--chatty-skeleton-height:24px"></chatty-skeleton>
```

Prototype  (cùng `skeleton.styles.js` và ) là chuẩn: chatty-ui port 1:1 các file này, và `chatty-ui/custom-elements.json` sinh từ chính prototype. Chưa surface nào dùng element thật; các mục theo surface bên dưới mô tả React đang chạy.

### Element

| Element | Module |
| --- | --- |
| `chatty-skeleton` | @avada/chatty-ui/elements/skeleton.js |

### API

| Kind | Name | Type | Default | Description |
| --- | --- | --- | --- | --- |
| attribute | `shape` | rect, text, circle | `'rect'` | Hình bone: khối, dòng chữ hoặc tròn. |
| attribute | `animation` | pulse, none | `'pulse'` | pulse hoặc none; reduced motion luôn tắt pulse. |
| part | `base` | – | – | Khối bone. |
| css-property | `--chatty-skeleton-width` | – | – | Chiều rộng; mặc định full. |
| css-property | `--chatty-skeleton-height` | – | – | Chiều cao; text và circle có mặc định riêng. |
| css-property | `--chatty-skeleton-color` | – | – | Màu bone. |

## Classic

### Import / element

| Component / element | Import / file | Scope root |
| --- | --- | --- |
| `Skeleton` | @avada/chatty-ui | .chatty-widget > #Avada-FAQ_Widget > .Avada-Faqs_Popup |

### Anatomy

| Part | Element |
| --- | --- |
| 1 | Một `div` rỗng, ref forward, nhận `className`, `style` và props. |

### Placement / order

| Context | Order |
| --- | --- |
| GIFPicker | Khi loading, caller render 8 `Skeleton` trong container lưới thay cho ảnh GIF. |
| Primitive | Component không sở hữu nhóm bone hay status; caller quyết định số lượng và vị trí. |

### Props

| Prop | Type | Values | Default | Notes |
| --- | --- | --- | --- | --- |
| `variant` | string | text, circular, rectangular | `rectangular` | Chọn hình dạng bone. |
| `width` | string / number | CSS width | Không có | Vào inline style khi có giá trị. |
| `height` | string / number | CSS height | Không có | Vào inline style khi có giá trị. |
| `animation` | string / false | pulse, wave, false | `pulse` | `wave` map sang utility bounce; false bỏ animation. |
| `className` | string | CSS classes | Không có | Gộp cuối danh sách class. |
| `props` | attributes | aria-_, data-_, handlers | Caller | Spread vào thẻ div, kể cả aria-hidden. |

### Sizes

| Size | Height | Padding | Gap | Radius | Font role (px/rem) | Hit target | Breakpoint / mechanism |
| --- | --- | --- | --- | --- | --- | --- | --- |
| text | Derived(16px = h-4 × 4px) | Không có | Caller | `chatty:rounded`; giá trị không khai báo trong config | Không có chữ | Không tương tác | Không override theo viewport |
| circular | Theo `height` của caller | Không có | Caller | `chatty:rounded-full` = 9999px | Không có chữ | Không tương tác | Không override theo viewport |
| rectangular | Theo `height` của caller | Không có | Caller | `chatty:rounded-md` = 8px | Không có chữ | Không tương tác | Không override theo viewport |
| Ô GIFPicker | 80px | Không có | 4px | Theo variant mặc định rectangular | Không có chữ | Không tương tác | Rộng `calc(25% - 4px)` mỗi ô |

### Tokens

| Part | Token | Light | Dark |
| --- | --- | --- | --- |
| Fill | `--chatty-muted` | #f1f5f9 | Same; popup chỉ override `--chatty-muted-foreground` |

### States

| State | Trigger | Visual change (values) | ARIA/attribute |
| --- | --- | --- | --- |
| Pulse | `animation` pulse | Thêm utility `chatty:animate-pulse` | Không thêm ARIA |
| Wave | `animation` wave | Thêm utility `chatty:animate-bounce`, không phải shimmer | Không thêm ARIA |
| Static | `animation` false | Không thêm class animation | Không thêm ARIA |
| Reduced motion | Không có handler | Not available: component không đọc prefers-reduced-motion | Caller chọn `animation` false |
| Loading semantics | Caller truyền props | Not available: component không đặt role, aria-busy hay aria-hidden | Caller truyền qua props |

### Behaviour

| Interaction | Result |
| --- | --- |
| Đổi kích thước | Bone giữ `width` / `height` inline; caller phải khớp với nội dung cuối để tránh nhảy bố cục. |
| GIFPicker | Caller không truyền `width`, `height` hay aria; kích thước đến từ CSS của container lưới. |

### Copy

| String | i18n key | Where |
| --- | --- | --- |
| Caller-owned | Caller-owned | Component không render chữ; status do caller. |

### Source

| File | Evidence |
| --- | --- |
|  | API và class |
|  | Caller Classic |
|  | Kích thước ô lưới |

## Do / Don't

Do: truyền `width` và `height` bằng kích thước cuối của nội dung.

Don't: dựa vào `Skeleton` để tự ẩn khỏi trình đọc màn hình hoặc tự dừng chuyển động.

## Accessibility

| Surface | Role / name | Keyboard / focus | Live region | Contrast pair / ratio |
| --- | --- | --- | --- | --- |
| Classic | div không có role; caller truyền aria-hidden qua props | Không nhận focus | Component không có; GIFPicker cũng không đặt | Không có chữ; nền bone lấy từ token muted, đo theo nền merchant |

## Target

```target
| Surface | Now | Target | Source |
| --- | --- | --- | --- |
| Classic | `Skeleton` không tự đặt aria-hidden và không đọc prefers-reduced-motion | Not in code yet — build with the body values. Mặc định `aria-hidden="true"` trên bone, giữ nguyên width/height và radius đã đo; khi `prefers-reduced-motion: reduce` thì bỏ animation như `animation={false}`; caller đặt một vùng status và aria-busy; không có tương tác hay disabled; trạng thái bình thường trở lại khi dữ liệu về và bone được gỡ. | [WCAG — pause stop hide](https://www.w3.org/WAI/WCAG22/Understanding/pause-stop-hide.html) |
| Classic · AI Mode · Admin | Component React theo surface | Not available — new build follows Target. Port `chatty-skeleton` từ mục Standard rồi chuyển caller từng surface; Inbox Target dùng token chatty-ui literal theo giá trị 2.0; gỡ wrapper hiện tại ở phase 1b trước khi chuyển caller. |  |
```


- [WCAG — pause stop hide](https://www.w3.org/WAI/WCAG22/Understanding/pause-stop-hide.html)
- [MDN: Web Components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components)
