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.
Live example
Open playground ↗App.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 behavior
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.
Props
| Prop | Type | Required | Default |
|---|---|---|---|
busy | boolean | No | — |
className | string | No | — |
compact | boolean | No | — |
disabled | boolean | No | — |
interruptLabel | string | No | — |
key | React.Key | null | undefined | No | — |
label | string | Yes | — |
leading | ReactNode | No | — |
onInterrupt | (() => void) | undefined | No | — |
onSend | (value: string) => void | Yes | — |
onValueChange | (value: string) => void | Yes | — |
placeholder | string | No | — |
ref | React.Ref<HTMLTextAreaElement> | undefined | No | — |
sendLabel | string | No | — |
shortcut | SendShortcut | No | — |
trailing | ReactNode | No | — |
value | string | Yes | — |
Constraints
- Keep the draft controlled and set busy while a send is in progress.
Anti-patterns
- Installing a second global Escape listener to stop sending.