PeakUIPro

ChatMessage

User and assistant message layouts for chat threads

Usage

import {ChatMessage} from "@peakui-pro/react";

Anatomy

import {ChatMessage, ChatMessageActions} from "@peakui-pro/react";

export default () => (
  <>
    {/* A user turn: a bubble pushed to the far edge */}
    <ChatMessage.User>
      <ChatMessage.Bubble>
        <ChatMessage.Content>...</ChatMessage.Content>
      </ChatMessage.Bubble>
    </ChatMessage.User>

    {/* An assistant turn: an avatar beside a column */}
    <ChatMessage.Assistant>
      <ChatMessage.Avatar fallback="AI" />
      <ChatMessage.Body>
        <ChatMessage.Content>...</ChatMessage.Content>
        <ChatMessage.Actions>
          <ChatMessageActions>...</ChatMessageActions>
        </ChatMessage.Actions>
      </ChatMessage.Body>
    </ChatMessage.Assistant>
  </>
);

Examples

User Message

Assistant Message

With Actions

Grouped

show={false} keeps the avatar slot empty, so bodies stay aligned.

Avatar Styles

Customization

Tailwind CSS

<ChatMessage.User className="[--chat-message-bubble-background:var(--accent)] [--chat-message-bubble-foreground:var(--accent-foreground)]">
  <ChatMessage.Bubble>
    <ChatMessage.Content>Hello</ChatMessage.Content>
  </ChatMessage.Bubble>
</ChatMessage.User>

Global CSS

@layer components {
  .chat-message--user {
    --chat-message-bubble-background: var(--accent);
    --chat-message-bubble-foreground: var(--accent-foreground);
  }

  .chat-message--assistant {
    --chat-message-body-inset: 0px;
  }
}

Styling Reference

PeakUI follows the BEM methodology, so variants and states are plain class names you can target.

CSS Classes

The ChatMessage component uses these CSS classes (view source styles):

Row Classes

  • .chat-message--assistant - Assistant row: avatar beside a column
  • .chat-message--user - User row: bubble at the far edge

Element Classes

  • .chat-message__avatar - Avatar slot; keeps its width when the avatar is hidden
  • .chat-message__body - Assistant column
  • .chat-message__bubble - User bubble; gains a border in forced colors
  • .chat-message__content - Wrapping text
  • .chat-message__actions - Action row container

Slots

  • [data-slot="chat-message-assistant"]
  • [data-slot="chat-message-user"]
  • [data-slot="chat-message-avatar"] - carries [data-size="sm" | "md" | "lg"]
  • [data-slot="chat-message-body"]
  • [data-slot="chat-message-bubble"]
  • [data-slot="chat-message-content"]
  • [data-slot="chat-message-actions-container"]

CSS Variables

Set on .chat-message--assistant and .chat-message--user:

VariablePurpose
--chat-message-gapSpace between avatar and body
--chat-message-padding-blockVertical padding of a row
--chat-message-body-gapSpace between the parts of the body
--chat-message-body-insetSpace reserved at the end of the body
--chat-message-bubble-radiusBubble corner radius
--chat-message-bubble-padding-blockBubble vertical padding
--chat-message-bubble-padding-inlineBubble horizontal padding
--chat-message-bubble-backgroundBubble background color
--chat-message-bubble-foregroundBubble text color
--chat-message-bubble-max-widthWidest the bubble grows

Set on .chat-message__avatar:

VariablePurpose
--chat-message-avatar-sizeWidth of the avatar slot, by size: 2rem, 2.5rem, 3rem

API Reference

ChatMessage.Assistant

PropTypeDefaultDescription
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes

ChatMessage.User

PropTypeDefaultDescription
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes

ChatMessage.Avatar

PropTypeDefaultDescription
srcstring-The image; without it, or until it loads, the fallback shows
fallbackReactNode-Initials or a glyph shown until the image loads, and on the server
altstring""Text alternative of the image; empty because the message already names the speaker
showbooleantrueWhen false the slot stays and is empty
color'default' | 'accent' | 'success' | 'warning' | 'danger''default'Fallback color of the core Avatar
size'sm' | 'md' | 'lg''sm'Avatar size; also sets the width of the slot
variant'default' | 'soft''default'Core Avatar style
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restOmit<ComponentProps<'div'>, 'children' | 'color'>-Native <div> attributes; the slot's own children are the Avatar

ChatMessage.Body

PropTypeDefaultDescription
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes

ChatMessage.Bubble

PropTypeDefaultDescription
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes

ChatMessage.Content

PropTypeDefaultDescription
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes

ChatMessage.Actions

PropTypeDefaultDescription
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes. Holds the container only; put ChatMessageActions inside. Also exported as ChatMessageActionsContainer

On this page