StickToBottomController
Keeps a scroller pinned to new content while the reader is at the bottom, and counts what arrives after they scroll up.
Reach for it in a chat, an agent log or a live feed, where new items and text that streams into the last item must stay in view until the reader scrolls up to read.
Import
import { StickToBottomController, createStickToBottomController } from '@ecopages/radiant-ui/controllers';Try it
The Chat log recipe runs it with a "new messages" button, streamed replies and older messages loaded at the top.
const stick = new StickToBottomController({
getScroller: () => scroller,
getContent: () => log,
onState: ({ atBottom, unseen }) => {
jump.hidden = atBottom;
jump.textContent = unseen ? `${unseen} new` : 'Jump to latest';
},
});
stick.attach();
jump.addEventListener('click', () => stick.scrollToBottom({ behavior: 'smooth' }));Config
| Option | Description |
|---|---|
getScroller | The scrolling element. Read on attach(). |
getContent | The element whose element children are the items, in order along the block axis. Read on attach(). Defaults to the scroller. |
threshold | How far from the end, in px, still counts as at the bottom. Default 16. |
onState | Receives { following, atBottom, unseen } on attach() and each time it changes. |
following is true while the controller keeps the end in view, atBottom while the scroller is within threshold of its end, and unseen counts the element children appended to the end of the content since following stopped.
Methods
| Method | Description |
|---|---|
scrollToBottom({ behavior? }) | Scrolls to the end, clears unseen and follows again. With 'smooth', content that changes size during the animation pins at once. |
release() | Stops following without scrolling, as a scroll up would. |
The state property holds the current { following, atBottom, unseen }.
Behavior
attach()scrolls to the end and starts following.- A scroll up that leaves the bottom stops following; a scroll up that stays within
thresholddoes not. A scroll down that reaches the bottom follows again. - While following, appended items, items that grow and a scroller that gets shorter keep the end in view. A
MutationObserversees items come and go, and aResizeObserverwatches the scroller, the content and each item. - While not following, the first visible item stays where it is when items are prepended or change size above it. The controller does this itself and sets
overflow-anchor: noneon the scroller while attached, because native scroll anchoring is missing in some browsers and would add its own correction in others. - When the content fits without scrolling, it follows, since there is nothing to scroll back to.
Lifecycle
attach() starts observing; attaching again with the same scroller and content changes nothing, and with others it moves there and follows again. destroy() stops observing and restores the scroller's inline overflow-anchor. scrollToBottom() and release() do nothing before attach() or after destroy().
With a host, createStickToBottomController(host, config) does the same on connect and disconnect; see bindController.
Accessibility: what the controller does and what you must provide
It writes no ARIA and moves no focus. Give a chat or log role="log", which is a polite live region, and set aria-busy="true" on an item while its text streams in, so a screen reader reads it once. Make the control that calls scrollToBottom a button, and move focus somewhere useful if it hides once clicked. A scroller with nothing focusable inside needs tabindex="0" and a name so the keyboard can scroll it.
Protected hooks
None. Change behavior through the config callbacks.