Message
One turn. The person's message, and the agent's reply with its actions.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import type { ComponentProps } from "@solidjs/web"
import { createSignal, createEffect, createMemo } from "solid-js"
import { Markdown } from "~/lib/ai-demo-markdown"
import { Bubble, BubbleContent } from "~/components/ui/bubble"
import { Button } from "~/components/ui/button"
import { Message, MessageContent, MessageFooter } from "~/components/ui/message"
import { Tooltip, TooltipContent, TooltipTrigger } from "~/components/ui/tooltip"
const reply = `Install Dialog, then open it from the gear button in \`site-header.tsx\`.
\`\`\`bash
npx solidui-cli@latest add dialog
\`\`\`
Wrap the button in a \`DialogTrigger\` and put the settings form in \`DialogContent\`. Focus moves into the dialog when it opens, and **Esc** closes it.`
export default function MessagePattern() {
const [copied, setCopied] = createSignal(false)
const [feedback, setFeedback] = createSignal<"like" | "dislike" | null>(null)
createEffect(
() => copied(),
(isCopied) => {
if (!isCopied) return
const timer = setTimeout(() => setCopied(false), 2000)
return () => clearTimeout(timer)
}
)
return (
<div class="mx-auto flex w-full max-w-md flex-col gap-8 py-6 text-sm">
<Message align="end">
<MessageContent class="gap-2">
<Bubble align="end" variant="muted">
<BubbleContent>
How do I add a settings dialog? It should open from the gear icon
in the header.
</BubbleContent>
</Bubble>
<MessageFooter>
<MessageAction label="Copy">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<rect width="14" height="14" x="8" y="8" rx="2" ry="2" />
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />
</svg>
</MessageAction>
<MessageAction label="Edit">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z" />
<path d="m15 5 4 4" />
</svg>
</MessageAction>
</MessageFooter>
</MessageContent>
</Message>
<Message>
<MessageContent class="gap-2">
<Markdown>{reply}</Markdown>
<MessageFooter class="px-0">
<MessageAction
label={copied() ? "Copied" : "Copy"}
onClick={() => {
navigator.clipboard.writeText(reply)
setCopied(true)
}}
>
{copied() ? (
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M20 6 9 17l-5-5" />
</svg>
) : (
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<rect width="14" height="14" x="8" y="8" rx="2" ry="2" />
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />
</svg>
)}
</MessageAction>
<MessageAction
label="Good response"
aria-pressed={feedback() === "like"}
onClick={() => setFeedback(feedback() === "like" ? null : "like")}
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class={feedback() === "like" ? "fill-current" : ""}
>
<path d="M15 5.88 14 10h5.83a2 2 0 0 1 1.92 2.56l-2.33 8A2 2 0 0 1 17.5 22H4a2 2 0 0 1-2-2v-8a2 2 0 0 1 2-2h2.76a2 2 0 0 0 1.79-1.11L12 2a3.13 3.13 0 0 1 3 3.88Z" />
<path d="M7 10v12" />
</svg>
</MessageAction>
<MessageAction
label="Bad response"
aria-pressed={feedback() === "dislike"}
onClick={() =>
setFeedback(feedback() === "dislike" ? null : "dislike")
}
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class={feedback() === "dislike" ? "fill-current" : ""}
>
<path d="M9 18.12 10 14H4.17a2 2 0 0 1-1.92-2.56l2.33-8A2 2 0 0 1 6.5 2H20a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2h-2.76a2 2 0 0 0-1.79 1.11L12 22a3.13 3.13 0 0 1-3-3.88Z" />
<path d="M17 14V2" />
</svg>
</MessageAction>
<MessageAction label="Retry">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" />
<path d="M3 3v5h5" />
<path d="M3 12a9 9 0 0 0 9 9 9.75 9.75 0 0 0 6.74-2.74L21 16" />
<path d="M16 16h5v5" />
</svg>
</MessageAction>
</MessageFooter>
</MessageContent>
</Message>
</div>
)
}
function MessageAction(
componentProps: ComponentProps<typeof Button> & { label: string }
) {
const content = createMemo(() => {
const { label, children, ...props } = componentProps
return (
<Tooltip>
<TooltipTrigger
render={(triggerProps) => (
<Button
{...triggerProps}
variant="ghost"
size="icon-sm"
aria-label={label}
{...props}
>
{children}
</Button>
)}
/>
<TooltipContent>
<p>{label}</p>
</TooltipContent>
</Tooltip>
)
})
return <>{content()}</>
}Use Message for one turn in a thread. The person’s text is a Bubble on a Message with align="end". The agent’s reply is markdown on a Message, with actions under it. A thread of these turns is Chat.
Installation
Message is built with the Message, Bubble, and Tooltip components.
Install the required Solid UI dependencies:
Copy and paste the following code into your project.
import type { ComponentProps } from "@solidjs/web"
import { omit } from "solid-js"
import { cn } from "~/lib/utils"
export function MessageGroup(
props: Omit<ComponentProps<"div">, "class"> & { class?: string }
) {
return (
<div
{...props}
data-slot="message-group"
class={cn("gap-2 flex min-w-0 flex-col", props.class)}
/>
)
}
export function Message(
props: Omit<ComponentProps<"div">, "class"> & {
class?: string
align?: "start" | "end"
}
) {
const rest = omit(props, "class", "align")
return (
<div
{...rest}
data-slot="message"
data-align={props.align ?? "start"}
class={cn(
"gap-2 text-sm group/message relative flex w-full min-w-0 data-[align=end]:flex-row-reverse",
props.class
)}
/>
)
}
export function MessageAvatar(
props: Omit<ComponentProps<"div">, "class"> & { class?: string }
) {
return (
<div
{...props}
data-slot="message-avatar"
class={cn(
"min-w-8 group-has-data-[slot=message-footer]/message:-translate-y-8 flex w-fit shrink-0 items-center justify-center self-end overflow-hidden rounded-full bg-muted",
props.class
)}
/>
)
}
export function MessageContent(
props: Omit<ComponentProps<"div">, "class"> & { class?: string }
) {
return (
<div
{...props}
data-slot="message-content"
class={cn(
"gap-2.5 group-data-[align=end]/message:*:data-slot:self-end flex w-full min-w-0 flex-col wrap-break-word",
props.class
)}
/>
)
}
export function MessageHeader(
props: Omit<ComponentProps<"div">, "class"> & { class?: string }
) {
return (
<div
{...props}
data-slot="message-header"
class={cn(
"px-3 text-xs font-medium text-muted-foreground group-has-data-[variant=ghost]/message:px-0 flex max-w-full min-w-0 items-center",
props.class
)}
/>
)
}
export function MessageFooter(
props: Omit<ComponentProps<"div">, "class"> & { class?: string }
) {
return (
<div
{...props}
data-slot="message-footer"
class={cn(
"px-3 text-xs font-medium text-muted-foreground group-has-data-[variant=ghost]/message:px-0 flex max-w-full min-w-0 items-center group-data-[align=end]/message:justify-end",
props.class
)}
/>
)
}Update the import paths to match your project setup.
Usage
import { Bubble, BubbleContent } from "~/components/ui/bubble"
import { Button } from "~/components/ui/button"
import { Message, MessageContent, MessageFooter } from "~/components/ui/message"<Message align="end">
<MessageContent>
<Bubble align="end" variant="muted">
<BubbleContent>How do I add a settings dialog?</BubbleContent>
</Bubble>
</MessageContent>
</Message>
<Message>
<MessageContent class="gap-2">
<p>Install Dialog, then open it from the gear button.</p>
<MessageFooter>
<Button variant="ghost" size="icon-sm" aria-label="Copy">
<CopyIcon />
</Button>
</MessageFooter>
</MessageContent>
</Message>See Message.
Actions
Put actions in MessageFooter. It follows the message alignment, so on the person’s turn the actions sit on the right. Use ghost icon buttons with a tooltip for each.
- Copy writes the reply to the clipboard and swaps to a check for two seconds.
- Good response and Bad response are toggles. Set
aria-pressedso screen readers announce the state. - Retry runs the turn again.
- Edit on the person’s turn puts the text back in the field.
Markdown
The reply streams as markdown. Route fenced code to a Code Block, so code in the reply gets line numbers and a copy button.
AI SDK
Branch on the message role. The person’s turn is the bubble. The agent’s turn is the reply, and regenerate with the message id runs it again.
if (message.role === "user") {
return (
<Message align="end">
<MessageContent>
<Bubble align="end" variant="muted">
<BubbleContent>{text}</BubbleContent>
</Bubble>
</MessageContent>
</Message>
)
}
return (
<Message>
<MessageContent class="gap-2">
<Markdown>{text}</Markdown>
<MessageFooter>
<Button
variant="ghost"
size="icon-sm"
aria-label="Retry"
onClick={() => regenerate({ messageId: message.id })}
>
<RefreshCcwIcon />
</Button>
</MessageFooter>
</MessageContent>
</Message>
)Migrating from AI SDK Elements
If you added Message from AI SDK Elements, your project has a components/ai-elements/message.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Message, Bubble and Tooltip. Every export keeps its name and props, so the code that uses it keeps working.
Migrate components/ai-elements/message.tsx from AI SDK Elements to the Solid UI Message, Bubble and Tooltip components.
Keep every export, with the same name and props, so the code that imports it keeps working. Only change how each part renders.
1. Install the components: npx solidui-cli@latest add message bubble tooltip
2. Read https://www.solid-ui.com/docs/ai-elements/message.md and build Message the same way.
3. Rebuild each part:
- This file exports Message and MessageContent too, so import the Solid UI parts under other names, like MessageRoot and MessageBody.
- Message renders the Solid UI Message, with align="end" when from is "user". Pass from down in a context.
- MessageContent renders the Solid UI MessageContent. For the person's messages, wrap the children in a Bubble with align="end" and variant="muted".
- MessageActions renders a MessageFooter.
- MessageAction renders a Button with variant="ghost" and size="icon-sm" in a Tooltip, with its label as the aria-label.
- Render MessageResponse with your Markdown renderer, and keep the branch parts and MessageToolbar as they are.
4. Remove the imports the file no longer uses.
5. Run the type checker and fix any errors.