# Message

One message of a conversation with an assistant.

One message of a conversation with an assistant. The person's message is a bubble at the end; the assistant's is plain text at the start, with an optional avatar. A header names who wrote it (name, an optional source Chip, the time). Actions sit under it (Copy, Retry, Edit, Good / Bad response), shown on hover and on focus, always on touch; they always take their room, so showing them moves nothing. While streaming it is aria-busy, offers no actions, and shows a shimmering "Thinking…" until its content arrives. Plain text through text, or any content (streaming text, tool chips, a code block, an approval card) as children. chat-composer's threads are made of it.

Kind: component · Category: Agent · Status: beta · Targets: React, Vue, Svelte

## Add

```bash
npx fndn add message
```

It writes the folder `message/` into the project's ui folder (`index.ts`, `message.tsx`, `Message.vue`, `Message.svelte`, `message.recipe.ts`), and what it is built from: [Avatar](/components/avatar.md), [Button](/components/button.md), [Chip](/components/chip.md), [Shimmer](/components/shimmer.md), [Toggle](/components/toggle.md), [Tooltip](/components/tooltip.md), [Code Block](/components/code-block.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Message } from "@/components/ui/message";
```

```vue
import { Message } from "@/components/ui/message";
```

```svelte
import { Message } from "$lib/components/ui/message";
```

## Parts

In the DOM the root carries `data-slot="message"` and each part `data-slot="message-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `article` | `message` |  | A row: the avatar (when there is one) and the main column, 8px apart. aria-busy while streaming. role=user: 56px clear of the start, everything at the end. |
| `avatar` | `span` | `message-avatar` | optional | The real Avatar (20px, the name's initials or the image), marked data-message="avatar", level with the header |
| `main` | `div` | `message-main` |  | Header, body and actions, 4px apart |
| `header` | `div` | `message-header` | optional | A 20px line, xs: name · source · time. Shown when one of them is given |
| `name` | `span` | `message-name` | optional | Medium, ink; cut with an ellipsis and a title when the line is too narrow (free text, not a choice) |
| `source` | `span` | `message-source` | optional | The real Chip (neutral), marked data-message="source": where the answer came from; it gives way and is cut before the time |
| `time` | `time` | `message-time` | optional | ink-2, tabular; datetime from the datetime prop |
| `body` | `div` | `message-body` |  | The content. role=user: the bubble (field fill, 12px radius, 6px × 12px padding). role=assistant: no box |
| `text` | `p` | `message-text` | optional | The text prop: sm, 1.5 line height (1.4 in a bubble), wraps anywhere, keeps line breaks, keeps its own direction; 15px on coarse pointers |
| `status` | `div` | `message-status` | optional | Streaming with nothing yet: role=status, a Shimmer "Thinking…" |
| `actions` | `div` | `message-actions` | optional | role=group named "Message actions": ghost xs square muted Buttons (Copy, Retry, Edit) named by their Tooltips, and Toggles (Good response, Bad response; at most one pressed, its thumb filled). Kept inside the message's box (never pulled past its edge). Not rendered while streaming |
| `announce` | `span` | `message-announce` | optional | Visually hidden role=status beside Copy: says Copied while the button is swapped |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `role` | `"user" \| "assistant"` | `"assistant"` | user: the person's message, a bubble at the end. assistant: plain text at the start. |
| `name` | `string` |  | Who wrote it, in the header ("Maya Chen", "Assistant", or a step's label). |
| `avatar` | `boolean \| string` |  | Shown beside the header: true for the name's initials, or an image URL (the initials show until it loads). |
| `source` | `string` |  | Where it came from, as a neutral Chip after the name (a file, a tool, a page). |
| `time` | `string` |  | When, as shown ("2 min ago", "10:42", "for 2s"). |
| `datetime` | `string` |  | The time as a machine-readable date, for the <time> element. |
| `text` | `string` |  | Plain text. For rich content (streaming text, tool chips, a code block) pass children / the default slot instead. |
| `streaming` | `boolean` | `false` | Still being written: aria-busy, no actions, and a shimmering "Thinking…" while there is no content yet. |
| `actions` | `("copy" \| "retry" \| "edit" \| "good" \| "bad")[]` | `[]` | The actions offered, always shown in this order: copy (puts copyText or text on the clipboard, says Copied for 1.5s), retry, edit, good and bad (Good / Bad response, at most one pressed). |
| `copyText` | `string` |  | What Copy puts on the clipboard; defaults to text (pass it when the content is children). |
| `feedback` | `"good" \| "bad"` |  | Bindable (React: `defaultFeedback` and `onFeedbackChange`; Vue: `v-model:feedback`; Svelte: `bind:feedback`). Good / Bad response, or none (null). While one is chosen the actions stay shown. |
| `labels` | `object` |  | { actions, copy, copied, retry, edit, good, bad, thinking }: the fixed strings, partial. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `action` | (action: 'copy' \| 'retry' \| 'edit' \| 'good' \| 'bad') an action was pressed (copy after it copied) |
| `feedbackChange` | (feedback: 'good' \| 'bad' \| null) |

## Slots

| Slot | Content |
| --- | --- |
| `default` | Rich content instead of text: StreamingText, ToolChips, a CodeBlock, an ApprovalCard |

## States

`hover`, `focus-within`, `streaming`, `copied`, `feedback`

## Keyboard and accessibility

Role: article; its actions a group named "Message actions".

Keyboard:

- Tab reaches each action (the row shows at once when focus enters it); Enter / Space press it
- Good / Bad response are toggle buttons (aria-pressed)

Notes:

- A thread of messages is the host's: give it role=log so a new message is announced once (chat-composer does).
- aria-busy while streaming, so assistive technology waits for the finished text; the Thinking status is role=status.
- Every action is icon-only and named (aria-label) by the text its Tooltip shows; Copy announces Copied through a visually hidden status.
- Author text keeps its own direction (unicode-bidi: plaintext).

## Motion

- actions: fade in over duration.fast when the pointer is over the message (a real pointer only); at once when focus enters them (kbd-instant); always shown on touch; they keep their room, so nothing moves
- copy: the Button's swap (Copy cross-fades into Check)
- thinking: the Shimmer's loop (static under reduced motion)
- arrival: none of its own; a host thread rises new messages in (chat-composer). Nothing plays on first paint

## Tokens

`color.ink`, `color.ink-2`, `color.field`, `text.xs`, `text.sm`, `text.md`, `duration.fast`

## Dependencies

- [Avatar](/components/avatar.md)
- [Button](/components/button.md)
- [Chip](/components/chip.md)
- [Shimmer](/components/shimmer.md)
- [Toggle](/components/toggle.md)
- [Tooltip](/components/tooltip.md)
- [Code Block](/components/code-block.md)

## Examples

### Why did the deploy fail?

React · `deploy-question.tsx`

```tsx
import { Message } from "@/components/ui/message";

export default function DeployQuestion() {
  return (
    <div className="flex w-80 flex-col gap-3">
      <Message role="user" text="Why did the last deploy of billing-api fail?" />
      <Message
        role="assistant" name="Assistant" avatar time="2 min ago" actions={["copy", "retry", "good", "bad"]}
        text="DATABASE_URL is missing from Production. Add it in Settings → Environment variables, then redeploy."
      />
    </div>
  );
}
```

Vue · `deploy-question.vue`

```vue
<script setup lang="ts">
import { Message } from "@/components/ui/message";
</script>

<template>
  <div class="flex w-80 flex-col gap-3">
    <Message role="user" text="Why did the last deploy of billing-api fail?" />
    <Message
      role="assistant" name="Assistant" avatar time="2 min ago" :actions="['copy', 'retry', 'good', 'bad']"
      text="DATABASE_URL is missing from Production. Add it in Settings → Environment variables, then redeploy."
    />
  </div>
</template>
```

Svelte · `deploy-question.svelte`

```svelte
<script lang="ts">
  import { Message } from "$lib/components/ui/message";
</script>

<div class="flex w-80 flex-col gap-3">
  <Message role="user" text="Why did the last deploy of billing-api fail?" />
  <Message
    role="assistant" name="Assistant" avatar time="2 min ago" actions={["copy", "retry", "good", "bad"]}
    text="DATABASE_URL is missing from Production. Add it in Settings → Environment variables, then redeploy."
  />
</div>
```

### An answer with its tool calls

React · `answer-with-tool-calls.tsx`

```tsx
import { Message } from "@/components/ui/message";
import { ToolChips, type ToolStep } from "@/components/ui/tool-chips";

const steps: ToolStep[] = [
  { icon: "read", label: "Read", chip: "dpl_7f3a9c/build.log", mono: true, detail: [{ text: "Step 3 of 5 exited with code 1" }] },
  { icon: "search", label: "Search code", chip: "DATABASE_URL", mono: true, detail: [{ text: "2 matches in billing-api" }] },
];
const ANSWER = "The build reads DATABASE_URL at start-up and Production does not define it. Add it, then redeploy.";

/* rich content goes in as children; Copy takes copyText */
export default function AnswerWithToolCalls() {
  return (
    <div className="w-80">
      <Message role="assistant" name="Assistant" avatar time="just now" actions={["copy", "good", "bad"]} copyText={ANSWER}>
        <div className="flex flex-col gap-2">
          <ToolChips steps={steps} labels={{ header: "2 tool calls" }} />
          <p className="text-sm leading-[1.5]">{ANSWER}</p>
        </div>
      </Message>
    </div>
  );
}
```

Vue · `answer-with-tool-calls.vue`

```vue
<script setup lang="ts">
import { Message } from "@/components/ui/message";
import { ToolChips, type ToolStep } from "@/components/ui/tool-chips";

const steps: ToolStep[] = [
  { icon: "read", label: "Read", chip: "dpl_7f3a9c/build.log", mono: true, detail: [{ text: "Step 3 of 5 exited with code 1" }] },
  { icon: "search", label: "Search code", chip: "DATABASE_URL", mono: true, detail: [{ text: "2 matches in billing-api" }] },
];
const ANSWER = "The build reads DATABASE_URL at start-up and Production does not define it. Add it, then redeploy.";
</script>

<!-- rich content goes in the default slot; Copy takes copy-text -->
<template>
  <div class="w-80">
    <Message role="assistant" name="Assistant" avatar time="just now" :actions="['copy', 'good', 'bad']" :copy-text="ANSWER">
      <div class="flex flex-col gap-2">
        <ToolChips :steps="steps" :labels="{ header: '2 tool calls' }" />
        <p class="text-sm leading-[1.5]">{{ ANSWER }}</p>
      </div>
    </Message>
  </div>
</template>
```

Svelte · `answer-with-tool-calls.svelte`

```svelte
<script lang="ts">
  import { Message } from "$lib/components/ui/message";
  import { ToolChips, type ToolStep } from "$lib/components/ui/tool-chips";

  const steps: ToolStep[] = [
    { icon: "read", label: "Read", chip: "dpl_7f3a9c/build.log", mono: true, detail: [{ text: "Step 3 of 5 exited with code 1" }] },
    { icon: "search", label: "Search code", chip: "DATABASE_URL", mono: true, detail: [{ text: "2 matches in billing-api" }] },
  ];
  const ANSWER = "The build reads DATABASE_URL at start-up and Production does not define it. Add it, then redeploy.";
</script>

<!-- rich content goes in as children; Copy takes copyText -->
<div class="w-80">
  <Message role="assistant" name="Assistant" avatar time="just now" actions={["copy", "good", "bad"]} copyText={ANSWER}>
    <div class="flex flex-col gap-2">
      <ToolChips {steps} labels={{ header: "2 tool calls" }} />
      <p class="text-sm leading-[1.5]">{ANSWER}</p>
    </div>
  </Message>
</div>
```

### Asks before running the tests

React · `asks-before-running.tsx`

```tsx
import { ApprovalCard, type ApprovalAction } from "@/components/ui/approval-card";
import { Message } from "@/components/ui/message";

const action: ApprovalAction = {
  title: "Run the tests in billing-api?",
  command: "pnpm --filter billing-api test",
  details: [{ label: "Branch", value: "fix/rate-limit", mono: true }],
};

/* the agent stops to ask: an approval card inside its message */
export default function AsksBeforeRunning() {
  return (
    <div className="w-80">
      <Message role="assistant" name="Assistant" avatar>
        <div className="flex flex-col gap-2">
          <p className="text-sm leading-[1.5]">I reset the window once a minute has passed. Shall I check it?</p>
          <ApprovalCard action={action} alwaysAllow />
        </div>
      </Message>
    </div>
  );
}
```

Vue · `asks-before-running.vue`

```vue
<script setup lang="ts">
import { ApprovalCard, type ApprovalAction } from "@/components/ui/approval-card";
import { Message } from "@/components/ui/message";

const action: ApprovalAction = {
  title: "Run the tests in billing-api?",
  command: "pnpm --filter billing-api test",
  details: [{ label: "Branch", value: "fix/rate-limit", mono: true }],
};
</script>

<!-- the agent stops to ask: an approval card inside its message -->
<template>
  <div class="w-80">
    <Message role="assistant" name="Assistant" avatar>
      <div class="flex flex-col gap-2">
        <p class="text-sm leading-[1.5]">I reset the window once a minute has passed. Shall I check it?</p>
        <ApprovalCard :action="action" always-allow />
      </div>
    </Message>
  </div>
</template>
```

Svelte · `asks-before-running.svelte`

```svelte
<script lang="ts">
  import { ApprovalCard, type ApprovalAction } from "$lib/components/ui/approval-card";
  import { Message } from "$lib/components/ui/message";

  const action: ApprovalAction = {
    title: "Run the tests in billing-api?",
    command: "pnpm --filter billing-api test",
    details: [{ label: "Branch", value: "fix/rate-limit", mono: true }],
  };
</script>

<!-- the agent stops to ask: an approval card inside its message -->
<div class="w-80">
  <Message role="assistant" name="Assistant" avatar>
    <div class="flex flex-col gap-2">
      <p class="text-sm leading-[1.5]">I reset the window once a minute has passed. Shall I check it?</p>
      <ApprovalCard {action} alwaysAllow />
    </div>
  </Message>
</div>
```

### Edit your question

React · `edit-your-question.tsx`

```tsx
import { useState } from "react";
import { Message } from "@/components/ui/message";
import { Textarea } from "@/components/ui/textarea";

const QUESTION = "Which invoices failed to send in November?";

/* Edit turns the bubble into a field; Enter keeps the new text */
export default function EditYourQuestion() {
  const [text, setText] = useState(QUESTION);
  const [editing, setEditing] = useState(false);
  return (
    <div className="flex w-80 flex-col items-end">
      {editing ? (
        <Textarea
          autoFocus aria-label="Edit your message" defaultValue={text} rows={2}
          onKeyDown={(e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); setText(e.currentTarget.value.trim() || text); setEditing(false); } if (e.key === "Escape") setEditing(false); }}
        />
      ) : (
        <Message role="user" name="Maya Chen" time="10:42" text={text} actions={["copy", "edit"]} onAction={(a) => a === "edit" && setEditing(true)} />
      )}
    </div>
  );
}
```

Vue · `edit-your-question.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Message } from "@/components/ui/message";
import { Textarea } from "@/components/ui/textarea";

const text = ref("Which invoices failed to send in November?");
const editing = ref(false);
function onKey(e: KeyboardEvent) {
  const field = e.target as HTMLTextAreaElement;
  if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); text.value = field.value.trim() || text.value; editing.value = false; }
  if (e.key === "Escape") editing.value = false;
}
</script>

<!-- Edit turns the bubble into a field; Enter keeps the new text -->
<template>
  <div class="flex w-80 flex-col items-end">
    <Textarea v-if="editing" autofocus aria-label="Edit your message" :value="text" :rows="2" @keydown="onKey" />
    <Message v-else role="user" name="Maya Chen" time="10:42" :text="text" :actions="['copy', 'edit']" @action="(a) => a === 'edit' && (editing = true)" />
  </div>
</template>
```

Svelte · `edit-your-question.svelte`

```svelte
<script lang="ts">
  import { Message } from "$lib/components/ui/message";
  import { Textarea } from "$lib/components/ui/textarea";

  let text = $state("Which invoices failed to send in November?");
  let editing = $state(false);
  function onKey(e: KeyboardEvent & { currentTarget: HTMLTextAreaElement }) {
    if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); text = e.currentTarget.value.trim() || text; editing = false; }
    if (e.key === "Escape") editing = false;
  }
</script>

<!-- Edit turns the bubble into a field; Enter keeps the new text -->
<div class="flex w-80 flex-col items-end">
  {#if editing}
    <!-- svelte-ignore a11y_autofocus -->
    <Textarea autofocus aria-label="Edit your message" value={text} rows={2} onkeydown={onKey} />
  {:else}
    <Message role="user" name="Maya Chen" time="10:42" {text} actions={["copy", "edit"]} onaction={(a) => a === "edit" && (editing = true)} />
  {/if}
</div>
```

### Thinking before the first word

React · `thinking.tsx`

```tsx
import { Message } from "@/components/ui/message";

/* streaming with nothing yet: aria-busy and a shimmering "Thinking…"; actions wait for the answer */
export default function Thinking() {
  return (
    <div className="w-80">
      <Message role="assistant" name="Assistant" avatar streaming actions={["copy", "retry"]} />
    </div>
  );
}
```

Vue · `thinking.vue`

```vue
<script setup lang="ts">
import { Message } from "@/components/ui/message";
</script>

<!-- streaming with nothing yet: aria-busy and a shimmering "Thinking…"; actions wait for the answer -->
<template>
  <div class="w-80">
    <Message role="assistant" name="Assistant" avatar streaming :actions="['copy', 'retry']" />
  </div>
</template>
```

Svelte · `thinking.svelte`

```svelte
<script lang="ts">
  import { Message } from "$lib/components/ui/message";
</script>

<!-- streaming with nothing yet: aria-busy and a shimmering "Thinking…"; actions wait for the answer -->
<div class="w-80">
  <Message role="assistant" name="Assistant" avatar streaming actions={["copy", "retry"]} />
</div>
```

## Recipe

`message.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* message — one message of a conversation with an assistant: the person's, in a bubble at the
 * end, or the assistant's, as plain text at the start with an optional avatar. A header names who
 * wrote it (name, an optional source Chip, the time); actions (Copy, Retry, Edit, Good / Bad
 * response) sit under it, shown on hover and on focus, always on touch.
 *
 * Composed: Avatar (20px, beside the header and the text), Chip (the source), Shimmer (the
 * "Thinking" status of a streaming message with nothing yet), Button (Copy, Retry, Edit: ghost xs
 * square muted, named by their Tooltips) and Toggle (Good / Bad response, which stay pressed).
 *
 * Motion. The actions fade in on hover over duration.fast (a real pointer only, group-hover is
 * gated); from the keyboard they are there at once (kbd-instant); they always take their room, so
 * showing them moves nothing. Copy cross-fades into Copied (the Button's swap). Arrivals are the
 * host thread's (chat-composer rises a new message in); nothing plays on first paint. Author text
 * keeps its own direction (plaintext); on a coarse pointer it reads at 16px. */

const PLAIN = "[unicode-bidi:plaintext]";

export const message = tv({
  slots: {
    base: "group/message flex w-full min-w-0 gap-2 text-ink",
    /* the Avatar, beside the header (the header's 20px line) */
    avatar: "shrink-0 mt-px",
    main: "flex min-w-0 flex-1 flex-col gap-1",
    header: "flex min-w-0 items-center gap-1.5 h-5 text-xs leading-none whitespace-nowrap",
    name: ["min-w-0 overflow-hidden text-ellipsis font-medium text-ink", PLAIN],
    /* the source is a Chip: it gives way first (down to 48px, cut in the end), then the name; the
     * time is never cut */
    source: "min-w-12 shrink-[100] overflow-hidden text-ellipsis whitespace-nowrap",
    time: ["shrink-0 text-ink-2 tabular-nums", PLAIN],
    body: "min-w-0",
    text: ["m-0 text-sm leading-[1.5] wrap-anywhere whitespace-pre-line pointer-coarse:text-md", PLAIN],
    status: "flex h-[21px] items-center text-sm",
    /* shown on hover and focus, always on touch; always takes its room */
    actions: [
      "kbd-instant flex items-center gap-0.5 opacity-0 motion-opacity-fast",
      "group-hover/message:opacity-100 group-focus-within/message:opacity-100",
      "pointer-coarse:opacity-100 [@media(hover:none)]:opacity-100",
    ],
  },
  variants: {
    role: {
      /* the person's message: a bubble at the end, 56px clear of the start */
      user: {
        base: "ps-14",
        main: "items-end",
        header: "justify-end",
        body: "max-w-full py-1.5 px-3 rounded-[12px] bg-field [&>[data-slot=message-text]]:leading-[1.4]",
      },
      assistant: {},
    },
    /* a chosen Good / Bad response keeps the actions shown, so the choice stays visible */
    kept: { true: { actions: "opacity-100" } },
  },
  defaultVariants: { role: "assistant", kept: false },
});

export type MessageRecipeProps = VariantProps<typeof message>;
```
