# Icon button

Nút chỉ có biểu tượng của Classic: ba size vuông, năm màu, tên truy cập do caller cấp.

## Key rules

- Classic: dùng `IconButton` với `size` small, medium hoặc large; kích thước vuông lấy từ `chatty:h-8 chatty:w-8`, `chatty:h-10 chatty:w-10`, `chatty:h-12 chatty:w-12`.
- Classic: luôn truyền `aria-label`; component không có copy mặc định.
- Classic: caller muốn vùng bấm lớn hơn thì ghi đè bằng `className` như `chatty:h-11 chatty:w-11` (emoji trong ChatPage).
- Classic: inline style đặt `outline: none` nên focus chỉ còn `chatty:focus-visible:ring-2`.
- Element `chatty-icon-button` ở 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 hành động có biểu tượng quen thuộc như đóng hoặc emoji. Dùng Button khi biểu tượng chưa tự giải thích kết quả. Không dùng cho điều hướng nội dung trong câu trả lời; đó là việc của link.

### Use instead

| Component | Use when |
| --- | --- |
| `IconButton` | Thao tác chỉ có biểu tượng, cần tên truy cập. |
| `Button` | Hành động có nhãn chữ hoặc cần variant fill của kit. |

## Live preview

Example (Standard) — Element chatty-icon-button: size sm, md, lg và tone; nhãn truy cập qua label.

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {
  ArrowUpIcon,
  IconClose,
  IconExternalLink,
  IconTrash
} from '@avada/chatty-ui/elements/icon-glyphs.js';
// Standard: the chatty-icon-button prototype chatty-ui ports from (chatty-ui/src/elements/icon-button.js).
import {html} from 'lit';
import '@avada/chatty-ui/elements/icon-button.js';

export function Standard() {
  return html`
    <chatty-icon-button size="sm" label="Close chat">${IconClose({size: 20})}</chatty-icon-button>
    <chatty-icon-button label="Close chat">${IconClose({size: 20})}</chatty-icon-button>
    <chatty-icon-button size="lg" label="Send message"
      >${ArrowUpIcon({size: 20})}</chatty-icon-button
    >
    <chatty-icon-button tone="critical" label="Delete message"
      >${IconTrash({size: 20})}</chatty-icon-button
    >
  `;
}
```

Example (Standard) — Disabled và link của element.

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {
  ArrowUpIcon,
  IconClose,
  IconExternalLink,
  IconTrash
} from '@avada/chatty-ui/elements/icon-glyphs.js';
// Standard: the chatty-icon-button prototype chatty-ui ports from (chatty-ui/src/elements/icon-button.js).
import {html} from 'lit';
import '@avada/chatty-ui/elements/icon-button.js';

export function States() {
  return html`
    <chatty-icon-button label="Close chat" disabled>${IconClose({size: 20})}</chatty-icon-button>
    <chatty-icon-button href="https://example.com/help" label="Open help"
      >${IconExternalLink({size: 20})}</chatty-icon-button
    >
  `;
}
```

Example (Current (React), being replaced) — Ba size và năm màu của API `IconButton`.

```jsx
import React from 'react';
import {IconButton} from '@avada/chatty-ui/components/icon-button.js';
import {CloseIcon} from '@avada/chatty-ui/icons/CloseIcon.js';
export function Catalogue() {
  return <div className="specimen-grid">
    <section>
      <h4>Sizes</h4>
      <div className="button-list">
        {['small', 'medium', 'large'].map(size => (
          <IconButton key={size} size={size} aria-label={'Close ' + size}><CloseIcon size={20} /></IconButton>
        ))}
      </div>
    </section>
    <section>
      <h4>Colors</h4>
      <div className="button-list">
        {['default', 'primary', 'secondary', 'error', 'inherit'].map(color => (
          <IconButton key={color} color={color} aria-label={'Close ' + color}><CloseIcon size={20} /></IconButton>
        ))}
      </div>
    </section>
  </div>;
}
```

Example (Current (React), being replaced) — Idle và disabled của `IconButton` small.

```jsx
import React from 'react';
import {IconButton} from '@avada/chatty-ui/components/icon-button.js';
import {CloseIcon} from '@avada/chatty-ui/icons/CloseIcon.js';
export function States() {
  return <div className="button-list">
    <IconButton size="small" aria-label="Close"><CloseIcon size={20} /></IconButton>
    <IconButton size="small" aria-label="Close unavailable" disabled><CloseIcon size={20} /></IconButton>
  </div>;
}
```

## Standard

Example (Standard) — Element chatty-icon-button: size sm, md, lg và tone; nhãn truy cập qua label.

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {
  ArrowUpIcon,
  IconClose,
  IconExternalLink,
  IconTrash
} from '@avada/chatty-ui/elements/icon-glyphs.js';
// Standard: the chatty-icon-button prototype chatty-ui ports from (chatty-ui/src/elements/icon-button.js).
import {html} from 'lit';
import '@avada/chatty-ui/elements/icon-button.js';

export function Standard() {
  return html`
    <chatty-icon-button size="sm" label="Close chat">${IconClose({size: 20})}</chatty-icon-button>
    <chatty-icon-button label="Close chat">${IconClose({size: 20})}</chatty-icon-button>
    <chatty-icon-button size="lg" label="Send message"
      >${ArrowUpIcon({size: 20})}</chatty-icon-button
    >
    <chatty-icon-button tone="critical" label="Delete message"
      >${IconTrash({size: 20})}</chatty-icon-button
    >
  `;
}
```

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

### API

| Kind | Name | Type | Default | Description |
| --- | --- | --- | --- | --- |
| attribute | `tone` | neutral, critical, success, caution | `'neutral'` | Màu icon theo ý nghĩa; giá trị lạ quay về neutral. |
| attribute | `size` | sm, md, lg | `'md'` | Kích thước ô vuông, cùng ba bậc của React. |
| attribute | `disabled` | boolean | `false` | Chặn kích hoạt, vẫn nhận focus và báo aria-disabled. |
| attribute | `href` | string | `''` | URL http, https, mailto hoặc tel thì render link; URL khác bị bỏ. |
| attribute | `label` | string | `''` | Tên truy cập; bắt buộc vì nội dung chỉ là icon. |
| event | `click` | MouseEvent | – | Kích hoạt native; bị chặn khi disabled. |
| slot | default | – | – | Icon (SVG hoặc chatty-icon). |
| part | `base` | – | – | Control native (button hoặc a). |
| css-property | `--chatty-icon-button-size` | – | – | Kích thước ô vuông; thắng size. |
| css-property | `--chatty-icon-button-color` | – | – | Màu icon theo tone. |
| css-property | `--chatty-icon-button-radius` | – | – | Bo góc. |

## Classic

### Import / element

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

### Anatomy

| Part | Element |
| --- | --- |
| 1 | Tag native a hoặc button, chọn bằng `href`. |
| 2 | `children` là biểu tượng do caller cấp. |

### Placement / order

| Context | Order |
| --- | --- |
| Emoji trong chat | Caller đặt `IconButton` emoji trước vùng gửi; id `Insert-Emoji-button`, `chatty:h-11 chatty:w-11`. |
| Primitive | Component nhận `children`, không tự tạo hàng nhiều nút. |

### Props

| Prop | Type | Values | Default | Notes |
| --- | --- | --- | --- | --- |
| `size` | string | small, medium, large | `medium` | Chọn width và height vuông. |
| `color` | string | default, primary, secondary, error, inherit | `default` | Chọn màu chữ và nền hover. |
| `disabled` | boolean | true, false | undefined | Chỉ chuyển cho native button; nhánh `href` bỏ qua. |
| `href` | string | URL | undefined | Truthy đổi Tag thành a và thêm `chatty:no-underline`. |
| `className` | string | CSS classes | undefined | Gộp cuối, ghi đè size qua tailwind-merge. |
| `style` | object | CSS declarations | undefined | Merge sau `background: 'none'`, `border: 'none'`, `outline: 'none'`. |
| `children` | ReactNode | Biểu tượng | undefined | Không có glyph mặc định. |
| `props` | attributes | Native attrs, handlers, ARIA attributes | Caller | Spread vào Tag; tên truy cập do caller đặt. |

### Sizes

| Size | Height | Padding | Gap | Radius | Font role (px/rem) | Hit target | Breakpoint / mechanism |
| --- | --- | --- | --- | --- | --- | --- | --- |
| small | Derived(32px = h-8 × 4px) | Không khai báo | Không khai báo | 8px (`chatty:rounded-md`) | Không có chữ | Native box | Không override theo viewport |
| medium | Derived(40px = h-10 × 4px) | Không khai báo | Không khai báo | 8px (`chatty:rounded-md`) | Không có chữ | Native box | Không override theo viewport |
| large | Derived(48px = h-12 × 4px) | Không khai báo | Không khai báo | 8px (`chatty:rounded-md`) | Không có chữ | Native box | Không override theo viewport |
| Emoji caller | Derived(44px = h-11 × 4px) | Không khai báo | Không khai báo | 8px (`chatty:rounded-md`) | Không có chữ | Native box | `className` ghi đè size small |

### Tokens

| Part | Token | Light | Dark |
| --- | --- | --- | --- |
| Text default | `--chatty-foreground` | #0f172a | Merchant `--faqsTextColor` |
| Text secondary | `--chatty-muted-foreground` | #64748b | Merchant `--faqsTextColorSecondary` |
| Text primary | `--chatty-primary` | #6366f1 | #6366f1 |
| Text error | `--chatty-destructive` | #ef4444 | #ef4444 |
| Hover fill | `--chatty-muted` | #f1f5f9 | #f1f5f9 |
| Focus ring | `--chatty-ring` | Merchant `--faqsTextColor`, fallback #303030 | Merchant `--faqsTextColor`, fallback #eee |
| Radius | `--chatty-radius-md` | 8px | 8px |

### States

| State | Trigger | Visual change (values) | ARIA/attribute |
| --- | --- | --- | --- |
| Hover default | pointer | `hover:chatty:bg-muted` | Không thêm ARIA |
| Hover primary | pointer | `hover:chatty:bg-primary/10` | Không thêm ARIA |
| Hover error | pointer | `hover:chatty:bg-destructive/10` | Không thêm ARIA |
| Focus-visible | keyboard | `chatty:focus-visible:ring-2`; inline `outline: 'none'` che outline | Native focus |
| Disabled | `disabled` | `chatty:disabled:opacity-50`, `chatty:disabled:pointer-events-none` | Native disabled chỉ trên button |
| Loading | caller | Not available built-in | Caller aria-busy |

### Behaviour

| Interaction | Result |
| --- | --- |
| Keyboard | Native button nhận Enter/Space; anchor nhận Enter. |
| Link | `href` đổi sang anchor; `disabled` không áp lên anchor. |

### Copy

| String | i18n key | Where |
| --- | --- | --- |
| Emoji | Emoji | `aria-label` của emoji caller |
| Caller label | Caller-owned | Tên truy cập do caller đặt |

### Source

| File | Evidence |
| --- | --- |
|  | API và element |
|  | Caller emoji |
|  | Classic token override |

## Do / Don't

Do: truyền `aria-label` mô tả thao tác và dùng một size trong cùng hàng.

Don't: dùng `IconButton` cho điều hướng trong nội dung; để `href` thay anchor link.

## Accessibility

| Surface | Role / name | Keyboard / focus | Live region | Contrast pair / ratio |
| --- | --- | --- | --- | --- |
| Classic | Native button hoặc anchor; tên từ `aria-label` | Button Enter/Space; anchor Enter; focus ring khi focus-visible | Caller-owned | Default Measured(#0f172a / #ffffff = 17.85:1); secondary Measured(#64748b / #ffffff = 4.76:1) |

## Target

```target
| Surface | Now | Target | Source |
| --- | --- | --- | --- |
| Classic | Size small là 32px; hit target không mở rộng | Not in code yet — build with the body values. Caller trên mobile truyền `chatty:h-11 chatty:w-11` để đạt 44px. | [WCAG — target size enhanced](https://www.w3.org/WAI/WCAG22/Understanding/target-size-enhanced.html) |
| Classic | Inline `outline: 'none'` che outline native | Not in code yet — build with the body values. Giữ ring khi focus-visible và đo tương phản của ring với nền thật. | [WCAG — focus appearance](https://www.w3.org/WAI/WCAG22/Understanding/focus-appearance.html) |
| Classic · AI Mode · Admin | Component React theo surface | Not available — new build follows Target. Port `chatty-icon-button` 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 — target size enhanced](https://www.w3.org/WAI/WCAG22/Understanding/target-size-enhanced.html)
- [WCAG — focus appearance](https://www.w3.org/WAI/WCAG22/Understanding/focus-appearance.html)
- [WAI-ARIA APG — button](https://www.w3.org/WAI/ARIA/apg/patterns/button/)
