PeakUIPro

ChatConversation

Scrollable chat viewport that follows streaming messages

Usage

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

Anatomy

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

export default () => (
  // The viewport scrolls inside a bounded height: `flex-1` in a flex column, or `h-full`.
  <ChatConversation className="flex-1">
    {/* Required for following: the root watches this part grow. */}
    <ChatConversation.Content>{messages}</ChatConversation.Content>
    {/* Optional: a styling and testing hook; following does not depend on it. */}
    <ChatConversation.ScrollAnchor />
    {/* Optional: always mounted, shown only away from the bottom. */}
    <ChatConversation.ScrollButton />
  </ChatConversation>
);

Examples

Full Chat

Compose the conversation with a reasoning trace and a prompt input.

Scroll Button

Add ScrollButton for an explicit jump-to-bottom control.

Streaming

Follows appended content at the bottom and holds still once the reader scrolls up.

Short Content

Keeps the button hidden while the content fits the viewport.

Switching Conversations

key the root per conversation: a released viewport stays released across a content swap.

Accessible Names

aria-label names each root; the default name is the same for every one.

Focusable Messages

tabIndex={-1} removes the viewport from the tab order when messages hold focusable content.

Quiet Log

aria-live="off" stops the log announcing additions, the scroll button included.

Customization

Tailwind CSS

<ChatConversation className="[--chat-conversation-content-max-width:48rem] [--chat-conversation-anchor-threshold:48px]">
  <ChatConversation.Content>{messages}</ChatConversation.Content>
  <ChatConversation.ScrollButton className="shadow-overlay" />
</ChatConversation>

Global CSS

@layer components {
  .chat-conversation {
    --chat-conversation-content-max-width: 48rem;
    --chat-conversation-content-gap: 1.5rem;
    --chat-conversation-scroll-button-offset: 1.5rem;
    --chat-conversation-anchor-threshold: 48px;
  }

  .chat-conversation__scroll-button {
    @apply shadow-overlay;
  }
}

Styling Reference

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

CSS Classes

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

Base Classes

  • .chat-conversation - Scrolling viewport

Element Classes

  • .chat-conversation__content - Centered message column
  • .chat-conversation__scroll-anchor - One-pixel marker at the foot of the content
  • .chat-conversation__scroll-button-container - Sticky, zero-height positioner of the button
  • .chat-conversation__scroll-button - Jump-to-bottom button, beside the core .button classes

Slots

  • [data-slot="chat-conversation"]
  • [data-slot="chat-conversation-content"]
  • [data-slot="chat-conversation-scroll-anchor"]
  • [data-slot="chat-conversation-scroll-button-container"]
  • [data-slot="chat-conversation-scroll-button"]

CSS Variables

VariablePurpose
--chat-conversation-content-max-widthWidest the message column grows; defaults to calc(var(--spacing) * 178.5)
--chat-conversation-content-gapSpace between messages; defaults to calc(var(--spacing) * 8)
--chat-conversation-content-padding-blockSpace above the first and below the last message; defaults to calc(var(--spacing) * 8)
--chat-conversation-content-padding-inlineSpace beside the column; defaults to calc(var(--spacing) * 4)
--chat-conversation-scroll-button-offsetDistance from the foot of the viewport to the foot of the button; defaults to calc(var(--spacing) * 4)
--chat-conversation-anchor-thresholdHow far from the bottom the reader still counts as at the bottom; read in script, so a px, rem or em length, never a calc(); defaults to 24px

Interactive States

The viewport takes focus so the keyboard can scroll it:

  • Focus: :focus-visible draws a ring inside the edge
  • Attached: [data-state="attached"] on the anchor while the viewport follows new content
  • Released: [data-state="released"] on the anchor after an upward scroll past the threshold
  • Button hidden: [data-state="hidden"] on the button container, with aria-hidden, a disabled button and visibility: hidden
  • Button shown: [data-state="visible"] on the button container
  • Reduced motion: @media (prefers-reduced-motion: reduce) ends the fade and scrolls to the bottom at once
  • Forced colors: @media (forced-colors: active) draws the focus ring in Highlight

API Reference

ChatConversation

PropTypeDefaultDescription
aria-labelstring"Conversation"Accessible name of the log. The default is the same for every root, so name each conversation when a page holds more than one
rolestring"log"A streamed answer is announced as it grows
tabIndexnumber0A scrollable region must be reachable by keyboard; pass -1 when the messages hold focusable content
aria-live'off' | 'polite' | 'assertive'"polite" (from role="log")The scroll button sits inside the log, so showing it adds it to the log; "off" silences additions, new messages included
keyReact.Key-Set it per conversation: a released viewport stays released when only Content is swapped
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes

ChatConversation.Content

PropTypeDefaultDescription
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes. The root follows growth of this part only: messages placed directly in the root are not followed

ChatConversation.ScrollAnchor

PropTypeDefaultDescription
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes. aria-hidden="true" is always set; data-state is "attached" or "released"

ChatConversation.ScrollButton

PropTypeDefaultDescription
childrenReact.ReactNode | (values: ButtonRenderProps) => React.ReactNodeAn arrow-down iconReplaces the default glyph
aria-labelstring"Scroll to bottom"Accessible name. The default applies to any icon-only button
isIconOnlybooleantrueSquare button for one icon; set false to show text beside it
variant'primary' | 'secondary' | 'tertiary' | 'outline' | 'ghost' | 'danger' | 'danger-soft''secondary'Core button style
size'sm' | 'md' | 'lg''sm'Core button size
isDisabledbooleanfalseWhether the button is disabled; it is always disabled while the viewport is at the bottom
onPress(e: PressEvent) => void-Handler called before the viewport scrolls to the bottom, smoothly or at once under reduced motion
classNamestring | (values: ButtonRenderProps) => string-Additional classes, or a function of the button's state
renderDOMRenderFunction<'button', ButtonRenderProps>-Replaces the rendered element; receives the props with data-slot set
...restButtonProps-Every other core Button prop

Render Props

PropTypeDescription
isPendingbooleanWhether the button is waiting on an operation
isPressedbooleanWhether the button is currently pressed
isHoveredbooleanWhether the button is hovered
isFocusedbooleanWhether the button is focused
isFocusVisiblebooleanWhether the button is focused by keyboard

On this page