# Message

One message in a conversation, with an optional avatar, header and footer.

Source: [message.tsx](https://github.com/jakejarvis/jarvis-ui/blob/main/src/components/ui/message.tsx)

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

export default function MessageConversation() {
  return (
    <div className="flex w-full max-w-md flex-col gap-4">
      <Message align="end">
        <MessageContent>
          <MessageBubble>How do I point www.example.com at my site?</MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageContent>
          <MessageBubble variant="ghost">
            Add the domain under Settings → Domains, then create a CNAME record at your DNS provider
            that points www.example.com to cname.example.net.
          </MessageBubble>
        </MessageContent>
      </Message>
      <Message align="end">
        <MessageContent>
          <MessageBubble>How long does it take to verify?</MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageContent>
          <MessageBubble variant="ghost">
            Usually a few minutes, though some DNS providers take up to an hour.
          </MessageBubble>
        </MessageContent>
      </Message>
    </div>
  );
}
```

## Installation

Add the registry, once per project:

```bash
npx shadcn@latest registry add @jarvis=https://ui.jarv.is/r/{name}.json
```

Then add the component:

```bash
npx shadcn@latest add @jarvis/message
```

## Usage

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

export function Exchange() {
  return (
    <>
      <Message align="end">
        <MessageContent>
          <MessageBubble>When does my plan renew?</MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageContent>
          <MessageBubble variant="ghost">Your plan renews on April 1.</MessageBubble>
        </MessageContent>
      </Message>
    </>
  );
}
```

`align="end"` places a message on the opposite side, for the person's own messages. A bubble is as
wide as its text, up to 80% of the message's width. A `ghost` bubble fills the full width. Long
words and links wrap inside the bubble.

`MessageBubble` preserves the line breaks in its text, so a message shows as the person typed it.
Render an assistant's reply with [Markdown](https://ui.jarv.is/components/markdown.md) in place of the bubble. Markdown
fills the message's width, as a [Trace](https://ui.jarv.is/components/trace.md) or [Tool](https://ui.jarv.is/components/tool.md) does.

## Composition

- `Message`
  - `MessageAvatar`
  - `MessageContent`
    - `MessageHeader`
    - `MessageBubble`
    - `MessageFooter`
- `MessageGroup`
  - `Message`

## Examples

### Variants

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

export default function MessageVariants() {
  return (
    <div className="flex w-full max-w-md flex-col gap-3">
      <Message>
        <MessageContent>
          <MessageBubble>Default, for most messages.</MessageBubble>
        </MessageContent>
      </Message>
      <Message align="end">
        <MessageContent>
          <MessageBubble variant="primary">Primary, to set your own apart.</MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageContent>
          <MessageBubble variant="outline">Outline, for quieter asides.</MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageContent>
          <MessageBubble variant="destructive">
            Destructive, for a message that failed.
          </MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageContent>
          <MessageBubble variant="ghost">
            Ghost has no frame and fills the width, for an assistant’s replies.
          </MessageBubble>
        </MessageContent>
      </Message>
    </div>
  );
}
```

### With an avatar

`MessageAvatar` holds an [Avatar](https://ui.jarv.is/components/avatar.md) and centers it on the message's first line,
however long the message runs. That's the `MessageHeader`, which names the sender above the bubble,
or else the first line of the bubble or reply.

```tsx
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
  Message,
  MessageAvatar,
  MessageBubble,
  MessageContent,
  MessageHeader,
} from "@/components/ui/message";
import { people } from "@/examples/data";

const [tony, ada] = people;

export default function MessageWithAvatar() {
  return (
    <div className="flex w-full max-w-md flex-col gap-4">
      <Message>
        <MessageAvatar>
          <Avatar>
            <AvatarImage src={ada.image} alt="" />
            <AvatarFallback>{ada.initials}</AvatarFallback>
          </Avatar>
        </MessageAvatar>
        <MessageContent>
          <MessageHeader>
            <span className="font-medium text-foreground">{ada.name}</span>
            <time dateTime="2026-03-04T09:41">9:41 AM</time>
          </MessageHeader>
          <MessageBubble>The staging deploy is green. Want me to promote it?</MessageBubble>
        </MessageContent>
      </Message>
      <Message align="end">
        <MessageAvatar>
          <Avatar>
            <AvatarImage src={tony.image} alt="" />
            <AvatarFallback>{tony.initials}</AvatarFallback>
          </Avatar>
        </MessageAvatar>
        <MessageContent>
          <MessageHeader>
            <span className="font-medium text-foreground">{tony.name}</span>
            <time dateTime="2026-03-04T09:42">9:42 AM</time>
          </MessageHeader>
          <MessageBubble variant="primary">Yes, ship it.</MessageBubble>
        </MessageContent>
      </Message>
    </div>
  );
}
```

### Grouped

`MessageGroup` brings one sender's messages closer together. Put the avatar on the last message, and
give the others an empty `MessageAvatar` so their bubbles line up with it.

```tsx
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
  Message,
  MessageAvatar,
  MessageBubble,
  MessageContent,
  MessageGroup,
  MessageHeader,
} from "@/components/ui/message";
import { people } from "@/examples/data";

const sam = people[2];

export default function MessageGrouped() {
  return (
    <MessageGroup className="max-w-md">
      <Message>
        <MessageAvatar />
        <MessageContent>
          <MessageHeader>
            <span className="font-medium text-foreground">{sam.name}</span>
          </MessageHeader>
          <MessageBubble>Quick update on the launch.</MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageAvatar />
        <MessageContent>
          <MessageBubble>The docs are live and the changelog goes out at noon.</MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageAvatar>
          <Avatar>
            <AvatarImage src={sam.image} alt="" />
            <AvatarFallback>{sam.initials}</AvatarFallback>
          </Avatar>
        </MessageAvatar>
        <MessageContent>
          <MessageBubble>Shout if anything looks off.</MessageBubble>
        </MessageContent>
      </Message>
    </MessageGroup>
  );
}
```

### With actions

`MessageFooter` holds actions such as a [Copy Button](https://ui.jarv.is/components/copy-button.md). `ghost` buttons in
the footer render their icons in the muted icon color, and an `xs` or `icon-xs` button at the edge
aligns its icon with the edge of the message. Give icon-only actions an `aria-label`. To keep an
action pending while it runs, return a promise from its `onClick`.

```tsx
"use client";

import { RefreshCwIcon } from "lucide-react";

import { Button } from "@/components/ui/button";
import { CopyButton } from "@/components/ui/copy-button";
import { Message, MessageBubble, MessageContent, MessageFooter } from "@/components/ui/message";

const reply =
  "Add the domain under Settings → Domains, then create a CNAME record at your DNS provider that points www.example.com to cname.example.net. Verification usually finishes within a few minutes.";

async function regenerate() {
  await new Promise((resolve) => setTimeout(resolve, 1500));
}

export default function MessageWithActions() {
  return (
    <div className="flex w-full max-w-md flex-col gap-4">
      <Message align="end">
        <MessageContent>
          <MessageBubble>How do I point www.example.com at my site?</MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageContent>
          <MessageBubble variant="ghost">{reply}</MessageBubble>
          <MessageFooter>
            <CopyButton value={reply} size="icon-xs" aria-label="Copy message" />
            <Button variant="ghost" size="icon-xs" aria-label="Regenerate" onClick={regenerate}>
              <RefreshCwIcon />
            </Button>
          </MessageFooter>
        </MessageContent>
      </Message>
    </div>
  );
}
```

### Failed

When a reply fails, say so in a `destructive` bubble with `role="alert"`, so screen readers announce
it, and offer a way to try again in its footer.

```tsx
"use client";

import { RefreshCwIcon } from "lucide-react";

import { Button } from "@/components/ui/button";
import { Message, MessageBubble, MessageContent, MessageFooter } from "@/components/ui/message";

async function retry() {
  await new Promise((resolve) => setTimeout(resolve, 1500));
}

export default function MessageFailed() {
  return (
    <div className="flex w-full max-w-md flex-col gap-4">
      <Message align="end">
        <MessageContent>
          <MessageBubble>Summarize this week’s incidents.</MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageContent>
          <MessageBubble variant="destructive" role="alert">
            The reply couldn’t be finished. Check your connection and try again.
          </MessageBubble>
          <MessageFooter>
            <Button variant="ghost" size="xs" onClick={retry}>
              <RefreshCwIcon />
              Retry
            </Button>
          </MessageFooter>
        </MessageContent>
      </Message>
    </div>
  );
}
```

### With a trace

An assistant's message can show the steps it took with a [Trace](https://ui.jarv.is/components/trace.md) above its
reply.

```tsx
import { GlobeIcon, SearchIcon } from "lucide-react";

import { Message, MessageBubble, MessageContent } from "@/components/ui/message";
import { Trace, TraceDescription, TraceIcon, TraceLine, TraceTitle } from "@/components/ui/trace";

export default function MessageWithTrace() {
  return (
    <div className="flex w-full max-w-md flex-col gap-4">
      <Message align="end">
        <MessageContent>
          <MessageBubble>Is example.com’s certificate about to expire?</MessageBubble>
        </MessageContent>
      </Message>
      <Message>
        <MessageContent className="gap-3">
          <Trace>
            <TraceLine>
              <TraceIcon>
                <SearchIcon />
              </TraceIcon>
              <TraceTitle>Looked up example.com’s certificate</TraceTitle>
              <TraceDescription>0.4s</TraceDescription>
            </TraceLine>
            <TraceLine>
              <TraceIcon>
                <GlobeIcon />
              </TraceIcon>
              <TraceTitle>Checked the certificate chain</TraceTitle>
            </TraceLine>
          </Trace>
          <MessageBubble variant="ghost">
            No. It was renewed on March 2 and is valid until June 1, so there’s nearly three months
            left. Renewal is automatic.
          </MessageBubble>
        </MessageContent>
      </Message>
    </div>
  );
}
```

## Props

| Part | Extends |
| --- | --- |
| `MessageGroup` | `<div>` |
| `Message` | `<div>` |
| `MessageAvatar` | `<div>` |
| `MessageContent` | `<div>` |
| `MessageHeader` | `<div>` |
| `MessageBubble` | `<div>` |
| `MessageFooter` | `<div>` |

Base UI's [styling](https://base-ui.com/react/handbook/styling) and [composition](https://base-ui.com/react/handbook/composition) guides say more about `className`, `style` and `render`.

### MessageGroup

| Prop         | Type                                                     | Default | Description                                                                                                                                                                               |
| ------------ | -------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `className?` | `string`                                                 | —       | CSS class applied to the element.                                                                                                                                                         |
| `style?`     | `React.CSSProperties`                                    | —       | Style applied to the element.                                                                                                                                                             |
| `render?`    | `ComponentRenderFn<HTMLProps, {}> \| React.ReactElement` | —       | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

### Message

| Prop         | Type                                                     | Default   | Description                                                                                                                                                                               |
| ------------ | -------------------------------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `align?`     | `"end" \| "start"`                                       | `"start"` | Which side of the conversation the message sits on: `end` for the person's own messages.                                                                                                  |
| `className?` | `string`                                                 | —         | CSS class applied to the element.                                                                                                                                                         |
| `style?`     | `React.CSSProperties`                                    | —         | Style applied to the element.                                                                                                                                                             |
| `render?`    | `ComponentRenderFn<HTMLProps, {}> \| React.ReactElement` | —         | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

### MessageAvatar

| Prop         | Type                                                     | Default | Description                                                                                                                                                                               |
| ------------ | -------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `className?` | `string`                                                 | —       | CSS class applied to the element.                                                                                                                                                         |
| `style?`     | `React.CSSProperties`                                    | —       | Style applied to the element.                                                                                                                                                             |
| `render?`    | `ComponentRenderFn<HTMLProps, {}> \| React.ReactElement` | —       | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

### MessageContent

| Prop         | Type                                                     | Default | Description                                                                                                                                                                               |
| ------------ | -------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `className?` | `string`                                                 | —       | CSS class applied to the element.                                                                                                                                                         |
| `style?`     | `React.CSSProperties`                                    | —       | Style applied to the element.                                                                                                                                                             |
| `render?`    | `ComponentRenderFn<HTMLProps, {}> \| React.ReactElement` | —       | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

### MessageHeader

| Prop         | Type                                                     | Default | Description                                                                                                                                                                               |
| ------------ | -------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `className?` | `string`                                                 | —       | CSS class applied to the element.                                                                                                                                                         |
| `style?`     | `React.CSSProperties`                                    | —       | Style applied to the element.                                                                                                                                                             |
| `render?`    | `ComponentRenderFn<HTMLProps, {}> \| React.ReactElement` | —       | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

### MessageBubble

| Prop         | Type                                                              | Default     | Description                                                                                                                                                                               |
| ------------ | ----------------------------------------------------------------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `variant?`   | `"default" \| "destructive" \| "ghost" \| "outline" \| "primary"` | `"default"` |                                                                                                                                                                                           |
| `className?` | `string`                                                          | —           | CSS class applied to the element.                                                                                                                                                         |
| `style?`     | `React.CSSProperties`                                             | —           | Style applied to the element.                                                                                                                                                             |
| `render?`    | `ComponentRenderFn<HTMLProps, {}> \| React.ReactElement`          | —           | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

### MessageFooter

| Prop         | Type                                                     | Default | Description                                                                                                                                                                               |
| ------------ | -------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `className?` | `string`                                                 | —       | CSS class applied to the element.                                                                                                                                                         |
| `style?`     | `React.CSSProperties`                                    | —       | Style applied to the element.                                                                                                                                                             |
| `render?`    | `ComponentRenderFn<HTMLProps, {}> \| React.ReactElement` | —       | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |
