# ChatBox Read a conversation, reply to a message, or share a file in a single thread. [Interactive guide](/docs/components/chatbox) · Forge 3.2.0 Use the API for your installed version. Run `npx --no-install wss3-forge api --json` inside the project. ## Implementation guidance ### When to use it Use ChatBox for a ready-made conversation with a composer. Use MessageScroller and Message when the transcript requires custom behavior or markup. ### How to compose it Keep ChatMessage records and sending in application state. Update delivery and typing from the transport and provide onAttach only when attachments can open. ### What to verify Exercise failed sending and retry. Confirm Enter sends, Shift+Enter inserts a line and pending state avoids accidental duplicate submission. ## Usage ```tsx import { useState } from 'react' import { ChatBox, type ChatMessage } from 'wss3-forge' ``` - Keep messages in application state. Append the outgoing message in onSend and update its status when your service reports delivery or a read receipt. These examples keep messages in memory. - ChatBox composes Forge Message and Bubble for each turn. Use sender="user" for outgoing messages, sender="assistant" for the other participant and sender="system" for a thread notice. Supply a name to identify the participant. - Message avatars use Forge Avatar. ChatMessage.avatar supplies its src, avatarColor supplies its color and name supplies the initials when there is no image. Use the same color for every message from a participant. - Enter sends a message and Shift+Enter adds a line. Pass typing and typingName while the other participant is composing a reply. Use loading when the composer must wait. - Pass onAttach to open your file picker. Each attachment needs a name, type and URL. The example uses a local image for the initial message and temporary object URLs for selected files. Supply your own image or upload URL in your app. - Use MiniChat with open and onToggle to control the launcher. Its default placement anchors it to the viewport. Set placement="inline" to keep the panel and launcher inside a layout. ```tsx export function StudioThread() { const [messages, setMessages] = useState([]) return { const message: ChatMessage = { id: crypto.randomUUID(), sender: 'user', content, timestamp: new Date(), } setMessages(current => [...current, message]) }} /> } ``` ## Runnable examples ### Write a message in an inline conversation ```tsx import { useState } from 'react' import { ChatBox, useResponsiveValue, type ChatMessage } from 'wss3-forge' const proofReview: ChatMessage[] = [ { id: 'm1', sender: 'assistant', name: 'Mira Chen', avatarColor: '#147453', content: 'Do you have the new cover proof?', timestamp: new Date(2026, 8, 8, 10, 4) }, { id: 'm2', sender: 'user', name: 'You', avatarColor: '#92503d', content: 'Yes. Is the title easier to read now?', timestamp: new Date(2026, 8, 8, 10, 5), status: 'read' }, { id: 'm3', sender: 'assistant', name: 'Mira Chen', avatarColor: '#147453', content: 'Yes. Keep the larger type on the spine.', timestamp: new Date(2026, 8, 8, 10, 6) }, ] function outgoing(content: string): ChatMessage { return { id: crypto.randomUUID(), sender: 'user', name: 'You', avatarColor: '#92503d', content, timestamp: new Date(), status: 'sent' } } function ProofReview() { const [messages, setMessages] = useState(proofReview) const height = useResponsiveValue({ xs: 488, sm: 420 }, 488) return setMessages(current => [...current, outgoing(content)])} /> } export default function Example() { return () } ``` ### Attachments and delivery ```tsx import { useEffect, useRef, useState } from 'react' import { ChatBox, type ChatMessage } from 'wss3-forge' function outgoing(content: string): ChatMessage { return { id: crypto.randomUUID(), sender: 'user', name: 'You', avatarColor: '#92503d', content, timestamp: new Date(), status: 'sent' } } function FileConversation() { const [messages, setMessages] = useState([ { id: 'file-request', sender: 'user', name: 'You', avatarColor: '#92503d', content: 'Which type should I match?', timestamp: new Date(2026, 8, 8, 11, 12), status: 'delivered' }, { id: 'file-reply', sender: 'assistant', name: 'Mira Chen', avatarColor: '#147453', content: 'Use the wooden letters in this photo.', timestamp: new Date(2026, 8, 8, 11, 14), attachments: [{ name: 'Wooden type.jpg', type: 'image/jpeg', url: '/typeset/wood-letterpress.jpg' }] }, ]) const fileInput = useRef(null) const objectUrls = useRef([]) useEffect(() => { const urls = objectUrls.current return () => urls.forEach(url => URL.revokeObjectURL(url)) }, []) return <> setMessages(current => [...current, outgoing(content)])} onAttach={() => fileInput.current?.click()} /> { const file = event.currentTarget.files?.[0] if (!file || !['image/jpeg', 'image/png', 'image/webp'].includes(file.type)) return const url = URL.createObjectURL(file) objectUrls.current.push(url) const message = { ...outgoing('Here is my sample.'), attachments: [{ name: file.name, type: file.type, url }] } setMessages(current => [...current, message]) event.currentTarget.value = '' }} /> } export default function Example() { return () } ``` ### Open a floating conversation ```tsx import { useState } from 'react' import { MiniChat, type ChatMessage } from 'wss3-forge' function outgoing(content: string): ChatMessage { return { id: crypto.randomUUID(), sender: 'user', name: 'You', avatarColor: '#92503d', content, timestamp: new Date(), status: 'sent' } } function PickupDesk() { const [open, setOpen] = useState(false) const [messages, setMessages] = useState([ { id: 'pickup', sender: 'user', name: 'You', avatarColor: '#92503d', content: 'Can I collect the proofs after lunch?', timestamp: new Date(2026, 8, 8, 11, 30), status: 'read' }, { id: 'address', sender: 'assistant', name: 'Robin Hill', avatarColor: '#147453', content: 'Yes. The side entrance opens at 13:00.', timestamp: new Date(2026, 8, 8, 11, 31) }, ]) return setOpen(current => !current)} messages={messages} onSend={content => setMessages(current => [...current, outgoing(content)])} /> } export default function Example() { return () } ``` ## Root package API ### ChatBox Read a conversation, reply to a message, or share a file in a single thread. Props type: `ChatBoxProps` - `messages` — `ChatMessage[]` - `onSend` — `(content: string) => void` - `onAttach?` — `() => void` - `title?` — `string` - `subtitle?` — `string` - `avatar?` — `ReactNode` - `placeholder?` — `string` - `loading?` — `boolean` - `typing?` — `boolean` - `typingName?` — `string` - `height?` — `number | string` - `headerActions?` — `ReactNode` ### MiniChat Props type: `MiniChatProps` - `messages` — `ChatMessage[]` - `onSend` — `(content: string) => void` - `title?` — `string` Default `'Chat'`. - `avatar?` — `ReactNode` - `open?` — `boolean` - `onToggle?` — `() => void` - `loading?` — `boolean` Default `false`. - `typing?` — `boolean` Default `false`. - `placement?` — `'viewport' | 'inline'` Default `'viewport'`. Options `"inline"`, `"viewport"`. Anchor the launcher to the viewport or lay it out inside its parent. ## Related types ```ts export interface ChatMessage { id: string content: string sender: 'user' | 'assistant' | 'system' timestamp: Date status?: 'sending' | 'sent' | 'delivered' | 'read' avatar?: string avatarColor?: AvatarProps['color'] name?: string attachments?: { name: string; url: string; type: string }[] } export interface AvatarProps extends Omit, 'onClick'> { name?: string src?: string size?: 'xs' | 'sm' | 'md' | 'default' | 'lg' | 'xl' color?: string icon?: ReactNode status?: 'online' | 'offline' | 'away' | 'busy' onClick?: () => void showTooltip?: boolean } interface ChatBoxProps { messages: ChatMessage[] onSend: (content: string) => void onAttach?: () => void title?: string subtitle?: string avatar?: ReactNode placeholder?: string loading?: boolean typing?: boolean typingName?: string height?: number | string headerActions?: ReactNode } interface MiniChatProps { messages: ChatMessage[] onSend: (content: string) => void title?: string avatar?: ReactNode open?: boolean onToggle?: () => void loading?: boolean typing?: boolean /** Anchor the launcher to the viewport or lay it out inside its parent. */ placement?: 'viewport' | 'inline' } ```