This page as markdown: /message-scroller.md · Index for agents: /llms.txt · Everything in one file: /llms-full.txt

MessageScroller

Status: stable

Scroll container for a conversation. Auto-follows new messages until the reader scrolls away, holds position when older history is prepended, and exposes scroll commands and visibility tracking through its ref.

Keywords: chat, conversation, messages, thread, transcript, stick to bottom, auto scroll, streaming, infinite scroll, inbox, ai chat, support chat

Usage

The scroll region a conversation lives in. It owns the scrolling and nothing else — not your messages, not your AI state, not your transport or persistence — so you bring the content renderer and it handles the behaviors that make streaming chat feel right: pinning to the newest message, releasing when the reader scrolls up, keeping the view still when older history loads above, jumping to a specific message, restoring a saved read position, and reporting what is actually on screen.

Best practices

Anatomy

Part Required Description
Scroll region yes The element that scrolls. Carries className: sizing, padding, and background.
Message column yes The flex column holding the children, spaced by gap. Publishes --message-gap so <Message grouped> can close it.
Jump to latest no A control stuck to the bottom edge, shown only while the reader is scrolled away from the newest message.

Props

Name Type Default Description
ref Ref — Imperative commands, not the DOM node: scrollToBottom(opts), scrollToTop(opts), scrollToMessage(id, opts) (returns false when the message is not rendered), isFollowing(), and element for the scrolling node itself. opts is ScrollCommandOptions: { animate?: boolean } (default true, and off whenever motion is), not the DOM's behavior.
autoFollow boolean true Keep the newest message in view as content arrives, until the reader scrolls away from the bottom. Set false for a transcript that must never move under the reader.
followThreshold number 64 Distance from the bottom, in px, that still counts as following. A band rather than a pixel, so a token arriving mid-frame does not flicker the state.
onFollowChange (following: boolean) => void — Notified when the viewport pins to, or releases from, the newest message.
initialScroll "bottom" | "top" | { messageId: string } "bottom" Where a freshly mounted thread lands. Pass a messageId to restore a saved read position. Applied once, on mount — changing it later will not yank a reader mid-conversation.
jumpToLatest boolean true Show the jump-to-latest control while the reader is scrolled away from the newest message.
jumpToLatestLabel string "Jump to latest" Accessible label and tooltip for that control.
gap "sm" | "md" | "lg" "md" Vertical rhythm between messages. Also published as --message-gap, which <Message grouped> reads to collapse the gap within a run.
onVisibleMessagesChange (ids: string[]) => void — Ids of the messages currently intersecting the viewport, in DOM order. The hook for read receipts, mark-as-seen, and unread counters.
...div props Omit<ComponentPropsWithRef<"div">, "ref"> — Spread onto the scrolling element. className bounds and pads the scroll region; onScroll composes with the component's own.

Variants

Tokens used

border, secondary, secondary-foreground, ring

Accessibility

Related

Examples

Streaming conversation

import { Bubble, Marker, Message, MessageScroller } from "@sableui/react"

<MessageScroller className="flex-1 min-h-0 px-4 py-6">{messages.map((m) => (<Message key={m.id} messageId={m.id} align={m.mine ? "end" : "start"}><Bubble variant={m.mine ? "primary" : "muted"} align={m.mine ? "end" : "start"}>{m.body}</Bubble></Message>))}{pending ? <Marker icon="spinner" spin shimmer>Thinking…</Marker> : null}</MessageScroller>

Scroll to a message on demand

import { Bubble, Button, Message, MessageScroller } from "@sableui/react"

function Thread({ messages, replyTo }) {
	const thread = useRef(null)
	return (
		<>
			<Button onClick={() => thread.current?.scrollToMessage(replyTo)}>Show original</Button>
			<MessageScroller ref={thread} className="max-h-96">
				{messages.map((m) => (<Message key={m.id} messageId={m.id}><Bubble>{m.body}</Bubble></Message>))}
			</MessageScroller>
		</>
	)
}

Restore a saved read position and track receipts

import { Bubble, Message, MessageScroller } from "@sableui/react"

<MessageScroller className="h-96" initialScroll={{ messageId: lastRead }} onVisibleMessagesChange={(ids) => markSeen(ids)}>{messages.map((m) => (<Message key={m.id} messageId={m.id}><Bubble>{m.body}</Bubble></Message>))}</MessageScroller>

Prepend older history without moving the view

import { Bubble, Button, Message, MessageScroller } from "@sableui/react"

<MessageScroller className="h-96" gap="sm">{hasMore ? <Button variant="secondary" loadingLabel="Loading…" onClick={loadOlder}>Load earlier messages</Button> : null}{messages.map((m) => (<Message key={m.id} messageId={m.id}><Bubble>{m.body}</Bubble></Message>))}</MessageScroller>