Skip to content
GitHub11 stars

Message Scroller

A chat scroll container that anchors turns, follows streamed responses, loads history without jumping and jumps to any message.

Hi! Did you get a chance to review the design?
Yes, I left a few comments on the layout.
Thanks. I'll update the spacing and the colors.
Great. The new header looks much cleaner.
Should we add a dark theme too?
Definitely, most of our users asked for it.
<div class="message-scroller-container">
    <MessageScrollerProvider AutoScroll="true" DefaultScrollPosition="MessageScrollerScrollPosition.End">
        <MessageScrollerRoot class="message-scroller-root">

These are the headless parts of Message Scroller, which the styled Message Scroller is built on. The provider owns the behavior: it follows new messages while the reader is at the end, keeps their place while they read earlier ones, and holds the scroll position steady when history loads above.

Anatomy

Import the primitives and assemble the parts:

@using Blazix.Primitives

<MessageScrollerProvider>
    <MessageScrollerRoot>
        <MessageScrollerViewport>
            <MessageScrollerContent>
                <MessageScrollerItem />
            </MessageScrollerContent>
        </MessageScrollerViewport>
        <MessageScrollerButton />
    </MessageScrollerRoot>
</MessageScrollerProvider>

Scrolling from code

Keep a @ref to the provider for its commands: ScrollToStartAsync, ScrollToEndAsync, and ScrollToMessageAsync for an item with a MessageId.

<MessageScrollerProvider @ref="_scroller">
    ...
</MessageScrollerProvider>
<button type="button" @onclick="() => _scroller!.ScrollToMessageAsync(firstUnreadId)">First unread</button>

@code {
    private MessageScrollerProvider? _scroller;
}

MessageScrollerScrollable renders its content with whether there's more to scroll toward each edge, and MessageScrollerVisibility with the messages in view and the anchored turn, again whenever they change.

API Reference

MessageScrollerProvider

Doesn't render its own HTML element.

ParameterTypeDefault
AutoScroll bool false
DefaultScrollPosition MessageScrollerScrollPosition MessageScrollerScrollPosition.End
ScrollEdgeThreshold int 8
ScrollPreviousItemPeek int 64
ScrollMargin int 0
ChildContent

The part's content.

RenderFragment? -

MessageScrollerRoot

Renders a <div> element.

ParameterTypeDefault
Render RenderFragment<PartRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-scrollable

The edges there's more to scroll toward.

'start' | 'end' | 'start end'
data-autoscrolling

Present while the scroller follows new content to the end.

-
data-pending-scroll

Present until the scroller has opened at its DefaultScrollPosition (End or LastAnchor), so you can hide the messages until then.

-

MessageScrollerViewport

Renders a <div> element.

ParameterTypeDefault
PreserveScrollOnPrepend bool true
Render RenderFragment<PartRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-scrollable

The edges there's more to scroll toward.

'start' | 'end' | 'start end'
data-autoscrolling

Present while the scroller follows new content to the end.

-
data-pending-scroll

Present until the scroller has opened at its DefaultScrollPosition (End or LastAnchor), so you can hide the messages until then.

-

MessageScrollerContent

Renders a <div> element.

ParameterTypeDefault
SpacerClass string? -
Render RenderFragment<PartRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

MessageScrollerItem

Renders a <div> element.

ParameterTypeDefault
MessageId string? -
ScrollAnchor bool false
Render RenderFragment<PartRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-message-id

The item's MessageId.

string
data-scroll-anchor

Present when the item is a scroll anchor.

-

MessageScrollerButton

Renders a <button> element.

ParameterTypeDefault
Direction MessageScrollerDirection MessageScrollerDirection.End
Behavior ScrollBehavior ScrollBehavior.Smooth
NativeButton bool? -
Render RenderFragment<MessageScrollerButtonRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-active

Whether there's more to scroll toward the button's edge. While it's false the button is inert.

'true' | 'false'

MessageScrollerScrollable

Doesn't render its own HTML element.

ParameterTypeDefault
ChildContent

The part's content.

RenderFragment<MessageScrollerScrollState>? -

MessageScrollerVisibility

Doesn't render its own HTML element.

ParameterTypeDefault
ChildContent

The part's content.

RenderFragment<MessageScrollerVisibilityState>? -