← All proposals

Chat Message

ChatMessage, with sender: user or agent.

Net-new componentOpen in Storybook

proposals/ChatMessage/ChatMessage.mdx43 lines · View on GitHub
import { Canvas, Controls, Meta, Title } from '@storybook/addon-docs/blocks';

import * as ChatMessageStories from './ChatMessage.stories';

<Meta of={ChatMessageStories} name='Usage' />

<Title />

## Purpose

One turn in a conversation, from the user or from an agent.

## Usage

Use it for each message in a chat or agent screen.

- `sender="user"` is a filled bubble on the right that hugs its text, up to 80% of the width.
- `sender="agent"` has no container and fills the width. Put anything in it: text, lists, a Thinking State, Chain of Thought.
- `ChatMessage` does not style the content inside an agent message. Render markdown in your own renderer: map fenced code to the PDS `CodeBlock`. Inline code needs no extra work: PDS already styles `<code>` inside paragraphs, spans and list items.
- `label` is required. It is read by screen readers before the message (for example "You" or "Zappy") and is not shown.
- `ChatMessage` does not space itself from its neighbors. Wrap the messages in `ChatMessageList`, which owns the gap between turns (20px, `--pds-spacing-l`). `ChatMessageList` has no story of its own; it appears in the Conversation story.

Do not use it for:

- A notice or status from the system or an agent (use `SectionMessage`), or a toast.
- Anything that is not a turn in a conversation, such as a comment thread or a log of events.

## Accessibility

- `label` is read by screen readers before the message (for example "You" or "Zappy") and is not shown. It is required: without it the message is read with no sender. Do not repeat the sender in the visible text.
- The message has no role, no interactive elements and no focus styles of its own. Interactive content inside an agent message (buttons, links, a Thinking State, Chain of Thought) brings its own keyboard behavior and labels.
- `ChatMessageList` has no role. Add `role` and `aria-label` on the list in your screen. `role='log'` is suggested for a streaming conversation, but what is announced while an agent writes is not designed yet and has not been tested.
- Not checked: the a11y panel, and contrast of the user bubble in light and dark.

## Implementation

See the table below for the available props. The other stories show the user bubble and a conversation.

<hr />

<Canvas of={ChatMessageStories.Conversation} />
<Controls of={ChatMessageStories.Agent} />