# chatty-surface

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

## Key rules

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

Conversation shell with a scrolling thread between fixed header and footer slots. Class `ChattySurface`.

### Attributes

| Name | Property | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `label` | `label` | `string` | `'Conversation'` | Accessible conversation name. |
| `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. |
| `new-messages-label` | `newMessagesLabel` | `string` | `'New messages'` | Translated action shown when updates arrive while reading older messages. |

### Properties

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

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `store` | `Object \| null` | `null` | Canonical store forwarded to the default or slotted thread. |
| `renderMessage` | `Function \| null` | `null` | Trusted callback forwarded to the default or slotted thread. |
| `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 | Thread or other conversation content; defaults to a store-backed thread. |
| `empty` | Greeting or empty state when the store has no messages. |
| `header` | Persistent header. |
| `footer` | Composer or contact step. |

### Parts

| Name | Description |
| --- | --- |
| `base` | Surface shell. |
| `header` | Header wrapper. |
| `viewport` | Scrolling conversation viewport. |
| `new-messages` | Jump to the latest messages. |
| `footer` | Footer wrapper. |

### CSS custom properties

| Name | Default | Description |
| --- | --- | --- |
| `--chatty-surface-height` | – | Shell height. |
| `--chatty-surface-footer-inset` | – | Additional footer clearance, default 0px. |

## Usage

```js
import '@avada/chatty-ui/tokens/tokens.css';
import {html} from 'lit';
import '@avada/chatty-ui/elements/surface.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-surface .store=${fixtures.store()} viewer-type="customer">
    <strong slot="header">Store assistant</strong>
    <chatty-thread></chatty-thread>
    <chatty-composer slot="footer"></chatty-composer>
  </chatty-surface>`;
}
```

## 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/surface.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-surface .store=${fixtures.store()} viewer-type="customer">
    <strong slot="header">Store assistant</strong>
    <chatty-thread></chatty-thread>
    <chatty-composer slot="footer"></chatty-composer>
  </chatty-surface>`;
}
```

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/surface.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 Empty() {
  return html`<chatty-surface .store=${fixtures.store([])} viewer-type="customer">
    <strong slot="header">Store assistant</strong>
    <div slot="empty">
      <h3>How can we help?</h3>
      <p>Ask about products, delivery or your order.</p>
    </div>
    <chatty-composer slot="footer"></chatty-composer>
  </chatty-surface>`;
}
```

## 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/surface.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-surface .store=${fixtures.store()} viewer-type="customer">
    <strong slot="header">Store assistant</strong>
    <chatty-thread></chatty-thread>
    <chatty-composer slot="footer"></chatty-composer>
  </chatty-surface>`;
}
```

| Part | Element | Source |
| --- | --- | --- |
| 1 | CSS part `base` | chatty-ui/custom-elements.json |
| 2 | CSS part `header` | chatty-ui/custom-elements.json |
| 3 | CSS part `viewport` | chatty-ui/custom-elements.json |
| 4 | CSS part `new-messages` | chatty-ui/custom-elements.json |
| 5 | CSS part `footer` | chatty-ui/custom-elements.json |

## Keyboard

No element-specific keys.

### Source

- chatty-ui/custom-elements.json
