# chatty-thread

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

## Key rules

- `chatty-thread` khai báo 4 attribute, 4 property không có attribute, 0 event, 1 slot, 1 part và 0 CSS custom property.
- Import 1 module `@avada/chatty-ui/elements/thread.js` là define `chatty-thread`; 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

Ordered canonical messages with keyed rows and calendar day boundaries. Class `ChattyThread`.

### Attributes

| Name | Property | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `label` | `label` | `string` | `'Conversation'` | Accessible conversation name. |
| `viewer-type` | `viewerType` | `string` | `'customer'` | Sender type on the trailing side. |
| `self-types` | `selfTypes` | `Array<string> \| null` | `null` | Sender types on the trailing side; null falls back to viewerType. |
| `locale` | `locale` | `string` | `'en'` | Locale used for calendar labels. |

### Properties

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

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `store` | `Object \| null` | `null` | Canonical store implementing getState and subscribe. |
| `strings` | `object` | `{}` | Translated clock, day, sender and delivery labels. |
| `renderMessage` | `Function \| null` | `null` | Trusted per-message callback returning sender, badge, meta and actions content. |
| `registry` | `Object \| null` | `null` | Trusted optional block registry forwarded to each message; null keeps built-in blocks. |

### Events

Not declared in the manifest.

### Slots

| Name | Description |
| --- | --- |
| default | Direct messages when no store is supplied. |

### Parts

| Name | Description |
| --- | --- |
| `base` | Conversation log. |

### CSS custom properties

Not declared in the manifest.

## Usage

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {html} from 'lit';
import '@avada/chatty-ui/elements/thread.js';
import '@avada/chatty-ui/elements/rating.js';
export const messages = [
  {
    id: 'question',
    senderType: 'customer',
    senderName: 'Customer',
    text: 'Which bag works for everyday trips?',
    status: 'sent',
    createdAt: '2026-01-15T10:00:00Z',
  },
  {
    id: 'answer',
    senderType: 'bot',
    senderName: 'Assistant',
    text: 'Try the **canvas tote**. It is lightweight and reusable.',
    status: 'sent',
    createdAt: '2026-01-15T10:01:00Z',
  },
  {
    id: 'detail',
    senderType: 'bot',
    senderName: 'Assistant',
    text: 'The wide straps make it comfortable to carry.',
    status: 'sent',
    createdAt: '2026-01-15T10:02:00Z',
    blocks: [{type: 'product', title: 'Canvas tote', price: '$24', image: 'https://chatty-ui-research.avada-tesla-chatty.workers.dev/fixtures/tote.svg'}],
  },
  {
    id: 'followup',
    senderType: 'customer',
    text: 'Does it come in other colors?',
    status: 'sent',
    createdAt: '2026-01-16T10:00:00Z',
  },
  {
    id: 'agent',
    senderType: 'agent',
    senderName: 'Alex',
    text: 'Yes, we also have navy and natural.',
    status: 'sent',
    createdAt: '2026-01-16T10:01:00Z',
  },
];
export const fixtures = {
  store: (items = messages) => {
    let state = {messages: items};
    const listeners = new Set();
    return {
      getState: () => state,
      subscribe(listener) {
        listeners.add(listener);
        return () => listeners.delete(listener);
      },
      setMessages(messages) {
        state = {messages};
        listeners.forEach((listener) => listener());
      },
    };
  },
};
export function Standard() {
  return html`<chatty-thread .store=${fixtures.store()} viewer-type="customer"></chatty-thread>`;
}
```

## 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/thread.js';
import '@avada/chatty-ui/elements/rating.js';
export const messages = [
  {
    id: 'question',
    senderType: 'customer',
    senderName: 'Customer',
    text: 'Which bag works for everyday trips?',
    status: 'sent',
    createdAt: '2026-01-15T10:00:00Z',
  },
  {
    id: 'answer',
    senderType: 'bot',
    senderName: 'Assistant',
    text: 'Try the **canvas tote**. It is lightweight and reusable.',
    status: 'sent',
    createdAt: '2026-01-15T10:01:00Z',
  },
  {
    id: 'detail',
    senderType: 'bot',
    senderName: 'Assistant',
    text: 'The wide straps make it comfortable to carry.',
    status: 'sent',
    createdAt: '2026-01-15T10:02:00Z',
    blocks: [{type: 'product', title: 'Canvas tote', price: '$24', image: 'https://chatty-ui-research.avada-tesla-chatty.workers.dev/fixtures/tote.svg'}],
  },
  {
    id: 'followup',
    senderType: 'customer',
    text: 'Does it come in other colors?',
    status: 'sent',
    createdAt: '2026-01-16T10:00:00Z',
  },
  {
    id: 'agent',
    senderType: 'agent',
    senderName: 'Alex',
    text: 'Yes, we also have navy and natural.',
    status: 'sent',
    createdAt: '2026-01-16T10:01:00Z',
  },
];
export const fixtures = {
  store: (items = messages) => {
    let state = {messages: items};
    const listeners = new Set();
    return {
      getState: () => state,
      subscribe(listener) {
        listeners.add(listener);
        return () => listeners.delete(listener);
      },
      setMessages(messages) {
        state = {messages};
        listeners.forEach((listener) => listener());
      },
    };
  },
};
export function Standard() {
  return html`<chatty-thread .store=${fixtures.store()} viewer-type="customer"></chatty-thread>`;
}
```

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/thread.js';
import '@avada/chatty-ui/elements/rating.js';
export const messages = [
  {
    id: 'question',
    senderType: 'customer',
    senderName: 'Customer',
    text: 'Which bag works for everyday trips?',
    status: 'sent',
    createdAt: '2026-01-15T10:00:00Z',
  },
  {
    id: 'answer',
    senderType: 'bot',
    senderName: 'Assistant',
    text: 'Try the **canvas tote**. It is lightweight and reusable.',
    status: 'sent',
    createdAt: '2026-01-15T10:01:00Z',
  },
  {
    id: 'detail',
    senderType: 'bot',
    senderName: 'Assistant',
    text: 'The wide straps make it comfortable to carry.',
    status: 'sent',
    createdAt: '2026-01-15T10:02:00Z',
    blocks: [{type: 'product', title: 'Canvas tote', price: '$24', image: 'https://chatty-ui-research.avada-tesla-chatty.workers.dev/fixtures/tote.svg'}],
  },
  {
    id: 'followup',
    senderType: 'customer',
    text: 'Does it come in other colors?',
    status: 'sent',
    createdAt: '2026-01-16T10:00:00Z',
  },
  {
    id: 'agent',
    senderType: 'agent',
    senderName: 'Alex',
    text: 'Yes, we also have navy and natural.',
    status: 'sent',
    createdAt: '2026-01-16T10:01:00Z',
  },
];
export const fixtures = {
  store: (items = messages) => {
    let state = {messages: items};
    const listeners = new Set();
    return {
      getState: () => state,
      subscribe(listener) {
        listeners.add(listener);
        return () => listeners.delete(listener);
      },
      setMessages(messages) {
        state = {messages};
        listeners.forEach((listener) => listener());
      },
    };
  },
};
export function Feedback() {
  return html`<chatty-thread
    .store=${fixtures.store(messages.slice(0, 2))}
    viewer-type="customer"
    .renderMessage=${(message) =>
      message.senderType === 'bot'
        ? {
            actions: html`<chatty-rating label="Rate this answer"></chatty-rating>`,
            badge: html`<span>AI</span>`,
          }
        : {}}
  ></chatty-thread>`;
}
```

## 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/thread.js';
import '@avada/chatty-ui/elements/rating.js';
export const messages = [
  {
    id: 'question',
    senderType: 'customer',
    senderName: 'Customer',
    text: 'Which bag works for everyday trips?',
    status: 'sent',
    createdAt: '2026-01-15T10:00:00Z',
  },
  {
    id: 'answer',
    senderType: 'bot',
    senderName: 'Assistant',
    text: 'Try the **canvas tote**. It is lightweight and reusable.',
    status: 'sent',
    createdAt: '2026-01-15T10:01:00Z',
  },
  {
    id: 'detail',
    senderType: 'bot',
    senderName: 'Assistant',
    text: 'The wide straps make it comfortable to carry.',
    status: 'sent',
    createdAt: '2026-01-15T10:02:00Z',
    blocks: [{type: 'product', title: 'Canvas tote', price: '$24', image: 'https://chatty-ui-research.avada-tesla-chatty.workers.dev/fixtures/tote.svg'}],
  },
  {
    id: 'followup',
    senderType: 'customer',
    text: 'Does it come in other colors?',
    status: 'sent',
    createdAt: '2026-01-16T10:00:00Z',
  },
  {
    id: 'agent',
    senderType: 'agent',
    senderName: 'Alex',
    text: 'Yes, we also have navy and natural.',
    status: 'sent',
    createdAt: '2026-01-16T10:01:00Z',
  },
];
export const fixtures = {
  store: (items = messages) => {
    let state = {messages: items};
    const listeners = new Set();
    return {
      getState: () => state,
      subscribe(listener) {
        listeners.add(listener);
        return () => listeners.delete(listener);
      },
      setMessages(messages) {
        state = {messages};
        listeners.forEach((listener) => listener());
      },
    };
  },
};
export function Standard() {
  return html`<chatty-thread .store=${fixtures.store()} viewer-type="customer"></chatty-thread>`;
}
```

| Part | Element | Source |
| --- | --- | --- |
| 1 | CSS part `base` | chatty-ui/custom-elements.json |

## Keyboard

No element-specific keys.

### Source

- chatty-ui/custom-elements.json
