Message Scroller
A chat scroll container that anchors turns, opens saved transcripts, follows streamed responses, loads history without jumping, and jumps to any message.
What Makes a Great Streaming Chat Experience
Building a chat interface used to be simple. You create an inverted list with an input. Type a message, it appends at the bottom. When a reply comes in, the list grows and scrolls. Done.
Streaming breaks that model. Messages arrive in chunks while you may still be reading, scrolling, or looking somewhere else entirely.
Now the challenge is preserving the reader's place while the conversation keeps changing. Get that wrong and the experience feels jumpy: people are pulled to the bottom, lose context, and have to find their way back.
In practice, this comes down to scroll: when to follow, when to hold, and when to let the reader decide. A great streaming chat should:
- Move only when the reader asked to move. If someone is reading, don't pull them somewhere else. Auto-scroll should never be the default.
- Follow only while they're following. If they're at the live edge, keep the stream in view. If they scroll away, leave them there.
- Every interaction is a signal. Scrolling is not the only one. Selecting text, using the keyboard, opening a link, or searching should all stop the interface from moving.
- Start a new turn near the top of the viewport. This gives the new turn somewhere it can be read from the beginning.
- Then stream in the answer. The answer should grow into the screen, not immediately push everything away.
- Keep part of the previous conversation in context. The prompt and reply should stay visually connected, and enough of the previous turn should remain visible so the reader knows where they are.
- Let new content arrive offscreen. The conversation can keep streaming without changing what the reader is looking at.
- Show what's happening out of view. Make it clear when a response is still streaming or when new messages have arrived.
- Make it easy to return to the latest reply. A "Jump to latest" action should bring the reader back and resume following.
- Let people jump anywhere in the conversation. Long threads need message links, search, unread markers, and direct navigation.
- Reopen where the reader left off. A saved conversation should open at the last meaningful turn. Often this is the last user message. Not the absolute bottom.
- Keep the reader's place when layout changes. Images load. Markdown expands. Code blocks render. Older messages appear above. None of that should make the reader lose their place.
- Handle interruptions without stealing position. Stopping, retrying, regenerating, branching, or errors should not unexpectedly move the conversation.
- Stay responsive in long threads. Streaming text, markdown, code, images, and long history should still feel responsive.
- Be accessible without the noise. Keep the transcript navigable, preserve keyboard focus, and announce important events at a comfortable pace.
Never move the reader against their intent.
MessageScroller
MessageScroller is a chat transcript scroller built for these behaviors. MessageScrollerProvider owns the scroll state and transcript-row behavior: opening position, streamed output, new-turn anchoring, prepended history, visibility, and scroll controls. MessageScroller is the styled frame that renders inside it.
MessageScroller is scoped to the scroll viewport. It does not own messages, AI state, transport, persistence, branching, or model state. Your product code stays focused on composing messages, markers, tools, attachments, and prompt inputs.
It gives you the scroll behavior that chat needs, without taking over the rest of the chat UI. And it stays fast, even in long conversations with rich markdown.
Installation
Usage
<MessageScrollerProvider>
<MessageScroller>
<MessageScrollerViewport>
<MessageScrollerContent>
@foreach (var message in messages)
{
<MessageScrollerItem @key="message.Id" MessageId="@message.Id" ScrollAnchor="@(message.Role == "user")">
<Message />
</MessageScrollerItem>
}
</MessageScrollerContent>
</MessageScrollerViewport>
<MessageScrollerButton />
</MessageScroller>
</MessageScrollerProvider>MessageScroller fills its parent, so place it inside a height-constrained container.
<div class="flex h-screen flex-col">
<MessageScrollerProvider>
<MessageScroller Class="flex-1">...</MessageScroller>
</MessageScrollerProvider>
</div>Composition
MessageScrollerProvider
└── MessageScroller
├── MessageScrollerViewport
│ └── MessageScrollerContent
│ ├── MessageScrollerItem (a message, marker, or row)
│ ├── MessageScrollerItem
│ └── MessageScrollerItem
└── MessageScrollerButtonMessageScrollerProvider: the headless root. Owns scroll state and the behavior parameters for opening position, auto-scroll, anchoring, scroll commands, and visibility tracking.MessageScroller: the styled frame. Lays out the viewport, content, and controls inside the provider.MessageScrollerViewport: the scrollable element. Receives native scroll events and preserves the visible row when older messages are prepended.MessageScrollerContent: the transcript container. Holds the rows and provides the live-region defaults for new messages.MessageScrollerItem: the transcript row boundary. Wrap every direct child of the content so the scroller can measure, anchor, preserve position, track visibility, and jump to it. An item can be a message, marker, typing indicator, separator, join/leave event, or "load earlier" row.MessageScrollerButton: the scroll control. Scrolls to the start or end of the transcript and is inert until there is content in its direction.
Core Concepts
Anchoring Turns
A turn is the part of the conversation that starts a new exchange. In a simple AI chat, that is usually the user's message and the assistant reply that follows.
An anchor is the row the viewport should treat as the start of that turn. Mark that row with ScrollAnchor. When a new anchor is appended, the viewport moves it near the top and keeps a peek of the previous item above it, so the new turn does not feel detached from its context.
<MessageScrollerItem MessageId="@message.Id" ScrollAnchor="@(message.Role == "user")" />Scroll anchors are not tied to message role. You can turn any row into an anchor: a user message, a system marker, a handoff event, or anything else that starts a meaningful turn. MessageScroller only needs to know which row should anchor the viewport.
In the following example, the user's message is anchored. When you send a new message, the viewport anchors it near the top and appends the assistant reply below it. Toggle the anchor to the assistant's message to see the difference.
Group Chat
In a group chat, the turn boundary is more specific than "the user message". It is often the message that asks the model to respond, or a marker like "Marcus joined the chat". Typing indicators and history controls usually should not anchor.
Because anchoring is role-independent, you can anchor a marker just as easily as a message.
<MessageScrollerItem MessageId="marcus-joined" ScrollAnchor="true">
<Marker Variant="MarkerVariant.Separator">
<MarkerContent>Marcus joined the chat</MarkerContent>
</Marker>
</MessageScrollerItem>Keeping Context Visible
When a new turn starts, it should still feel like part of the same continuous thread. ScrollPreviousItemPeek keeps a slice of the previous item visible above the anchor (here, 64 pixels), so the reader keeps their context instead of feeling like the conversation restarted on a blank page.
<MessageScrollerProvider ScrollPreviousItemPeek="64">
<MessageScroller>...</MessageScroller>
</MessageScrollerProvider>Adjust the peek amount in the example below to see how it affects the conversation.
Following the Live Edge
When the reader is at the live edge, either because they stayed there or returned there, AutoScroll keeps streamed replies in view as they grow. Scrolling away from the live edge releases the view, whether by wheel, touch, keyboard scroll keys, or dragging the scrollbar. An explicit message jump releases it too. New chunks can then arrive without moving the reader.
AutoScroll composes with turn anchoring. When a new turn anchors near the top, the view stays put while the reply streams into the room below it. Once the reply fills the viewport, the reader is back at the live edge and follow-output takes over from the anchor.
<MessageScrollerProvider AutoScroll="true">
<MessageScroller>...</MessageScroller>
</MessageScrollerProvider>Calling ScrollToEndAsync, or pressing MessageScrollerButton, re-engages follow-output when AutoScroll is enabled, so a reader who scrolled away can return to the live edge and keep following. The root and viewport expose data-autoscrolling while that programmatic scroll to the latest message runs, so you can conditionally apply styles during the transition.
Opening Saved Threads
It can seem reasonable to reopen a saved thread at the absolute end of the transcript, but that often drops the reader into the conversation without enough context. A better default is LastAnchor: show the last meaningful turn, like the user's latest message, with the reply below it.
That gives the reader an immediate place in the thread. They can see what they asked, where the answer starts, and continue from there without reconstructing the conversation from the bottom edge.
<MessageScrollerProvider DefaultScrollPosition="MessageScrollerScrollPosition.LastAnchor">
<MessageScroller>...</MessageScroller>
</MessageScrollerProvider>LastAnchor is keyed on ScrollAnchor, not message role. If no anchor exists, or the last anchored turn already fits in the viewport, it falls back to End.
Use Start when you want to resume at the beginning of a conversation, or End when the absolute latest message is the right place to land.
Avoiding a Flash on Reload
A scroll container always opens at the top. HTML has no way to set scrollTop, so a prerendered transcript shows the oldest messages first. Once the circuit connects, DefaultScrollPosition moves the view, and you see a jump.
When DefaultScrollPosition is End or LastAnchor, the viewport has data-pending-scroll until that position is applied. The styled viewport stays hidden while the attribute is present, so you see the frame instead of the jump. Start does not need this.
If you want End visible on first paint, add an inline script right after the viewport. Give the viewport an id, scroll it to the bottom, and remove data-pending-scroll.
<MessageScroller>
<MessageScrollerViewport id="messages">
<MessageScrollerContent>...</MessageScrollerContent>
</MessageScrollerViewport>
<script>
(function () {
var viewport = document.getElementById("messages")
if (!viewport) {
return
}
viewport.scrollTop = viewport.scrollHeight
viewport.removeAttribute("data-pending-scroll")
})()
</script>
<MessageScrollerButton />
</MessageScroller>Put the script in your page, not in the scroller. It only works for End, and only when the messages are already in the prerendered HTML. If you use a Content Security Policy, pass a nonce.
Do not use this script with LastAnchor. Skip it when messages load after the first render.
Loading Earlier Messages
Loading earlier messages should not move the conversation the reader is already looking at. When older rows are prepended above the current transcript, MessageScrollerViewport preserves the visible row so the reader stays in the same place while history loads above them.
This is enabled by default through PreserveScrollOnPrepend.
Use stable MessageId values for message rows. That gives the scroller a specific row to preserve instead of guessing from whichever pixel happens to sit at the viewport edge.
Animating New Messages
MessageScrollerItem can be animated directly. Keep MessageId and ScrollAnchor on it, and use transform and opacity for the entrance.
A common chat pattern is to animate the user's message when it is sent, then let the assistant reply stream into a regular row below it. Start the user row below its final position so it feels like it rises from the live edge of the viewport.
<MessageScrollerItem MessageId="@message.Id" ScrollAnchor="true"
Class="animate-in fade-in slide-in-from-bottom-4 duration-300 motion-reduce:animate-none">
<Message />
</MessageScrollerItem>Avoid animating height, margin, or padding for row entrances; those changes can fight the scroller's positioning work. If the reader prefers reduced motion, skip the entrance animation and keep the scroll behavior the same.
Jumping to Messages
Search results, permalinks, outline items, and toolbar buttons often need to drive the transcript from outside the message list. Keep a @ref to MessageScrollerProvider for those controls. The provider renders no element, so the controls can sit anywhere inside it, including outside the MessageScroller frame.
<MessageScrollerProvider @ref="_scroller">
<Button @onclick="() => _scroller.ScrollToMessageAsync(id)">Jump</Button>
<MessageScroller>...</MessageScroller>
</MessageScrollerProvider>
@code {
private MessageScrollerProvider _scroller = null!;
}The provider has ScrollToStartAsync, ScrollToEndAsync and ScrollToMessageAsync, which also takes scroll options. ScrollToMessageAsync targets the MessageId on MessageScrollerItem, so rows that need to be addressable should have stable ids. It returns false when the target is not mounted and cannot be queued.
ScrollToMessageAsync can queue a target before items exist, which covers permalinks while the transcript mounts. After rows have mounted, a missing id returns false instead of starting a guessed retry loop. A true result means the scroll ran or was queued, not that the row is already in view.
Tracking the Reader's Position
Use MessageScrollerVisibility to track the reader's position in the conversation. A common example is a table-of-contents or a jump menu that highlights the current anchored turn.
<MessageScrollerVisibility Context="visibility">
<span>Current turn: @visibility.CurrentAnchorId</span>
</MessageScrollerVisibility>CurrentAnchorId answers "where am I" by reporting the current anchored turn, and it stays set after that anchor scrolls above the viewport. VisibleMessageIds answers "what is on screen", in document order.
Visibility is pay-for-what-you-use. Tracking only runs while a MessageScrollerVisibility is rendered, and rows need a MessageId to participate.
Reading Scroll State
Use MessageScrollerScrollable when you need scroll state in C#, such as a status indicator or a custom "jump to latest" control. It reports which edges the viewport can still scroll toward; "at the start/end" is the negation (!Start / !End), and "scrollable at all" is Start || End. For styling the scroller itself, prefer the data-scrollable attribute.
<MessageScrollerScrollable Context="scroll">
<span>@(scroll.End ? "Newer messages below" : "Up to date")</span>
</MessageScrollerScrollable>Performance
Our performance goal for MessageScroller is to keep the scroll hot path outside of component state: no rerenders for transcript rows, no forced layout on every scroll, and as little off-screen paint work as the browser can avoid.
Scroll position, anchoring, and follow-output are tracked in the browser and mirrored onto the root and viewport through data-* attributes, so scrolling and streaming do not rerender transcript rows or cross the circuit.
The styled MessageScrollerItem also ships with content-visibility: auto and contain-intrinsic-size. Rows stay in the DOM for selection, copy, find-in-page, prerendering, and assistive tech, but the browser can skip rendering work for rows far outside the viewport.
Visibility tracking is pay-for-what-you-use. A jump menu or active turn indicator costs nothing until a MessageScrollerVisibility renders.
This is comfortable for the expected range of a chat transcript: hundreds to low thousands of turns, including messages with markdown and composed components.
Virtualization
Virtualization is intentionally left outside the component. MessageScroller renders real DOM rows and stays fast well into the thousands of turns (see Performance), so most transcripts never need it.
When a transcript is large enough to need virtualization, render the rows with Blazor's Virtualize inside MessageScrollerContent. It finds the viewport as its nearest scrolling ancestor.
<MessageScrollerProvider>
<MessageScroller>
<MessageScrollerViewport>
<MessageScrollerContent Class="block min-h-full">
<Virtualize Items="messages" Context="message" ItemSize="86" OverscanCount="8">
<div @key="message.Id" class="pb-4">
<Message>@message.Content</Message>
</div>
</Virtualize>
</MessageScrollerContent>
</MessageScrollerViewport>
<MessageScrollerButton />
</MessageScroller>
</MessageScrollerProvider>Accessibility
MessageScroller keeps the scroll container keyboard reachable and the transcript announceable without forcing a specific message UI.
MessageScrollerViewport is a labelled, keyboard-focusable scroll region by default. It uses role="region", aria-label="Messages", and tabindex="0", so keyboard users can focus the transcript and scroll it directly.
MessageScrollerContent marks the transcript as a live region with role="log" and aria-relevant="additions". New rows can be announced, but streamed text mutations do not have to be announced token by token.
<MessageScrollerContent aria-busy="@(status == "streaming" ? "true" : "false")">
...
</MessageScrollerContent>Pass aria-busy while a turn streams if announcements should wait for the completed message row.
MessageScrollerButton renders a real button. When there is nothing to scroll toward, it sets inert, uses tabindex="-1", and exposes data-active="false" so inactive scroll controls do not create extra focus stops.
RTL
To enable RTL support in Blazix, see the RTL configuration guide.
Translated with AI for the demo, so it may have mistakes.
API Reference
MessageScroller
Other attributes are passed to the element it renders.
MessageScrollerViewport
Other attributes are passed to the element it renders.
MessageScrollerContent
Other attributes are passed to the element it renders.
MessageScrollerItem
Other attributes are passed to the element it renders.
MessageScrollerButton
Other attributes are passed to the element it renders.