Skip to content
Cynosurev0.5.0
SearchStorybookGitHubnpmColour mode: System. Switch to Light

Components / More

ChatMessage

One message in an assistant chat: the person's at the end, filled with the accent; the assistant's at the start, after its avatar. It holds any content (render markdown into Prose yourself), shows a caret while text streams in, and offers actions such as copy and retry.

Default

When to useLink to "When to use"

  • Assistant chats: a conversation between a person and an assistant, in a side panel or a page.

Use something else when: the message is a system notice in the thread (rate limits, a lost connection): use a Notice; or it is a comment thread between people with names and times: build it from Avatar and Text.

UsageLink to "Usage"

tsx
Copy
import { Avatar, ChatMessage, CopyButton, IconButton, Prose } from "@arshad-shah/cynosure";

<ChatMessage role="user">{question}</ChatMessage>
<ChatMessage
  role="assistant"
  avatar={<Avatar name="Assistant" icon={sparkles} size="sm" decorative />}
  streaming={isStreaming}
  actions={
    <>
      <CopyButton value={answer} size="sm" />
      <IconButton icon={retry} label="Retry" size="sm" variant="ghost" onClick={retryLast} />
    </>
  }
>
  <Prose size="sm">{renderMarkdown(answer)}</Prose>
</ChatMessage>

The person's messages keep their line breaks. Make the avatar decorative: each message is already named by its author.

ErrorsLink to "Errors"

tone="danger" tints the bubble for a message that reports a failure.

Danger tone

Wide contentLink to "Wide content"

A bubble hugs its text and stops at --cy-chat-message-max-width (82%). With wide, it takes the whole row after the avatar, for tables and code.

Wide content takes the whole row

StreamingLink to "Streaming"

While streaming, a caret follows the content and the message is aria-busy, so screen readers wait for the whole reply. The caret is placed after the content: after the last word for plain text, under the last block for Prose.

Streaming shows acaret

The threadLink to "The thread"

A thread is a ScrollArea with follow: it keeps to the end while replies stream in, lets go when the person scrolls up to read, and follows again when they return to the end. onAtBottomChange tells you when to offer a way back (handle.scrollToEnd(), here in the ScrollArea's overlay).

Do not make the thread a live region: it would read every streamed fragment. When an assistant reply finishes, announce it once through useAnnounce (inside an Announcer).

tsx
Copy
const announce = useAnnounce();
const scroller = useRef<ScrollAreaHandle>(null);
const [atBottom, setAtBottom] = useState(true);
// When a reply finishes streaming:
announce(replyText);

<ScrollArea
  label="Conversation"
  follow
  onAtBottomChange={setAtBottom}
  handle={scroller}
  overlay={!atBottom && <Button size="sm" onClick={() => scroller.current?.scrollToEnd()}>Latest</Button>}
>
  {messages.map((m) => (
    <ChatMessage key={m.id} role={m.role} avatar={m.role === "assistant" ? botAvatar : undefined}>
      …
    </ChatMessage>
  ))}
</ScrollArea>
Thread_

PropsLink to "Props"

Every prop of ChatMessage
PropWhat it doesTypeDefault
roleRequired
Who wrote it: the person's messages sit at the end, filled; the assistant's at the start, after avatar."user" | "assistant"
avatar
A picture at the start of the message (end, for the person's), usually a decorative Avatar.ReactNode
tone
"danger" for a message that reports a failure (the request failed, the model refused). Default "neutral"."neutral" | "danger""neutral"
wide
Content that needs the whole row (tables, code): the bubble takes the full width instead of hugging its text.boolean
actions
Controls for the message (CopyButton, a retry IconButton), in a toolbar under it: one Tab stop, arrows between them. Shown while the message is hovered or holds focus, and always on screens without hover.ReactNode
streaming
Text is still arriving: a blinking caret follows the content and the message is busy (aria-busy).boolean
labels
Replace the built-in English names of the message and its actions.Partial<ChatMessageLabels>
className
Added to the root element, for layout.string
style
Layout only. Restyle through --cy-chat-message-* tokens.CSSProperties
children
The content. Render markdown into a Prose yourself; plain text keeps its line breaks.ReactNode

Generated from the type definitions in @arshad-shah/cynosure 0.5.0.

Behaviour and accessibilityLink to "Behaviour and accessibility"

Behaviour and accessibility, table 1
InputResult
TabMoves to the message's actions (one stop per message), then on
Left arrow / Right arrow in the actionsMoves between them (mirrored in right-to-left text)
Pointer over the messageShows the actions
  • Structure: each message is an article named by its author (labels.user, "You"; labels.assistant, "Assistant"), so screen readers can move message by message.
  • Actions: an APG toolbar named "Message actions". With a mouse, they fade in while the message is hovered or holds focus; they keep their space, so nothing moves. Keyboard focus always reveals them.
  • Streaming: aria-busy while text arrives; the caret is hidden from assistive technology.
  • Right to left: the layout mirrors: the person's messages on the left, the assistant's avatar on the right.
  • Reduced motion: the caret stops blinking and stays lit.

TouchLink to "Touch"

Screens without hover show the actions all the time, and IconButtons keep a 44px hit area on coarse pointers.

ThemingLink to "Theming"

Theming, table 2
TokenDefaultWhat it changes
--cy-chat-message-bg--cy-color-surface-raisedThe assistant's bubble
--cy-chat-message-border--cy-color-lineThe assistant's bubble border
--cy-chat-message-user-bg--cy-color-accentThe person's bubble
--cy-chat-message-user-color--cy-color-on-accentThe person's text
--cy-chat-message-radius--cy-radius-cardBubble corners (the one by the avatar is a quarter of it)
--cy-chat-message-padding--cy-space-3 --cy-space-5Inside the bubble
--cy-chat-message-max-width82%The widest a bubble grows (not with wide)
--cy-chat-message-caret--cy-color-accentThe streaming caret
--cy-chat-message-gap--cy-space-3Between the avatar and the message

Prose · ScrollArea · Announcer · Avatar · CopyButton

These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.

Getting started
Theming
Icons
Accessibility
Code editors and highlighters
Using Cynosure without React
Build-tool adapters
Components
Accordion
AppShell
AspectRatio
Breadcrumbs
Calendar
Card
Container
Disclosure
Divider
DocumentViewport
Grid
Inline
PageHeader
PanZoomCanvas
ScrollArea
SideNav
SortableList
SplitPane
Stack
StatusBar
Tabs
TitleBar
Toolbar
Tree
VirtualList
ZoomControls
Announcer
Avatar
Badge
CodeBlock
DescriptionList
EmptyState
ErrorBoundary
Field
Heading
IconTile
Image
Kbd
List
LoadingDots
Notice
ProgressBar
Prose
Skeleton
Stat
Steps
Table
Text
Button
Checkbox
Checklist
ColorField
ColorSwatches
Combobox
CompareSlider
CopyButton
DateField
FileDrop
IconButton
KeyValueEditor
Link
NumberField
Pagination
PathField
RadioGroup
SearchField
SegmentedControl
Select
SelectionFrame
SignaturePad
Slider
Switch
TagInput
TextArea
TextField
ThemeToggle
TimeField
ButtonBase
ChoiceBase
DialogBase
FileInputBase
HiddenValue
Icon
InputBase
LinkBase
LiveRegion
TextAreaBase
ChatMessage
Choice
DataGrid
DeviceFrame
HighlightedText
HotkeyField
ListRow
OptionList
OverlayFrame
QRCode
RowActions
Section
SiteFooter
SiteHeader
SkipLink
SortButton
VisuallyHidden
CommandPalette
ContextMenu
Dialog
FloatingToolbar
HoverCard
Menu
Menubar
Popover
ProgressOverlay
Sheet
Toast
Tooltip
Changelog