# chatty-composer

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

## Key rules

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

Form-associated chat input; the host owns sending and attachment upload. Class `ChattyComposer`.

### Attributes

| Name | Property | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `value` | `string` | `''` | Draft text and form value. |
| `name` | `name` | `string` | `'message'` | Name used by the enclosing form. |
| `label` | `label` | `string` | `'Message'` | Accessible input label. |
| `send-label` | `sendLabel` | `string` | `'Send'` | Translated send button text. |
| `placeholder` | `placeholder` | `string` | `'Write a message…'` | Optional draft hint. |
| `disabled` | `disabled` | `boolean` | `false` | Prevents edits and submission while pending. |
| `sending` | `sending` | `boolean` | `false` | Host-owned send in progress; retains and dims the draft without dropping focus. |

### Properties

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

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `attachments` | `array` | `[]` | Attachment descriptors supplied by the host. |

### Events

| Name | Detail type | Description |
| --- | --- | --- |
| `chatty-send` | `CustomEvent<{text: string, attachments: Array}>` | Nonempty submission; cancel to retain the draft. |
| `click` | `MouseEvent` | Native send button click. |

### Slots

| Name | Description |
| --- | --- |
| `toolbar` | Suggestion or prefill actions before the typing row. |
| `attachments` | Selected attachment previews. |

### Parts

| Name | Description |
| --- | --- |
| `base` | Composer container. |
| `control` | Textarea. |
| `send` | Send button. |

### 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/composer.js';
import '@avada/chatty-ui/elements/button.js';
export function Standard() {
  return html`<div
    @chatty-send=${(event) => {
      event.currentTarget.querySelector('[role=status]').textContent = `Sent: ${event.detail.text}`;
    }}
  >
    <chatty-composer>
      <chatty-button
        slot="toolbar"
        @click=${(event) => event.currentTarget.closest('chatty-composer').send('Track my order')}
        >Track my order</chatty-button
      >
    </chatty-composer>
    <p role="status"></p>
  </div>`;
}
```

## 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/composer.js';
import '@avada/chatty-ui/elements/button.js';
export function Standard() {
  return html`<div
    @chatty-send=${(event) => {
      event.currentTarget.querySelector('[role=status]').textContent = `Sent: ${event.detail.text}`;
    }}
  >
    <chatty-composer>
      <chatty-button
        slot="toolbar"
        @click=${(event) => event.currentTarget.closest('chatty-composer').send('Track my order')}
        >Track my order</chatty-button
      >
    </chatty-composer>
    <p role="status"></p>
  </div>`;
}
```

## 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/composer.js';
import '@avada/chatty-ui/elements/button.js';
export function Standard() {
  return html`<div
    @chatty-send=${(event) => {
      event.currentTarget.querySelector('[role=status]').textContent = `Sent: ${event.detail.text}`;
    }}
  >
    <chatty-composer>
      <chatty-button
        slot="toolbar"
        @click=${(event) => event.currentTarget.closest('chatty-composer').send('Track my order')}
        >Track my order</chatty-button
      >
    </chatty-composer>
    <p role="status"></p>
  </div>`;
}
```

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

## Keyboard

| Key | Action |
| --- | --- |
| Enter | Send a nonempty draft; IME composition does not send. |
| Shift+Enter | Insert a new line. |

### Source

- chatty-ui/custom-elements.json
