# Chat composer

Use `ChatComposer` from `@ancrel/workspace` when the application owns the draft and the lifetime of a response. Supply the current `value`, store edits in `onValueChange`, and use the string passed to `onSend`. Whitespace-only drafts disable sending. The callback receives the original spacing and newlines.

```tsx
'use client'

import { useState } from 'react'
import { ChatComposer } from '@ancrel/workspace'

export default function App() {
  const [draft, setDraft] = useState('')
  const [sending, setSending] = useState(false)
  const [message, setMessage] = useState('')
  return <>
    <ChatComposer label="Message" placeholder="Write a message" value={draft} onValueChange={setDraft}
      busy={sending} onSend={text => { setMessage(text); setDraft(''); setSending(true) }}
      onInterrupt={() => setSending(false)} leading={<span>{draft.length} characters</span>} />
    <output aria-label="Delivery">{sending ? 'Sending' : 'Idle'}</output>
    <p>{message}</p>
  </>
}
```

Expected:

Type a message and press Enter. The message appears below the composer and the delivery status changes to Sending. Focus the textarea and press Escape to return to Idle. Shift+Enter inserts a newline.

## Keyboard and focus

The default shortcut is Enter. Set `shortcut="mod-enter"` for Command+Enter on Apple platforms or Ctrl+Enter elsewhere. Shift+Enter inserts a newline. The shared IME handler leaves Enter available for selecting a candidate, including when the browser delivers Enter just after composition ends.

Set `busy` while a response is in progress and supply `onInterrupt` to show the stop action. Escape interrupts from the textarea or a focused control in the footer. The composer respects a nested control that already handled Escape and consumes the event when it interrupts. An enclosing overlay can handle an unconsumed Escape during bubbling.

## Labels and layout

Give the textarea an accessible `label`. Supply `sendLabel`, `interruptLabel` and `placeholder` for localized text. Place accessory controls in the footer with `leading` and `trailing`. The `compact` layout starts at one row. Long drafts scroll inside the textarea and users can resize it vertically. The ref points to the textarea.

Load the token, UI and workspace stylesheets at the application entry point.

| `Prop` | `Type` | `Required` | `Default` |
| - | - | - | - |
| `busy` | `boolean` | `false` | `—` |
| `className` | `string` | `false` | `—` |
| `compact` | `boolean` | `false` | `—` |
| `disabled` | `boolean` | `false` | `—` |
| `interruptLabel` | `string` | `false` | `—` |
| `key` | `React.Key \| null \| undefined` | `false` | `—` |
| `label` | `string` | `true` | `—` |
| `leading` | `ReactNode` | `false` | `—` |
| `onInterrupt` | `(() => void) \| undefined` | `false` | `—` |
| `onSend` | `(value: string) => void` | `true` | `—` |
| `onValueChange` | `(value: string) => void` | `true` | `—` |
| `placeholder` | `string` | `false` | `—` |
| `ref` | `React.Ref<HTMLTextAreaElement> \| undefined` | `false` | `—` |
| `sendLabel` | `string` | `false` | `—` |
| `shortcut` | `SendShortcut` | `false` | `—` |
| `trailing` | `ReactNode` | `false` | `—` |
| `value` | `string` | `true` | `—` |

```ts
import * as React from 'react';
import type { ChatComposerProps } from './types.js';
export type * from './types.js';
export declare const ChatComposer: React.ForwardRefExoticComponent<ChatComposerProps & React.RefAttributes<HTMLTextAreaElement>>;
```
