Skip to content
Playground

Message Scroller

A user-respecting transcript scroller for live replies, saved conversations, and long message history.

Message Scroller manages the scrolling behavior around a conversation without owning the message UI. It follows new content only while the reader is at the live edge, preserves their position when older history is prepended, and exposes commands for navigation.

New Chat

How can I help you today?

Morning, Zenos!

What are we working on today? Press send to start a new conversation.

Ready

The preview uses useChat from @ai-sdk/react with a local scripted ChatTransport. It exercises the same message, status, and streamed-part shapes as an AI-backed application without sending a network request or requiring an API key. Replace the mock transport with your application transport when connecting a real model.

TSX
import { Message, MessageScroller } from "@zentara-co/zenos-ui";import { useChat } from "@ai-sdk/react";

Give the root a height-constrained parent, then compose the viewport, live-region content, and stable message items. MessageScroller.Button appears after the reader moves away from the latest message.

TSX
<div className="h-96">  <MessageScroller.Provider>    <MessageScroller>      <MessageScroller.Viewport>        <MessageScroller.Content>          {messages.map((message) => (            <MessageScroller.Item              key={message.id}              messageId={message.id}              scrollAnchor={message.startsTurn}            >              <Message>{message.content}</Message>            </MessageScroller.Item>          ))}        </MessageScroller.Content>      </MessageScroller.Viewport>      <MessageScroller.Button />    </MessageScroller>  </MessageScroller.Provider></div>

autoScroll defaults to true. Appended content follows the live edge only while the reader is already there. Scrolling upward releases that behavior, so a streamed response cannot pull the reader away from older content. Set autoScroll={false} when the application should never follow appended content automatically.

TSX
<MessageScroller.Provider autoScroll={false}>{/* transcript */}</MessageScroller.Provider>

Choose the initial position with defaultScrollPosition:

  • "end" opens at the newest message.
  • "start" opens at the beginning.
  • "last-anchor" opens at the latest item marked with scrollAnchor.

Use scrollPreviousItemPeek to control the space shown above an anchored turn.

TSX
<MessageScroller.Provider defaultScrollPosition="last-anchor" scrollPreviousItemPeek={64}>  {/* transcript */}</MessageScroller.Provider>

preserveScrollOnPrepend defaults to true. When older items are inserted before the current list, the viewport compensates for the added height so the visible message stays in place. Keep every messageId stable across pagination.

Use useMessageScroller inside the provider for transcript navigation. scrollToMessage returns false when the requested ID is not mounted.

TSX
function TranscriptControls() {  const { scrollToEnd, scrollToMessage, scrollToStart } = useMessageScroller();   return (    <>      <button onClick={() => scrollToStart({ behavior: "smooth" })}>First</button>      <button onClick={() => scrollToMessage("message-42", { block: "center" })}>        Jump to result      </button>      <button onClick={() => scrollToEnd({ behavior: "smooth" })}>Latest</button>    </>  );}

useMessageScrollerVisibility returns visibleMessageIds and the nearest visible currentAnchorId. Use that state for a transcript outline, unread markers, or a synchronized search result list.

The jump button uses logical positioning and mirrors automatically in a dir="rtl" region or DirectionProvider. Message alignment remains the responsibility of the composed Message and Bubble parts.

MessageScroller.Provider
MessageScroller.Root
MessageScroller.Viewport
MessageScroller.Content
MessageScroller.Item
MessageScroller.Button

API

PropTypeDefaultDescription
autoScroll boolean true Follows appended content while the reader remains at the live edge.
defaultScrollPosition start | end | last-anchor end Initial position when the transcript is mounted.
preserveScrollOnPrepend boolean true Preserves the visible position when older history is prepended.
scrollPreviousItemPeek number 48 Space above an item when a turn anchor is positioned.
messageId string — Stable Item identifier used by commands and visibility tracking.
scrollAnchor boolean false Marks an Item as a conversation-turn boundary.
target start | end end Transcript boundary targeted by MessageScroller.Button.
native props React.HTMLAttributes<HTMLDivElement> — Forwards native attributes, events, aria-*, data-*, className, style, and ref.

Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props, including className, style, aria-*, and data-*.

The viewport is keyboard-focusable and labeled “Conversation” by default. Content uses role="log" with a polite live region for additions. Keep sender names and message status in readable text, provide stable IDs, and avoid announcing large batches when loading historical messages.

The behavior is informed by the shadcn Message Scroller while using Zenos tokens, compound APIs, and direction conventions.