# Tooltip

Nhắc lại tên hoặc giải thích ngắn cho control; chỉ Classic có HtmlTooltip đang chạy.

## Key rules

- Bọc trigger bằng `HtmlTooltip`; tooltip mở khi chuột vào và đóng khi chuột ra, không mở bằng focus.
- Trigger luôn có tên riêng (`aria-label` hoặc text); tooltip chỉ là lời nhắc, không thay label.
- Tooltip là một dòng, 12px, nền `#fff` (hoặc `#000` khi `isLightMode` false).
- AI Mode, Admin và Admin · Knowledge files không import `HtmlTooltip`; xem Use when / Avoid.
- Element `chatty-tooltip` ở mục Standard là chuẩn chatty-ui port theo; mục Classic mô tả component React đang chạy.

## Use when / Avoid

Dùng cho icon quen thuộc hoặc label bị rút gọn trong widget Classic. Không đặt hướng dẫn bắt buộc, link hay nút trong tooltip: nội dung không nhận pointer và không có điều khiển bàn phím. AI Mode và admin chưa có tooltip dùng chung; nếu cần, xem Target.

### Use instead

| Component | Use when |
| --- | --- |
| `HtmlTooltip` | Nhắc tên icon-only action trong Classic message action. |
| `HtmlTooltip` | Giải thích trạng thái hết hàng trên action của product card. |
| `Button` | Thao tác cần tương tác: dùng Button, không nhúng action vào tooltip. |

## Live preview

Example (Standard) — Element chatty-tooltip đang hiện, phía dưới và phía trên trigger; token chuẩn.

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

export function Standard() {
  return html`
    <chatty-tooltip open style="margin: 40px 48px 48px 24px">
      <button
        type="button"
        aria-label="Emoji"
        style="display:inline-grid;place-items:center;width:32px;height:32px;padding:0;border:1px solid var(--chatty-color-border);border-radius:999px;background:var(--chatty-color-bg-surface);color:var(--chatty-color-text);cursor:pointer"
      >
        ${IconSmile({size: 16})}
      </button>
      <span slot="content">Emoji</span>
    </chatty-tooltip>
    <chatty-tooltip open placement="top" style="margin: 48px 24px 40px 48px">
      <button
        type="button"
        aria-label="Add to cart, out of stock"
        style="display:inline-grid;place-items:center;width:32px;height:32px;padding:0;border:1px solid var(--chatty-color-border);border-radius:999px;background:var(--chatty-color-bg-surface);color:var(--chatty-color-text);cursor:pointer"
      >
        +
      </button>
      <span slot="content">Out of stock</span>
    </chatty-tooltip>
  `;
}
```

Example (Current (React), being replaced) — Hover More actions để hiện tooltip; focus không mở tooltip.

```jsx
import React from 'react';
import {HtmlTooltip} from '@avada/chatty-ui/widget/Parts/HtmlTooltip.js';
import {IconButton} from '@avada/chatty-ui/components/icon-button.js';
import {MoreHorizIcon} from '@avada/chatty-ui/icons/MoreHorizIcon.js';
export function Label() {
  return <HtmlTooltip title="More actions" placement="bottom-start"><IconButton aria-label="More actions"><MoreHorizIcon size={20} /></IconButton></HtmlTooltip>;
}
```

## Standard

Example (Standard) — Element chatty-tooltip đang hiện, phía dưới và phía trên trigger; token chuẩn.

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

export function Standard() {
  return html`
    <chatty-tooltip open style="margin: 40px 48px 48px 24px">
      <button
        type="button"
        aria-label="Emoji"
        style="display:inline-grid;place-items:center;width:32px;height:32px;padding:0;border:1px solid var(--chatty-color-border);border-radius:999px;background:var(--chatty-color-bg-surface);color:var(--chatty-color-text);cursor:pointer"
      >
        ${IconSmile({size: 16})}
      </button>
      <span slot="content">Emoji</span>
    </chatty-tooltip>
    <chatty-tooltip open placement="top" style="margin: 48px 24px 40px 48px">
      <button
        type="button"
        aria-label="Add to cart, out of stock"
        style="display:inline-grid;place-items:center;width:32px;height:32px;padding:0;border:1px solid var(--chatty-color-border);border-radius:999px;background:var(--chatty-color-bg-surface);color:var(--chatty-color-text);cursor:pointer"
      >
        +
      </button>
      <span slot="content">Out of stock</span>
    </chatty-tooltip>
  `;
}
```

Prototype  (cùng `tooltip.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. Element hiện khi hover chuột sau `delay` hoặc ngay khi focus bàn phím, ẩn khi rời, blur, bấm, cuộn hoặc Escape; bong bóng chỉ nhắc lại tên nên ẩn với công nghệ hỗ trợ. Bong bóng không có viền (shadow-2 đã có vòng 1px), chữ body-x-small 13/16 weight 450 theo HtmlTooltip. Chưa surface nào dùng element thật; mục Classic bên dưới mô tả React đang chạy.

### Element

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

### API

| Kind | Name | Type | Default | Description |
| --- | --- | --- | --- | --- |
| attribute | `open` | boolean | `false` | Tooltip đang hiện; hover, focus bàn phím và Escape đổi giá trị này. |
| attribute | `placement` | top, bottom | `'bottom'` | Hiện dưới hoặc trên trigger; tự lật khi hết chỗ. |
| attribute | `delay` | number | `350` | Thời gian chờ (ms) trước khi hiện khi hover chuột. |
| slot | default | – | – | Control kích hoạt; giữ tên truy cập riêng. |
| slot | `content` | – | – | Chữ của tooltip. |
| part | `base` | – | – | Bong bóng tooltip trong top layer. |
| css-property | `--chatty-tooltip-max-width` | – | – | Chiều rộng tối đa trước khi chữ xuống dòng. |

## Classic

### Import / element

| Component / element | Import / file | Scope root |
| --- | --- | --- |
| `HtmlTooltip` | @avada/chatty-ui/widget/Parts/HtmlTooltip | Wrapper `div` của chính component; không dùng portal |

### Anatomy

| Part | Element |
| --- | --- |
| 1 | Wrapper `div` inline-flex, position relative, bọc trigger (`children`). |
| 2 | Bong bóng `div` absolute chứa `title`, chỉ render khi mở và có title. |

### Placement / order

| Context | Order |
| --- | --- |
| Placement mặc định | `bottom`: dưới trigger, căn giữa, cách 4px. |
| Các placement khác | `top`, `left`, `right`, `bottom-start`, `bottom-end`, `top-start`, `top-end`, mỗi hướng cách 4px. |
| Message action | Emoji `top`, Reply to `left`, Download `top`; mỗi tooltip bọc một icon. |

### Props

| Prop | Type | Values | Default | Notes |
| --- | --- | --- | --- | --- |
| `title` | node | Nội dung dòng tooltip | Không có | Không render nếu rỗng. |
| `placement` | string | Các giá trị ở Placement / order | `bottom` | Giá trị lạ rơi về nhánh `default` cùng kiểu `bottom`. |
| `isLightMode` | boolean | true / false | `true` | Chọn màu nền, màu chữ và bóng. |
| `open` | boolean | true / false | Không có | Boolean chuyển sang chế độ controlled, bỏ qua hover. |
| `className` | string | CSS class | Không có | Gắn vào wrapper. |
| `style` | object | CSS declarations | Không có | Trộn sau `position` và `display` của wrapper. |
| `children` | ReactNode | Trigger | Không có | Render trước bong bóng. |
| `PopperProps` | object | Caller truyền | Không có | Caller MessageAction truyền prop này nhưng component không khai báo nên bị bỏ qua. |

### Sizes

| Size | Height | Padding | Gap | Radius | Font role (px/rem) | Hit target | Breakpoint / mechanism |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Một kích thước | Theo nội dung | `4px 8px` | Cách trigger 4px qua margin placement | 8px | 12px; fontWeight 450 | `pointerEvents` none, không nhận pointer | Không có override theo viewport |

### Tokens

| Part | Token | Light | Dark |
| --- | --- | --- | --- |
| Nền | `backgroundColor` | #fff | #000 |
| Chữ | `color` | #000 | #fff |
| Bóng | `boxShadow` | 0 2px 8px rgba(0,0,0,0.15) | `none` |
| Lớp | `zIndex` | 999999 | 999999 |

### States

| State | Trigger | Visual change (values) | ARIA/attribute |
| --- | --- | --- | --- |
| Hover mở | `onMouseEnter` trên wrapper | Bong bóng hiện khi `visible` true | Không có role, id hay aria-describedby |
| Hover đóng | `onMouseLeave` trên wrapper | Bong bóng bị gỡ khỏi DOM | Không thêm ARIA |
| Controlled | `open` là boolean | Hiện theo `open`; handler hover không đổi state | Không thêm ARIA |
| Focus | Không có handler | Not available: không có onFocus / onBlur, focus không mở tooltip | Trigger tự giữ tên riêng |
| Touch / Escape | Không có handler | Not available: không có long-press hay phím Escape | Không thêm ARIA |

### Behaviour

| Interaction | Result |
| --- | --- |
| Dòng đơn | `whiteSpace` nowrap: không xuống dòng; caller tự giữ title ngắn. |
| Pointer | `pointerEvents` none: bong bóng không chặn click vào trigger. |
| Ẩn khi mở picker | Caller Emoji dùng `open` để ẩn tooltip khi picker mở. |
| Tooltip rỗng | Product card truyền `title` undefined khi còn hàng nên không có bong bóng. |

### Copy

| String | i18n key | Where |
| --- | --- | --- |
| Emoji | Emoji | Tooltip của nút emoji |
| Reply to | Reply to | Tooltip của nút trả lời |
| Download | Download | Tooltip của nút tải xuống |
| Out of stock | Out of stock | Tooltip action product card khi hết hàng |

### Source

| File | Evidence |
| --- | --- |
|  | API, handlers và style inline |
|  | Caller message action |

## Do / Don't

Do: giữ tên riêng cho trigger và giữ `title` ngắn, một dòng.

Don't: dùng tooltip thay label cho action khó đoán, hoặc đặt link và nút trong tooltip.

## Accessibility

Contrast đo trên màu inline của component; theme của merchant không đổi hai màu này.

| Surface | Role / name | Keyboard / focus | Live region | Contrast pair / ratio |
| --- | --- | --- | --- | --- |
| Classic | Bong bóng không có role, id hay aria-describedby; trigger giữ tên riêng | Hover mở tooltip; focus bàn phím không mở và không có Escape | Không có | Light Measured(#000000 / #ffffff = 21.00:1); `isLightMode` false Measured(#ffffff / #000000 = 21.00:1) |

## Target

```target
| Surface | Now | Target | Source |
| --- | --- | --- | --- |
| Classic | Focus bàn phím không mở tooltip; không có Escape | Not in code yet — build with the body values. Thêm `onFocus` / `onBlur` trên wrapper để mở và đóng, giữ nguyên kích thước và vị trí; thêm `id` cho bong bóng cùng `role="tooltip"`, nối trigger bằng `aria-describedby`; Escape đóng tooltip; trạng thái bình thường trở lại khi blur hoặc Escape. | [WCAG — content on hover or focus](https://www.w3.org/WAI/WCAG22/Understanding/content-on-hover-or-focus.html) |
| Classic | HtmlTooltip React | Not available — new build follows Target. Port `chatty-tooltip` từ mục Standard; bong bóng ở top layer qua Popover API, mở bằng focus bàn phím và đóng bằng Escape. |  |
| Admin | Component Inbox đúng Target chưa ship | Not available — new build follows Target. **Tooltip**: token 2.0: 12px/16px, nền inverse, radius element; mở bằng hover/focus và đóng bằng Esc. Dùng ở Composer tools và nút icon. 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 Tooltip design target](https://chatty-ui-research.avada-tesla-chatty.workers.dev/maps/inbox-target-tooltip.webp)

Ảnh mock đang hiển thị Insert product với chữ 12/18 và radius 6px. Target Inbox là 12/16 và radius element 8px như mẫu giá trị dưới đây; hover/focus/Escape vẫn là yêu cầu triển khai, không claim ảnh chứng minh đã ship.

Example (Current (React), being replaced) — Mẫu giá trị Target Inbox: chữ 12/16, nền inverse và radius element 8px; fixture tĩnh, không phải component đang chạy.

```jsx
import React from 'react';
export function Values() {
  return <section data-tooltip-target style={{'--chatty-radius-element':'8px','--chatty-type-family':'ShopifyInter, sans-serif','--chatty-type-weight-regular':400,fontFamily:'var(--chatty-type-family)',padding:16}}>
    <link rel="stylesheet" href="https://cdn.shopify.com/static/fonts/shopifyinter/v0/styles.css"/>
    <p>Target Inbox · mẫu giá trị tĩnh, chưa phải implementation.</p>
    <span data-tooltip-values style={{display:'inline-block',background:'#1b1b1b',color:'#fff',fontSize:12,lineHeight:'16px',fontWeight:'var(--chatty-type-weight-regular)',borderRadius:'var(--chatty-radius-element)',padding:'4px 8px'}}>Insert product</span>
    <p>12/16 · inverse · radius element 8px</p>
  </section>;
}
```


- [WCAG — content on hover or focus](https://www.w3.org/WAI/WCAG22/Understanding/content-on-hover-or-focus.html)
- [WAI-ARIA APG — tooltip](https://www.w3.org/WAI/ARIA/apg/patterns/tooltip/)
