Conversation

A chat transcript that follows new messages.

It’s a log region, read out as messages arrive. The scroller follows new content while you’re at the bottom, and leaves you where you are once you scroll up. MessageStep shows a tool call under an assistant’s message.

Preview
basic.tsx
import { Conversation, Message, MessageStep } from "~/components/conversation"

export default function Example() {
  return (
    <div className="h-80 w-full max-w-md border border-border bg-background">
      <Conversation aria-label="Pagination agent" className="h-full">
        <Message from="user">Fit the council story on A1 without cutting the lead photo.</Message>
        <Message from="assistant" name="Pagination agent">
          Reading the page layout first.
        </Message>
        <MessageStep>read_page_layout · complete</MessageStep>
        <MessageStep state="error">apply_layout_operation · error</MessageStep>
        <MessageStep>apply_layout_operation · complete</MessageStep>
        <MessageStep>measure_layout_revision · complete</MessageStep>
        <Message from="assistant" name="Pagination agent">
          The story now runs in two columns with the frame 12 lines taller. The overflow is gone and
          the photo keeps its size.
        </Message>
      </Conversation>
    </div>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesScroll AreaprimitiveTextprimitive
Used byNothing yet.
Packages None.

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/conversation.json

Manually

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/conversation.tsx
import type * as React from "react"

import { ScrollArea } from "~/components/ui/scroll-area"
import { textVariants } from "~/components/ui/text"
import { cn } from "~/lib/utils"

/*
 * A chat transcript, like an agent's. The scroller follows new messages while
 * you're at the bottom, and leaves you where you are once you scroll up.
 *
 * <Conversation aria-label="…">
 *   <Message from="user">…</Message>
 *   <Message from="assistant" name="…">…</Message>
 * </Conversation>
 */

/**
 * The transcript's scrolling log. Give it a height and an `aria-label`. It starts at the newest
 * message and follows new ones while you're at the bottom; scrolling up lets go.
 */
function Conversation({ className, children, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="conversation"
      role="log"
      className={cn("flex min-h-0 flex-col", className)}
      {...props}
    >
      <ScrollArea stickToEnd className="min-h-0 flex-1">
        <div className="flex flex-col gap-lg p-lg">{children}</div>
      </ScrollArea>
    </div>
  )
}

type MessageProps = React.ComponentProps<"div"> & {
  /** Who wrote it. The person's messages sit on the right, in a shaded box. */
  from: "user" | "assistant"
  /** A name over the message, like the agent's. */
  name?: React.ReactNode
}

/** One message. Plain text and inline elements wrap; longer content can be anything. */
function Message({ from, name, className, children, ...props }: MessageProps) {
  return (
    <div
      data-slot="message"
      data-from={from}
      className={cn(
        textVariants({ size: "sm" }),
        "group/message flex max-w-[85%] flex-col gap-2xs data-[from=assistant]:self-start data-[from=user]:self-end",
        className,
      )}
      {...props}
    >
      {name !== undefined && (
        <span className={cn(textVariants({ size: "xs", weight: "medium", tone: "muted" }))}>
          {name}
        </span>
      )}
      <div
        className={cn(
          textVariants({ leading: "relaxed" }),
          "whitespace-pre-wrap group-data-[from=user]/message:rounded-lg group-data-[from=user]/message:bg-muted group-data-[from=user]/message:px-md group-data-[from=user]/message:py-sm",
        )}
      >
        {children}
      </div>
    </div>
  )
}

/** A tool call or step under an assistant's message: a name and its state. */
function MessageStep({
  state = "done",
  className,
  children,
  ...props
}: React.ComponentProps<"div"> & { state?: "running" | "done" | "error" }) {
  return (
    <div
      data-slot="message-step"
      data-state={state}
      className={cn(
        textVariants({ size: "xs", family: "mono", tone: "muted" }),
        "flex items-center gap-sm before:size-1.5 before:shrink-0 before:rounded-full before:bg-current data-[state=error]:text-destructive data-[state=running]:before:animate-pulse",
        className,
      )}
      {...props}
    >
      {children}
    </div>
  )
}

export { Conversation, Message, MessageStep }
export type { MessageProps }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Conversation

The transcript's scrolling log. Give it a height and an aria-label. It starts at the newest message and follows new ones while you're at the bottom; scrolling up lets go.

Takes the props of a native div.

Message

One message. Plain text and inline elements wrap; longer content can be anything.

from*ours

Who wrote it. The person's messages sit on the right, in a shaded box.

Type "user" | "assistant"

nameours

A name over the message, like the agent's.

Type ReactNode

MessageStep

A tool call or step under an assistant's message: a name and its state.

stateours

Type "done" | "error" | "running" · Default done