# Avatar

Avatar tròn của kit chung; Classic chỉ dùng nó cho ảnh sản phẩm và badge số lượng trong đơn hàng.

## Key rules

- Dùng `Avatar` với `AvatarFallback` từ `@avada/chatty-ui`; mặc định `width: 36` và `height: 36` (số thuần của React style), bo `50%`.
- Order group ghi đè: container ảnh sản phẩm bo 8px, viền 1px; badge số lượng 16px.
- Đặt `alt` bằng tên sản phẩm hoặc chuỗi rỗng; root `Avatar` là `div`, không có role ảnh.
- Element `chatty-avatar` ở 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 cần một khung tròn cố định cho ảnh, chữ hoặc biểu tượng nhỏ. Kit không có trạng thái tải ảnh lỗi, nên caller tự chọn ảnh hoặc fallback. Classic hiện chỉ có caller là nhóm ảnh sản phẩm trong đơn hàng; không có avatar người gửi trong luồng chat Classic dùng component này.

### Use instead

| Component | Use when |
| --- | --- |
| `Avatar` | Khung tròn mặc định `width: 36` chứa ảnh hoặc `AvatarFallback`. |
| `img` | Ảnh sản phẩm đặt trực tiếp trong `Avatar`, caller tự cấp `alt`. |
| `Badge` | Nhãn trạng thái hoặc số đứng cạnh đối tượng, không phải khung ảnh tròn. |

## Live preview

Example (Standard) — Element chatty-avatar: size sm, md, lg với chữ viết tắt và ảnh.

```html
<chatty-avatar size="sm" label="Anna Lee">AL</chatty-avatar>
<chatty-avatar label="Anna Lee">AL</chatty-avatar>
<chatty-avatar size="lg" label="Anna Lee">AL</chatty-avatar>
<chatty-avatar
  ><img
    alt="Blue mug"
    src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 36 36'%3E%3Crect width='36' height='36' fill='gainsboro'/%3E%3Crect x='10' y='12' width='14' height='14' rx='2' fill='slategray'/%3E%3C/svg%3E"
/></chatty-avatar>
```

Example (Current (React), being replaced) — Avatar mặc định với AvatarFallback và AvatarImage; dữ liệu cục bộ.

```jsx
import React from 'react';
import AvatarGroup from '@scripttag/v1/MuiPages/AvatarGroup';
import {Avatar, AvatarFallback, AvatarImage} from '@avada/chatty-ui/components/avatar.js';

const avatars = ['/fixtures/avatar-alex.svg', '/fixtures/avatar-minh.svg'];

export function Catalogue() {
  return (
    <div className="button-list">
      <div>
        <Avatar>
          <AvatarFallback>AB</AvatarFallback>
        </Avatar>
        <p>Fallback</p>
      </div>
      <div>
        <Avatar>
          <AvatarImage src={avatars[0]} alt="Alex" />
        </Avatar>
        <p>Image</p>
      </div>
    </div>
  );
}
```

Example (Current (React), being replaced) — AvatarGroup thật của đơn hàng; JSX được trích đúng caller.

```jsx
import React, {useMemo, useState} from 'react';

import {Avatar, AvatarFallback} from '@avada/chatty-ui';

import {sizedShopifyImage} from '@scripttag/platform/helpers/shopifyImage.js';

const MAX_PRODUCT_IMAGES_TO_SHOW = 4;

export default function AvatarGroup({lineItems, i18n}) {
  const surplusCount = lineItems.length - MAX_PRODUCT_IMAGES_TO_SHOW;
  const [showAll, setShowAll] = useState(false);

  const lineItemsToShow = useMemo(() => {
    if (showAll) return lineItems;
    return surplusCount > 0 ? lineItems.slice(0, 3) : lineItems;
  }, [lineItems, showAll, surplusCount]);

  return (
    <div className="Avada-Faqs_Avatar-Group">
      {lineItemsToShow.map(({image, quantity, title}, index) => (
        <div key={index} className="chatty:relative">
          <Avatar variant="rounded" className="Avada-Faqs_Avatar-Container">
            <img
              src={sizedShopifyImage(image?.url, 48)}
              alt={title || ''}
              className="chatty:w-full chatty:h-full chatty:object-cover"
            />
          </Avatar>
          <div className="Avada-Faqs_Avatar-Badge-Wrapper chatty:absolute -chatty:top-1 -chatty:right-1">
            <Avatar className="Avada-Faqs_Avatar-Badge chatty:w-6 chatty:h-6">
              <AvatarFallback className="chatty:text-xs">{quantity}</AvatarFallback>
            </Avatar>
          </div>
        </div>
      ))}
      {surplusCount > 0 && !showAll && (
        <span className="Avada-Faqs_Avatar-Card_Link" onClick={() => setShowAll(prev => !prev)}>
          +{surplusCount + 1} {i18n('more')}
        </span>
      )}
    </div>
  );
}
```

Example (Standard) — Availability fixture: online, offline, break, holiday và null (không có dot).

```js
import '@avada/chatty-ui/tokens/tokens.css';
// Standard: the chatty-avatar prototype chatty-ui ports from (chatty-ui/src/elements/avatar.js).
import {html} from 'lit';
import '@avada/chatty-ui/elements/avatar.js';

export function Status() {
  return html`${['online', 'offline', 'break', 'holiday', null].map(status => html`<chatty-avatar .status=${status} label=${status ?? 'No availability'}>S</chatty-avatar>`)}`;
}
```

## Standard

Example (Standard) — Element chatty-avatar: size sm, md, lg với chữ viết tắt và ảnh.

```html
<chatty-avatar size="sm" label="Anna Lee">AL</chatty-avatar>
<chatty-avatar label="Anna Lee">AL</chatty-avatar>
<chatty-avatar size="lg" label="Anna Lee">AL</chatty-avatar>
<chatty-avatar
  ><img
    alt="Blue mug"
    src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 36 36'%3E%3Crect width='36' height='36' fill='gainsboro'/%3E%3Crect x='10' y='12' width='14' height='14' rx='2' fill='slategray'/%3E%3C/svg%3E"
/></chatty-avatar>
```

Prototype  (cùng `avatar.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-avatar` | @avada/chatty-ui/elements/avatar.js |

### API

`status`: online / offline / break / holiday hoặc null (mặc định). Dot 9px ở góc avatar, màu từ STATUS_DOT_COLOR của React; null không render node. Host cấp availability đã resolve.

| Kind | Name | Type | Default | Description |
| --- | --- | --- | --- | --- |
| attribute | `size` | sm, md, lg | `'md'` | Kích thước khung; md bằng khung mặc định của React. |
| attribute | `label` | string | `''` | Tên truy cập khi slot là chữ viết tắt; ảnh dùng alt của img. |
| attribute | `status` | online, offline, break, holiday, null | null | Availability; null ẩn dot. |
| part | `status` | – | – | Dot availability ở góc. |
| slot | default | – | – | img (cover) hoặc chữ viết tắt, icon fallback. |
| part | `base` | – | – | Khung tròn. |
| css-property | `--chatty-avatar-size` | – | – | Kích thước khung; thắng size. |
| css-property | `--chatty-avatar-radius` | – | – | Bo góc; mặc định tròn. |
| css-property | `--chatty-avatar-background` | – | – | Nền fallback sau chữ viết tắt. |

## Classic

### Import / element

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

### Anatomy

| Part | Element |
| --- | --- |
| 1 | Root `div` của `Avatar`, nhận `className`, `style` và props khác. |
| 2 | `img` của `AvatarImage` hoặc `img` do caller đặt trực tiếp. |
| 3 | `AvatarFallback`: `div` căn giữa chữ hoặc số, đặt trong `Avatar`. |

### Placement / order

| Context | Order |
| --- | --- |
| Nhóm ảnh đơn hàng | Với mỗi sản phẩm: `Avatar` chứa `img`, rồi wrapper badge tuyệt đối ở top -4px, right -4px chứa `Avatar` số lượng. |
| Nhóm ảnh đơn hàng | Tối đa 4 ảnh; nếu nhiều hơn, hiện 3 ảnh rồi link `more` mở toàn bộ. |

### Props

| Prop | Type | Values | Default | Notes |
| --- | --- | --- | --- | --- |
| `className` | string | CSS classes | undefined | Áp lên root `Avatar`. |
| `style` | object | CSS declarations | undefined | Gộp sau style mặc định nên ghi đè được kích thước. |
| `src` | string | URL ảnh | undefined | Chỉ có trên `AvatarImage`. |
| `alt` | string | Chuỗi mô tả hoặc rỗng | undefined | Chỉ có trên `AvatarImage`; caller đặt tên sản phẩm hoặc chuỗi rỗng. |
| `variant` | string | rounded | Caller | Caller truyền `variant` nhưng `Avatar` chỉ chuyển nó vào `div` qua props. |

### Sizes

| Size | Height | Padding | Gap | Radius | Font role (px/rem) | Hit target | Breakpoint / mechanism |
| --- | --- | --- | --- | --- | --- | --- | --- |
| default | `height: 36` | Không có | Không khai báo | `50%` | `fontSize: '0.875rem'`, `fontWeight: 500` | Không tương tác | Không có override theo viewport |
| Order container | `height: 36` (không override) | Không có | 12px giữa các item | 8px | Không có chữ | Không tương tác | `!important` trong scope popup |
| Order quantity badge | 16px | Không có | Không khai báo | `50%` (inherit từ default) | `heading-small`; `font-size: inherit` | Không tương tác | `!important` trong scope popup |
| Order badge wrapper | 20px | Không có | Không khai báo | `50%` | Không có chữ | Không tương tác | Vị trí tuyệt đối top -4px, right -4px |

### Tokens

| Part | Token | Light | Dark |
| --- | --- | --- | --- |
| Container border | `border` | 1px solid #e3e3e3 | `border-color`: transparent |
| Badge fill | `background-color` | #707070 | #6c6c6c |
| Badge text | `color` | white | white |
| Badge wrapper fill | `background` | white | #433f3f |

### States

| State | Trigger | Visual change (values) | ARIA/attribute |
| --- | --- | --- | --- |
| Default | Render | `overflow: hidden`; `flexShrink: 0` giữ khung tròn không bị co. | Không thêm ARIA trên root |
| Dark | Theme | Container viền transparent; badge và wrapper đổi nền theo Tokens. | Không thêm ARIA |
| Image | Có `alt` | `AvatarImage` giữ `aspectRatio: '1/1'`, chiều cao và rộng 100%. | `alt` do caller đặt |

### Behaviour

| Interaction | Result |
| --- | --- |
| Ảnh lỗi | Không có state tải hoặc lỗi; `AvatarImage` và `AvatarFallback` là hai phần tử độc lập, caller quyết định hiển thị. |
| Click | Không có handler; root là `div`. |
| Số lượng | Caller truyền `quantity` làm nội dung của `AvatarFallback`. |

### Copy

| String | i18n key | Where |
| --- | --- | --- |
| Tên sản phẩm | Caller-owned | `alt` của `img` (`title`), rỗng khi thiếu |
| Số lượng | Caller-owned | `AvatarFallback` (`quantity`) |

### Source

| File | Evidence |
| --- | --- |
|  | API và style mặc định |
|  | Caller duy nhất của Classic |
|  | Override trong scope popup |

## Do / Don't

Do: đặt `alt` bằng tên sản phẩm và để `AvatarFallback` giữ số lượng.

Don't: dùng root `Avatar` như nút; nó là `div` không có handler hay role.

## Accessibility

| Surface | Role / name | Keyboard / focus | Live region | Contrast pair / ratio |
| --- | --- | --- | --- | --- |
| Classic | `div` không role; tên đến từ `alt` của `img` do caller | Không focusable; không có handler | Không có | Chữ `white` trên nền #707070; không có dòng nguồn nào chứng minh tỉ lệ nên không nêu |

## Target

```target
| Surface | Now | Target | Source |
| --- | --- | --- | --- |
| Classic · AI Mode · Admin | Component React theo surface | Not available — new build follows Target. Port `chatty-avatar` từ mục Standard rồi chuyển caller từng surface; Inbox Target đọc token chatty-ui literal theo giá trị 2.0 sau phase 1b; ảnh có thể là `chatty-image` đặt vào slot để lo fallback. |  |
| Admin | Component Inbox đúng Target chưa ship | Not available — new build follows Target. **Avatar**: 20/28/36px; initials và 6 màu theo tên; ảnh lỗi về initials; Online dot và Channel badge; logo shop radius avatar. Dùng ở 8, 5, 10 và Details khách. Không dùng component Polaris trong Inbox; giữ App Bridge cho phần khung embedded. | [Shopify: Built for Shopify requirements](https://shopify.dev/docs/apps/launch/built-for-shopify/requirements) |
```

![Inbox Avatar design target](https://chatty-ui-research.avada-tesla-chatty.workers.dev/maps/inbox-target-overview.webp)

Ảnh Target từ mock; các trạng thái chưa xuất hiện trong ảnh vẫn là yêu cầu triển khai, không claim đã ship.


- [MDN: Web Components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components)
