Message Scroller
A scrollable message container with end-following and optional stable-key reading restoration.
A scrollable message container that follows appended messages while at the tail, lets readers explicitly jump back to the latest item, and supports optional stable-key reading position restoration.
Preview
Section titled “Preview”Installation
Section titled “Installation”Available starting in 0.3.0-rc.1 and later releases.
Compose Root, Viewport, and Jump:
- End-following: Appended messages stay in view while the reader is at the bottom. After scrolling away, use
Jumpto return to the latest message without taking focus. - Unread count and naming: Pass
newContentCountdirectly from your application state. Seta11yLabelonRootto name the scrollable region (defaults to'Messages').
Stable-key reading composition
Section titled “Stable-key reading composition”asMessageScrollerReading is an opt-in Hook exported from @proto.ui/prototypes-base/message-scroller. Compose it after a Base or Shadcn Viewport in your prototype so it shares the same Scroll Surface and disables generic preserve-visible, keeping stable-key restoration under one controller.
Configuration options:
readingCapture: Anchor selection mode —'first-crossing'(default, picks the first item whose end extends past the inset) or'visible-preferred'(prefers the first visibleprimaryitem, falling back to the first visible item).readingInset: Inset offset used during capture (default0).readingPredecessor: Whether to fall back to the saved predecessor when an exact key is missing (defaulttrue).
The host supplies MessageReadingFrame with mount epoch, scrollOffset, viewportSize, and ordered viewport-relative entries { key, start, end, primary }. Anchors have the shape { key, previousKey, index, offset, scrollOffset }. Restoration tries an exact key match first, then the saved predecessor (when enabled), then the saved absolute offset—never using index as a substitute identity. Scroll adjustments emit Scroll.request({ kind: 'by', ... }), leaving clamping and execution timing to the host.
An initial restore hint remains live across empty content, late arrivals, missing-key fallbacks, and zero-height exact matches until explicit reader input retires it. For selection restoration, the host supplies selection identity, offsets, length, endpoint availability, and prefix equality; the Hook skips modified text, existing non-collapsed selections, and duplicate replays, and prevents programmatic selectionchange events from pausing follow mode. DOM measurement, Range operations, fingerprinting, and persistence storage remain in your application.
The pure helpers captureMessageReadingAnchor, resolveMessageReadingAnchor, planMessageReadingRestore, and messageSelectionRestoreEligible expose the same policy outside the Hook.
| Prop | Type | Default |
|---|---|---|
newContentCount | number | 0 |
a11yLabel | string | Messages |
Viewport takes no props. Jump accepts the same props as Button.
asMessageScrollerReading Methods and State
Section titled “asMessageScrollerReading Methods and State”| Public method / state | Responsibility |
|---|---|
beginReading(following, anchor) | Initializes the reading session and optional restoration hint once. |
syncReadingFollowing(following), resumeReading() | Syncs an external reading-mode change or explicitly resumes tail following. Jump calls resumeReading when this Hook is composed. |
readingInput(input, frame) | Handles pointer/key/wheel, content focus, selection, or backfill facts to pause following and retire active hints. |
captureReading(frame), observeReadingScroll(frame) | Captures the current reading anchor and processes scroll reports without overwriting an active initial hint. |
restoreReading(frame), commitReadingRestore(ticket, actualFrame) | Issues a restore request and commits the actual offset; ticket and mount epoch reject stale acknowledgments and duplicate async captures. |
readingAnchorForPersistence(frame) | Returns a serializable anchor only when historical reading should replace the saved hint. |
planSelectionRestore(facts), commitSelectionRestore(ticket) | Determines selection restore eligibility and deduplicates completed host Range operations. |
readingMode, restoringReading, getReadingEpoch() | Exposes the current reading mode, restore state, and measurement epoch. |