AI and chat
Chat
Building blocks for chat and assistant interfaces: a transcript that follows streaming replies without taking over the scroll position, messages, a prompt input with Stop, citations and tool calls. They render a conversation; any model or SDK can drive them.
import '@reactloom/chat/styles.css';
import { MessageList, Message, PromptInput, Citations, CitationRef, ToolCall } from '@reactloom/chat';Examples
Streaming conversation
Send a message: the reply streams word by word and is announced once, when complete. Scroll up while it streams and the list stays put.
import { useEffect, useRef, useState } from 'react';
import { Message, MessageList, PromptInput, type MessageStatus } from '@reactloom/chat';
import { Avatar, IconButton } from '@reactloom/ui';
import { CopyIcon } from '@reactloom/icons';
interface Turn {
id: number;
sender: 'user' | 'assistant';
text: string;
status: MessageStatus;
}
const answer =
'A live region announces changes without moving focus. In a chat, announce each reply once it is complete, so screen-reader users are not interrupted by every streamed word.';
export default function Example() {
const [turns, setTurns] = useState<Turn[]>([
{ id: 1, sender: 'user', text: 'What is a live region?', status: 'complete' },
{ id: 2, sender: 'assistant', text: answer, status: 'complete' },
]);
const timer = useRef<ReturnType<typeof setInterval>>(undefined);
const streaming = turns.some((t) => t.status === 'streaming');
useEffect(() => () => clearInterval(timer.current), []);
const stop = () => {
clearInterval(timer.current);
setTurns((all) => all.map((t) => (t.status === 'streaming' ? { ...t, status: 'complete' } : t)));
};
// Stand-in for a model: streams the same answer word by word.
const send = (text: string) => {
const id = Date.now();
setTurns((all) => [...all, { id, sender: 'user', text, status: 'complete' }, { id: id + 1, sender: 'assistant', text: '', status: 'streaming' }]);
const words = answer.split(' ');
let i = 0;
clearInterval(timer.current);
timer.current = setInterval(() => {
i += 1;
setTurns((all) => all.map((t) => (t.id === id + 1 ? { ...t, text: words.slice(0, i).join(' ') } : t)));
if (i >= words.length) stop();
}, 90);
};
return (
<div style={{ display: 'flex', flexDirection: 'column', gap: 12, width: '100%', maxWidth: 640, height: 460 }}>
<MessageList style={{ flex: 1, border: '1px solid var(--pl-color-border-default)', borderRadius: 8 }}>
{turns.map((t) => (
<Message
key={t.id}
sender={t.sender}
status={t.status}
avatar={t.sender === 'assistant' ? <Avatar name="Loom" alt="" size="sm" /> : undefined}
actions={
t.sender === 'assistant' ? (
<IconButton icon={<CopyIcon />} label="Copy reply" size="sm" variant="ghost" onClick={() => void navigator.clipboard?.writeText(t.text)} />
) : undefined
}
>
{t.text}
</Message>
))}
</MessageList>
<PromptInput onSubmit={send} streaming={streaming} onStop={stop} maxLength={1000} />
</div>
);
}Sources, tool calls and errors
ToolCall is collapsed by default; CitationRef markers link to the numbered Citations list.
import { CitationRef, Citations, Message, MessageList, ToolCall } from '@reactloom/chat';
export default function Example() {
return (
<MessageList style={{ width: '100%', maxWidth: 640 }} messages={{ label: 'Weather conversation' }}>
<Message sender="user">Will I need an umbrella in Oslo today?</Message>
<Message author="Loom">
<ToolCall name="get_forecast" input={{ city: 'Oslo', hours: 12 }} output={{ rain_probability: 0.7, temperature_c: 12 }} />
<p>
Probably: there is a 70% chance of rain this afternoon
<CitationRef index={1} href="#chat-source-met" title="MET Norway forecast" />.
</p>
<Citations idPrefix="chat-source-" sources={[{ id: 'met', title: 'MET Norway forecast', href: 'https://www.met.no/en', snippet: 'Rain likely from 14:00, 12°C.' }]} />
</Message>
<Message author="Loom" status="error" error="The forecast service did not answer." onRetry={() => {}} />
</MessageList>
);
}API reference
| Prop | Type | Default |
|---|---|---|
childrenMessage elements, oldest first. | ReactNode | — |
followOutputKeep the newest message in view while the reader is at the bottom. | boolean | true |
bottomThresholdPixels from the bottom that still count as “at the bottom”. | number | 32 |
messagesTranscript name and “Jump to latest”. | Partial<MessageListMessages> | — |
| Prop | Type | Default |
|---|---|---|
senderAlignment and default author name. | 'user' | 'assistant' | 'system' | 'assistant' |
author · avatar · timeHeader content; author is also the message’s name. | ReactNode · ReactNode · Date | — |
statusStreaming shows a typing indicator and marks the message busy. | 'complete' | 'streaming' | 'error' | 'complete' |
error · onRetryError text and a Retry button. | ReactNode · () => void | — |
actionsButtons under the message (copy, feedback). | ReactNode | — |
announceAnnounce the message when it completes. | boolean | true except for user messages |
| Prop | Type | Default |
|---|---|---|
onSubmitrequiredCalled with the trimmed text; the input then clears. | (value: string) => void | — |
value · defaultValue · onValueChangeControlled or uncontrolled text. | string | — |
streaming · onStopSend becomes Stop; Enter no longer sends. | boolean · () => void | — |
maxLengthShows a count and blocks sending above it. | number | — |
onAttach · accept · attachmentsAttach button, file types and the attachments shown above the text. | (files) => void · string · ReactNode | — |
toolbarExtra controls before the send button. | ReactNode | — |
maxRowsRows before the text area scrolls. | number | 8 |
submitOnEnterEnter sends; with false, Ctrl/⌘ + Enter sends. | boolean | true |
| Prop | Type | Default |
|---|---|---|
Citations sourcesrequiredNumbered sources; the host of href is shown when source is omitted. | { id, title, href?, source?, snippet? }[] | — |
Citations idPrefix · linkPropsItem ids for CitationRef links; props for every link (target, rel). | string · anchor props | 'source-' |
CitationRef index · href · titleAn inline numbered link, named “Source 1: Title”. | number · string · string | — |
ToolCall name · statusHeader of the collapsible block. | string · running | complete | error | — |
ToolCall input · outputObjects are shown as formatted JSON. | unknown | — |
Customization
Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.
MessageList
Slot class names
Add classes to inner parts with classNames (className is shorthand for root): root, viewport, jump, announcer.
CSS variables
| Variable | Default |
|---|---|
--pl-chat-gap | var(--pl-space-4) |
--pl-chat-padding | var(--pl-space-4) |
Theme tokens it reads
--pl-focus-ring-color --pl-focus-ring-width --pl-shadow-md
Message
Slot class names
Add classes to inner parts with classNames (className is shorthand for root): root, avatar, body, header, author, time, content, typing, error, actions.
CSS variables
| Variable | Default |
|---|---|
--pl-message-max-width | 42rem |
--pl-message-user-bg | var(--pl-color-accent-subtle) |
--pl-message-user-fg | inherit |
State attributes
Target these in your CSS to style states without depending on class names.
| Attribute | Values |
|---|---|
[data-sender] | user · assistant · system |
Theme tokens it reads
--pl-color-accent-subtle --pl-color-danger-fg --pl-color-fg-muted --pl-font-size-sm --pl-font-size-xs --pl-radius-full --pl-radius-lg
PromptInput
Slot class names
Add classes to inner parts with classNames (className is shorthand for root): root, attachments, textarea, toolbar, attach, counter, submit.
CSS variables
| Variable | Default |
|---|---|
--pl-prompt-radius | var(--pl-radius-lg) |
State attributes
Target these in your CSS to style states without depending on class names.
| Attribute | Values |
|---|---|
[data-disabled] | present when on |
[data-over] | present when on |
Theme tokens it reads
--pl-color-bg-surface --pl-color-border-default --pl-color-danger-fg --pl-color-fg-muted --pl-focus-ring-color --pl-font-size-xs --pl-radius-lg
Citations
Slot class names
Add classes to inner parts with classNames (className is shorthand for root): root, label, list, item, number, title, source, snippet.
Theme tokens it reads
--pl-color-accent-fg --pl-color-bg-subtle --pl-color-border-default --pl-color-fg-muted --pl-focus-ring-color --pl-focus-ring-width --pl-font-size-sm --pl-font-size-xs --pl-radius-full --pl-radius-md
CitationRef
Theme tokens it reads
--pl-color-accent-subtle --pl-color-bg-subtle --pl-color-fg-muted --pl-focus-ring-color --pl-focus-ring-width --pl-font-size-xs --pl-radius-full
ToolCall
Slot class names
Add classes to inner parts with classNames (className is shorthand for root): root, trigger, status, content, section, code.
CSS variables
| Variable | Default |
|---|---|
--pl-motion-duration-fast | 120ms |
--pl-tool-call-max-height | 16rem |
State attributes
Target these in your CSS to style states without depending on class names.
| Attribute | Values |
|---|---|
[data-status] | running · complete · error |
Theme tokens it reads
--pl-color-bg-subtle --pl-color-border-default --pl-color-danger-fg --pl-color-fg-muted --pl-color-success-fg --pl-focus-ring-color --pl-focus-ring-width --pl-font-size-sm --pl-font-size-xs --pl-radius-md --pl-radius-sm
Accessibility
- The transcript is a named
role="log"that is not live itself: streamed tokens are not read out one by one. Each completed reply is announced once through a status region; history is not read on load. - Each message is an
articlenamed by its author; a streaming message isaria-busy. - Scrolling up is never interrupted. “Jump to latest” appears instead and moves focus back to the transcript.
- The prompt never sends text still being composed with an input method (Japanese, Chinese, Korean).
| Key | Action |
|---|---|
| Enter | Send (in the prompt) |
| Shift + Enter | New line |
| Tab | Move to attach, Send or Stop |
Usage guidelines
Do
- Show who is speaking, and keep Stop reachable while a reply streams.
- Render model output as text or sanitised markdown.
Don’t
- Don’t move focus to new replies; announce them instead.