import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { bubbleClass, bubbleContentClass } from '#components/ui/bubble.ts';
import { buttonClass } from '#components/ui/button.ts';
import {
codeBlockClass, codeBlockHeaderClass, codeBlockTitleClass, codeBlockActionsClass,
codeBlockContentClass, codeBlockCodeClass, codeBlockLines,
} from '#components/ui/code-block.ts';
import { messageClass, messageContentClass, messageFooterClass } from '#components/ui/message.ts';
import {
CopyIcon, PencilIcon, RefreshCcwIcon, ThumbsDownIcon, ThumbsUpIcon,
} from '#lib/ui/lucide.ts';
const inlineCode = 'rounded-sm bg-muted px-1 py-0.5 font-mono text-[0.9em]';
const command = 'npx @webjsdev/ui add dialog';
// A ghost icon button with its label in a tooltip, as shadcn's MessageAction.
function MessageAction(label: string, icon: unknown) {
return html`
<ui-tooltip>
<ui-tooltip-trigger>
<button type="button" data-slot="button" data-variant="ghost" data-size="icon-sm" aria-label=${label} class=${buttonClass({ variant: 'ghost', size: 'icon-sm' })}>
${icon}
</button>
</ui-tooltip-trigger>
<ui-tooltip-content><p>${label}</p></ui-tooltip-content>
</ui-tooltip>
`;
}
// The settled turn. The kit does not parse Markdown, so the reply is written
// out the way a Markdown renderer would emit it, with the fence as a code
// block titled by its language.
export default function MessagePattern() {
return html`
<div class="mx-auto flex w-full max-w-md flex-col gap-8 py-6 text-sm">
<div data-slot="message" data-align="end" class=${messageClass()}>
<div data-slot="message-content" class=${cn(messageContentClass(), 'gap-2')}>
<div data-slot="bubble" data-variant="muted" data-align="end" class=${bubbleClass({ variant: 'muted' })}>
<div data-slot="bubble-content" class=${bubbleContentClass()}>
How do I add a settings dialog? It should open from the gear icon
in the header.
</div>
</div>
<div data-slot="message-footer" class=${messageFooterClass()}>
${MessageAction('Copy', CopyIcon())}
${MessageAction('Edit', PencilIcon())}
</div>
</div>
</div>
<div data-slot="message" data-align="start" class=${messageClass()}>
<div data-slot="message-content" class=${cn(messageContentClass(), 'gap-2')}>
<div class="space-y-4">
<p>Install Dialog, then open it from the gear button in <code class=${inlineCode}>site-header.tsx</code>.</p>
<div data-slot="code-block" data-variant="default" data-size="default" class=${codeBlockClass()}>
<div data-slot="code-block-header" class=${codeBlockHeaderClass()}>
<div data-slot="code-block-title" class=${codeBlockTitleClass()}>bash</div>
<div data-slot="code-block-actions" class=${codeBlockActionsClass()}>
<ui-code-block-copy-button value=${command}></ui-code-block-copy-button>
</div>
</div>
<pre data-slot="code-block-content" data-numbered tabindex="0" dir="ltr" style="counter-reset: line 0" class=${codeBlockContentClass()}><code data-slot="code-block-code" class=${codeBlockCodeClass()}>${codeBlockLines(command)}</code></pre>
</div>
<p>Wrap the button in a <code class=${inlineCode}>DialogTrigger</code> and put the settings form in <code class=${inlineCode}>DialogContent</code>. Focus moves into the dialog when it opens, and <strong>Esc</strong> closes it.</p>
</div>
<div data-slot="message-footer" class=${cn(messageFooterClass(), 'px-0')}>
${MessageAction('Copy', CopyIcon())}
${MessageAction('Good response', ThumbsUpIcon())}
${MessageAction('Bad response', ThumbsDownIcon())}
${MessageAction('Retry', RefreshCcwIcon())}
</div>
</div>
</div>
</div>
`;
}
View Code
Use Message for one turn in a thread. The person's text is a Bubble on a Message with data-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 following dependencies:
Copy and paste the following code into your project.
/**
* Message: one entry in a conversation (a chat bubble with its avatar,
* header and footer), and the group that stacks several. Tier-1 class
* helper; compose with plain elements, and pair with the bubble helpers for
* the speech bubble itself.
*
* shadcn parity:
* MessageGroup → messageGroupClass()
* Message → messageClass(), with data-align="start" | "end"
* MessageAvatar → messageAvatarClass()
* MessageContent → messageContentClass()
* MessageHeader → messageHeaderClass()
* MessageFooter → messageFooterClass()
*
* `data-align="end"` on the message mirrors it to the inline end (the user's
* own messages), and the footer follows. Alignment is logical, so it flips
* under `dir="rtl"`.
*
* A11y (required for accessible output): render the conversation as a list
* (`<ol>` of messages, or `role="log"` on the container when new messages
* arrive live, which also makes them announced). Each message should say who
* sent it in text a screen reader reaches, the header or a visually hidden
* name, and an avatar image needs `alt` (or `alt=""` when the name is
* already in the header).
*
* Design tokens used: --muted, --muted-foreground.
*
* @example
* ```html
* <ol class=${messageGroupClass()} role="log" aria-label="Conversation">
* <li class=${messageClass()} data-align="end">
* <div class=${messageContentClass()}>
* <div class=${messageHeaderClass()}>You</div>
* <p>Can you ship it today?</p>
* </div>
* </li>
* </ol>
* ```
*/
/** The stack of messages. */
export function messageGroupClass(): string {
return 'gap-2 flex min-w-0 flex-col';
}
/** One message row. `data-align="end"` mirrors it to the inline end. */
export function messageClass(): string {
return 'gap-2 text-sm group/message relative flex w-full min-w-0 data-[align=end]:flex-row-reverse';
}
/** The sender's avatar beside the message. */
export function messageAvatarClass(): string {
return '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';
}
/** The column holding the header, the bubble and the footer. */
export function messageContentClass(): string {
return 'gap-2.5 group-data-[align=end]/message:*:data-slot:self-end flex w-full min-w-0 flex-col wrap-break-word';
}
/** The sender name and time above the bubble. */
export function messageHeaderClass(): string {
return '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';
}
/** Actions or status below the bubble. */
export function messageFooterClass(): string {
return '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';
}
Update the import paths to match your project setup.
Usage
import { bubbleClass, bubbleContentClass } from '#components/ui/bubble.ts';
import { buttonClass } from '#components/ui/button.ts';
import { messageClass, messageContentClass, messageFooterClass } from '#components/ui/message.ts';
import '#components/ui/tooltip.ts';
import { CopyIcon } from '#lib/utils/icons.ts';
<div data-slot="message" data-align="end" class=${messageClass()}>
<div data-slot="message-content" class=${messageContentClass()}>
<div data-slot="bubble" data-variant="muted" data-align="end" class=${bubbleClass({ variant: 'muted' })}>
<div data-slot="bubble-content" class=${bubbleContentClass()}>How do I add a settings dialog?</div>
</div>
</div>
</div>
<div data-slot="message" data-align="start" class=${messageClass()}>
<div data-slot="message-content" class=${cn(messageContentClass(), 'gap-2')}>
<p>Install Dialog, then open it from the gear button.</p>
<div data-slot="message-footer" class=${messageFooterClass()}>
<button type="button" data-slot="button" data-variant="ghost" data-size="icon-sm" aria-label="Copy" class=${buttonClass({ variant: 'ghost', size: 'icon-sm' })}>
${CopyIcon()}
</button>
</div>
</div>
</div>
See Message.
Actions
Put actions in the message footer. It follows the message alignment, so on the person's turn the actions sit on the right. Use ghost icon buttons with a <ui-tooltip> for each, and give each button its own aria-label, since the tip describes the button rather than naming it.
- Copy writes the reply to the clipboard and swaps to a check for two seconds. That is a click handler in a component of your own.
- 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. The kit does not parse Markdown, so render it with the Markdown library you already use and 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, with a Retry button that asks your server to run the turn again. See AI SDK for streaming the reply from a 'use server' action.
function turn(message: ChatMessage) {
const text = message.parts.map((part) => (part.type === 'text' ? part.text : '')).join('');
if (message.role === 'user') {
return html`<div data-slot="message" data-align="end" class=${messageClass()}>
<div data-slot="message-content" class=${messageContentClass()}>
<div data-slot="bubble" data-variant="muted" data-align="end" class=${bubbleClass({ variant: 'muted' })}>
<div data-slot="bubble-content" class=${bubbleContentClass()}>${text}</div>
</div>
</div>
</div>`;
}
return html`<div data-slot="message" data-align="start" class=${messageClass()}>
<div data-slot="message-content" class=${cn(messageContentClass(), 'gap-2')}>
${markdown(text)}
<div data-slot="message-footer" class=${messageFooterClass()}>
<button type="button" data-slot="button" data-variant="ghost" data-size="icon-sm" aria-label="Retry"
class=${buttonClass({ variant: 'ghost', size: 'icon-sm' })}
@click=${() => this.retry(message.id)}>
${RefreshCcwIcon()}
</button>
</div>
</div>
</div>`;
}
Call turn for each message in your chat component's render(). markdown stands for your Markdown renderer, and retry for a method that calls your action again for that message.
Migrating from AI SDK Elements
If you used Message from AI SDK Elements, its wrapper lives in components/ai-elements/message.tsx. In a WebJs app it becomes this pattern, built from WebJs UI components. Each export of the wrapper maps onto a part: