Skip to content
Ballpoint

Message Scroller

The scroll behaviour of a chat transcript: a new turn anchored near the top, streamed output followed only at the live edge, saved threads opened where you say, older messages loaded without losing your place.

From shadcn/ui
Question 1: what next for the notebook?
Answer 1: start a page, ink it, and let the margin do the rest.
Question 2: what next for the notebook?
Answer 2: start a page, ink it, and let the margin do the rest.
Question 3: what next for the notebook?
Answer 3: start a page, ink it, and let the margin do the rest.
Question 4: what next for the notebook?
Answer 4: start a page, ink it, and let the margin do the rest.
Question 5: what next for the notebook?
Answer 5: start a page, ink it, and let the margin do the rest.
Question 6: what next for the notebook?
Answer 6: start a page, ink it, and let the margin do the rest.
Question 7: what next for the notebook?
Answer 7: start a page, ink it, and let the margin do the rest.

Installation

pnpm dlx shadcn@latest add @ballpoint/message-scroller

Usage

tsx
import { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport } from "@/components/ui/message-scroller"

<MessageScrollerProvider autoScroll defaultScrollPosition="end">
  <MessageScroller>
    <MessageScrollerViewport>
      <MessageScrollerContent>
        {messages.map((message) => (
          <MessageScrollerItem key={message.id} messageId={message.id} scrollAnchor={message.role === "user"}>
            <Message>…</Message>
          </MessageScrollerItem>
        ))}
      </MessageScrollerContent>
    </MessageScrollerViewport>
    <MessageScrollerButton />
  </MessageScroller>
</MessageScrollerProvider>

Examples

Streaming

Ask a follow-up: the question is anchored near the top, the reply is followed while you stay at the live edge, and scrolling up lets go of it.

Why do the lines wander?

Loading history

Older notes go in above; the one being read does not move.

Note 21: a thought, written down.
Note 22: a thought, written down.
Note 23: a thought, written down.
Note 24: a thought, written down.
Note 25: a thought, written down.
Note 26: a thought, written down.
Note 27: a thought, written down.
Note 28: a thought, written down.
Note 29: a thought, written down.
Note 30: a thought, written down.

API reference

MessageScrollerProvider autoScroll
default false
boolean

Follow streamed output while the reader is at the live edge. A wheel, touch, key or scrollbar drag lets go of it; the button takes it up again.

MessageScrollerProvider defaultScrollPosition
default "start"
"start" | "end" | "last-anchor"

Where a thread opens. "last-anchor" is the start of the last turn, or the end if that turn fits on screen.

MessageScrollerProvider scrollPreviousItemPeek
default 0
number

px of the previous turn kept in view above a newly anchored one.

MessageScrollerItem messageId / scrollAnchor
string / boolean

A stable id, for keeping your place, jumping and visibility; and whether the row starts a turn.

MessageScrollerViewport preserveScrollOnPrepend
default true
boolean

Keep the visible row where it is when older rows are added above.

MessageScrollerButton direction
default "end"
"start" | "end"

Scrolls there. Inert and out of sight until there is more in that direction.

useMessageScroller()
{ scrollToMessage, scrollToEnd, scrollToStart }

Move around from code. scrollToMessage returns false when the row isn't there.

useMessageScrollerVisibility()
{ currentAnchorId, visibleMessageIds }

The turn being read and the messages on screen. Tracked only while something uses it.

useMessageScrollerScrollable()
{ start, end }

Which edges can still be scrolled to.

MessageScrollerContent aria-busy
boolean

Pass true while a reply streams, so it is announced when it is finished.