Skip to content
WebJs
Menu

One turn. The person's message, and the agent's reply with its actions.

How do I add a settings dialog? It should open from the gear icon in the header.

Install Dialog, then open it from the gear button in site-header.tsx.

bash
npx @webjsdev/ui 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.

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.

pnpm dlx @webjsdev/ui add message bubble tooltip

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-pressed so 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:

AI SDK ElementsWebJs UI
MessageA message, with data-align="end" on the person's turn.
MessageContentThe message content. On the person's turn it holds a bubble with data-variant="muted" and data-align="end".
MessageActionsThe message footer.
MessageActionA ghost icon-sm button inside a <ui-tooltip>, with its label as the aria-label and the tip.
MessageResponseThe reply, rendered by your Markdown library with fenced code routed to a Code Block.
MessageBranch, MessageToolbarNo kit part. Keep them as markup of your own, laid out with the message footer.