Message
Slant / People & chat
One chat message from the user or the assistant.
Demo
Usage
import { Button, CodeBlock, Message, Thread } from '@tuttn/slant'
// author lines the bubble up and colours it. authorLabel replaces the hidden
// "Assistant: " prefix for one message. typing shows the dots instead of text.
<Thread label="Conversation">
<Message author="assistant">Hi. What should the next session pick up?</Message>
<Message author="user">Persist checkpoints for <code>TASK-24</code>.</Message>
<Message author="assistant" authorLabel="Review bot: ">Two checks failed.</Message>
{typing && <Message author="assistant" typing />}
</Thread>
// Under an assistant's answer, a row of actions: copy (the visible text, or
// copyText), retry, a good and a bad rating, then your own.
<Message author="assistant" copyable copyText={markdown} onRetry={regenerate}
rating={rating} onRatingChange={setRating}
actions={<Button variant="ghost" size="sm" onClick={share}>Share</Button>}>
The next session starts from step 3.
</Message>
// The bubble is a <div>, so an answer can hold blocks: paragraphs, a code block.
<Message author="assistant" copyable defaultRating={null}>
<p>Run the migration first:</p>
<CodeBlock language="sh" code="npm run db:migrate" />
</Message>Props
| Prop | Type | Default | Description |
|---|---|---|---|
author | 'user' | 'assistant' | — | Who wrote it: aligns and colours the bubble. |
authorLabel | string | messages.you or messages.assistant ("You: ", "Assistant: ") | The prefix read to screen readers before the text. |
typing | boolean | — | Shows the typing dots, named by `messages.youTyping` or `messages.assistantTyping`, instead of the children. A typing message has no actions. |
copyable | boolean | — | Shows a Copy button under an assistant answer. It copies `copyText`, or else the text the reader sees in the bubble (without the hidden author prefix), shows a check for a moment and announces `messages.copied`. |
copyText | string | — | The text the Copy button copies, when it is not the answer's visible text (markdown source, say). |
onRetry | () => void | — | Shows a Retry button under an assistant answer, which calls this. |
rating | MessageRating | — | The reader's rating of an assistant answer. Pass it with onRatingChange to control the rate buttons; null is no rating. |
defaultRating | MessageRating | null | The rating it starts with, when uncontrolled. Passing it also shows the rate buttons. |
onRatingChange | (rating: MessageRating) => void | — | Shows the rate buttons (good and bad response) under an assistant answer, and is called with the new rating; pressing the pressed one again clears it to null. |
actions | ReactNode | — | More actions, after the built-in ones, in the same row under the answer: small ghost Buttons, say. |
…rest | HTMLAttributes | — | className, style, the ref and other attributes reach the root <div>, which holds the bubble (sl-message-bubble) and, under an answer, the actions row (sl-message-actions). |
Keyboard
| Tab | moves through an answer’s actions: Copy, Retry, Good response, Bad response, then your own |
| Enter / Space | presses the focused action; on a rate button, presses it, or clears the rating if it was already pressed |
Accessibility
- Each message starts with a visually hidden prefix,
messages.youormessages.assistant(“You: ”, “Assistant: ”), so who said it is read aloud, not only shown by side and colour. - A typing message is the typing dots as an image named
messages.youTypingormessages.assistantTyping(“Assistant is typing”). It has no actions. - The actions under an answer are a
role="group"namedmessages.messageActions(“Message actions”). Each is an icon button with its own name:messages.copy,messages.retry,messages.goodResponse,messages.badResponse. - The rate buttons are toggle buttons with
aria-pressed: at most one is pressed, and pressing it again clears the rating to null. - Copy says “Copied” through the root’s polite announcer once the clipboard has the text, and shows a check for a moment; if the browser refuses the write, nothing is said. It copies what the reader sees, leaving out the hidden author prefix and other screen-reader-only or aria-hidden text, unless you pass
copyText. - Messages rise in with a short animation, which stops when the reader prefers reduced motion.
- Rename the prefixes and the action names for one message with
authorLabel, or for all of them on a<Slant messages>root.
The root is a <div> holding the bubble, so a message can carry blocks: paragraphs, a list, a code block. Slant styles only its own parts, never the content you pass in, so give plain elements your own styles or use Slant’s Text and Stack, as the demo does. The bubble shrinks to its content up to 85% of the thread; the actions sit under it at the start.
A message lines up by author only inside a Thread; on its own it sits at the start. Actions show only under an assistant’s answer, never under the reader’s own messages or while typing.