Skip to main content

Message Scroller

A chat scroll container that follows streaming content, anchors turns, keeps its place on prepend, and offers a jump-to-latest button.

Kinetic Assistant
Starting thread with bottom scroll anchoring enabled.
Linh Tran
How does MessageScroller decide when to show the floating button?
Kinetic Assistant
Intersection sentinels track both top and bottom edges while scrolling.
Linh Tran
Got it. Does it also keep user turns anchored during streaming?
Kinetic Assistant
Yes. Mark any user turn with scrollAnchor so defaultScrollPosition can jump straight to it.
Linh Tran
And focus recovery when the jump button hides?
Kinetic Assistant
Scroll up to reveal the button, then click or press Enter to settle smoothly back to the viewport.
at-bottom·unread=2

Customize

Button sizeIcon Small (icon-sm)
Unread count2
Button blur (px)4
Speed1

Installation

npx shadcn@latest add @kinetic/message-scroller

Usage

Wrap the scroller in MessageScrollerProvider and give each MessageScrollerItem a messageId.

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

export function ChatLog({ messages }: { messages: { id: string; text: string }[] }) {
  return (
    <MessageScrollerProvider autoScroll>
      <MessageScroller className="h-80 rounded-lg border">
        <MessageScrollerViewport>
          <MessageScrollerContent className="p-4">
            {messages.map((message) => (
              <MessageScrollerItem key={message.id} messageId={message.id}>
                {message.text}
              </MessageScrollerItem>
            ))}
          </MessageScrollerContent>
        </MessageScrollerViewport>
        <MessageScrollerButton />
      </MessageScroller>
    </MessageScrollerProvider>
  );
}

Following new content

With autoScroll the view follows growing content until the user scrolls up. defaultScrollPosition sets where it starts: end, start or last-anchor.

<MessageScrollerProvider autoScroll defaultScrollPosition="last-anchor">...</MessageScrollerProvider>

Anchors

Mark user turns with scrollAnchor so a new reply scrolls the question to the top, leaving scrollPreviousItemPeek pixels of the previous item visible.

<MessageScrollerItem messageId={id} scrollAnchor={role === "user"}>...</MessageScrollerItem>

Jump button

MessageScrollerButton appears when the latest message is out of view. direction picks the edge, and unreadCount shows an animated counter.

<MessageScrollerButton unreadCount={3} />

Hooks

useMessageScroller() returns scrollToEnd, scrollToStart and scrollToMessage. useMessageScrollerScrollable() reports whether each edge can scroll, and useMessageScrollerVisibility() returns the visible message ids.

const { scrollToMessage } = useMessageScroller();

scrollToMessage("m42");

Motion preferences

Smooth scrolling falls back to instant jumps when reduced motion is enabled. blurPx and speed tune the jump button reveal.

API Reference

MessageScrollerProvider

Zero-dependency scroll state store coordinating sticky streaming autoScroll, initial scroll placement, anchor visibility, and prepend stabilization.

PropTypeDefaultDescription
autoScrollbooleanfalseFollows growing content automatically until the user scrolls upward.
defaultScrollPosition"start" | "end" | "last-anchor""end"Initial scroll target on mount.
scrollEdgeThreshold / scrollPreviousItemPeek / scrollMarginnumber8 / 64 / 0Edge detection threshold, anchor peek height, and scroll-to-message offset in pixels.

MessageScroller / MessageScrollerViewport / MessageScrollerContent / MessageScrollerItem

Scroll region landmark, ARIA log container with bottom safe padding, and item visibility tracker.

PropTypeDefaultDescription
preserveScrollOnPrepend (Viewport)booleantrueMaintains viewport offset when older messages are prepended above.
messageId / scrollAnchor (Item)string / booleanscrollAnchor=falseRegisters message ID for programmatic scrolling and marks turn anchoring.

MessageScrollerButton

Kinetic Button-backed floating control entering via spatial Euler spring and blur, with directionalSwap unread counter and viewport focus recovery.

PropTypeDefaultDescription
direction / behavior"start" | "end" / "smooth" | "auto""end" / "smooth"Scrolls toward top or latest bottom edge with reduced-motion-safe behavior.
variant / sizeKineticButtonVariant / KineticButtonSize"secondary" / "icon-sm"Kinetic Button appearance and size (automatically expands width when unreadCount > 0).
unreadCount / blurPx / speednumber / number / numberblurPx=4 / speed=1Optional animated unread counter badge, reveal blur radius (0-16px), and spring speed (0.25-4).

useMessageScroller / useMessageScrollerScrollable / useMessageScrollerVisibility

Hooks providing scrollToEnd, scrollToBottom, scrollToStart, scrollToMessage, edge state, and visible anchor IDs.

PropTypeDefaultDescription
Hooks returnscrollToEnd / { start, end } / { currentAnchorId, visibleMessageIds }-Imperative navigation controls and reactive store snapshots.

Notes

Always nest in fixed order: MessageScrollerProvider > MessageScroller > MessageScrollerViewport > MessageScrollerContent > MessageScrollerItem, with MessageScrollerButton placed inside MessageScroller after MessageScrollerViewport. Never substitute ScrollArea for streaming chat threads.