# chatty-message

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

## Key rules

- `chatty-message` khai báo 6 attribute, 6 property không có attribute, 0 event, 5 slot, 6 part và 1 CSS custom property.
- Import 1 module `@avada/chatty-ui/elements/message.js` là define `chatty-message`; 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 canonical message with sender grouping and registered rich blocks. Class `ChattyMessage`.

### Attributes

| Name | Property | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `message` | `message` | `Object \| null` | `null` | Canonical message; keep untouched message references stable. |
| `message-id` | `messageId` | `string` | `''` | Stable ID selected from store.messages. |
| `viewer-type` | `viewerType` | `string` | `'customer'` | Sender type displayed on the trailing side. |
| `self-types` | `selfTypes` | `Array<string> \| null` | `null` | Sender types on the trailing side; null falls back to viewerType. |
| `surface` | `surface` | `string` | `''` | Visual surface; inferred from the consumer theme root. |
| `locale` | `locale` | `string` | `'en'` | Locale used for clocks and full datetimes. |

### Properties

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

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `prev` | `Object \| null` | `null` | Previous canonical message for direct rendering. |
| `next` | `Object \| null` | `null` | Next canonical message for direct rendering. |
| `store` | `Object \| null` | `null` | Canonical store, optionally owned by chatty-thread. |
| `strings` | `object` | `{}` | Translated sender, delivery, rich-content and block-action labels. |
| `renderMessage` | `Function \| null` | `null` | Trusted callback returning sender, badge, meta and actions Lit content per message. |
| `registry` | `Object \| null` | – | Trusted Map-like block extensions; built-in types remain available and layout(type) supplies positioning flags. |

### Events

Not declared in the manifest.

### Slots

| Name | Description |
| --- | --- |
| default | Additional block content. |
| `sender` | Readable sender at the start of a run. |
| `badge` | Sender badge. |
| `meta` | Metadata after the message. |
| `actions` | Message actions. |

### Parts

| Name | Description |
| --- | --- |
| `base` | Message row. |
| `bubble` | Message text. |
| `blocks` | Rich blocks before or after the message text. |
| `sender` | Sender label and badge. |
| `meta` | Metadata wrapper. |
| `actions` | Actions wrapper. |

### CSS custom properties

| Name | Default | Description |
| --- | --- | --- |
| `--chatty-message-radius` | – | Bubble radius. |

## Usage

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {html} from 'lit';
import '@avada/chatty-ui/elements/message.js';
const message = {
  id: 'answer',
  senderType: 'bot',
  senderName: 'Assistant',
  text: 'Try the **canvas tote**. It is lightweight and reusable.',
  status: 'sent',
  createdAt: '2026-01-15T10:01:00Z',
};
export function Standard() {
  return html`<chatty-message .message=${message} viewer-type="customer"></chatty-message>`;
}
```

## 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.

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {html} from 'lit';
import '@avada/chatty-ui/elements/message.js';
const message = {
  id: 'answer',
  senderType: 'bot',
  senderName: 'Assistant',
  text: 'Try the **canvas tote**. It is lightweight and reusable.',
  status: 'sent',
  createdAt: '2026-01-15T10:01:00Z',
};
export function Standard() {
  return html`<chatty-message .message=${message} viewer-type="customer"></chatty-message>`;
}
```

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

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {html} from 'lit';
import '@avada/chatty-ui/elements/message.js';
const message = {
  id: 'answer',
  senderType: 'bot',
  senderName: 'Assistant',
  text: 'Try the **canvas tote**. It is lightweight and reusable.',
  status: 'sent',
  createdAt: '2026-01-15T10:01:00Z',
};
export function Centered() {
  return html`<chatty-message
    .message=${{
      id: 'joined',
      senderType: 'system',
      layout: 'centered',
      text: 'Agent joined',
      createdAt: '2026-01-15T10:05:00Z',
    }}
  ></chatty-message>`;
}
```

## 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ố.

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {html} from 'lit';
import '@avada/chatty-ui/elements/message.js';
const message = {
  id: 'answer',
  senderType: 'bot',
  senderName: 'Assistant',
  text: 'Try the **canvas tote**. It is lightweight and reusable.',
  status: 'sent',
  createdAt: '2026-01-15T10:01:00Z',
};
export function Standard() {
  return html`<chatty-message .message=${message} viewer-type="customer"></chatty-message>`;
}
```

| Part | Element | Source |
| --- | --- | --- |
| 1 | CSS part `base` | chatty-ui/custom-elements.json |
| 2 | CSS part `bubble` | chatty-ui/custom-elements.json |
| 3 | CSS part `blocks` | chatty-ui/custom-elements.json |
| 4 | CSS part `sender` | chatty-ui/custom-elements.json |
| 5 | CSS part `meta` | chatty-ui/custom-elements.json |
| 6 | CSS part `actions` | chatty-ui/custom-elements.json |

## Keyboard

No element-specific keys.

### Source

- chatty-ui/custom-elements.json
