Message Scroller
A conversation that scrolls itself: it opens on the last turn, holds a new turn near the top while its reply arrives, follows the end until you scroll away, and keeps your place when older messages load above. In the look of the app's style.
Usage
import { Component, ViewEncapsulation } from '@angular/core';import { ScBubble, ScBubbleContent, ScFrame, ScMessage, ScMessageContent, ScMessageScroller, ScMessageScrollerButton, ScMessageScrollerContent, ScMessageScrollerItem, ScMessageScrollerViewport,} from '@semantic-components/ui';import { SiArrowDownIcon } from '@semantic-icons/lucide-icons';
@Component({ selector: 'app-message-scroller-usage-demo', imports: [ ScFrame, ScBubble, ScBubbleContent, ScMessage, ScMessageContent, ScMessageScroller, ScMessageScrollerButton, ScMessageScrollerContent, ScMessageScrollerItem, ScMessageScrollerViewport, SiArrowDownIcon, ], template: ` <div scFrame class="h-80 overflow-hidden"> <div scMessageScroller> <div scMessageScrollerViewport aria-label="Conversation"> <div scMessageScrollerContent class="p-4"> @for (message of messages; track message.id) { <div scMessageScrollerItem [messageId]="message.id" [scrollAnchor]="message.role === 'user'" > <div scMessage [alignment]="message.role === 'user' ? 'end' : 'start'" > <div scMessageContent> <div scBubble [alignment]="message.role === 'user' ? 'end' : 'start'" [variant]="message.role === 'user' ? 'default' : 'muted'" > <div scBubbleContent>{{ message.text }}</div> </div> </div> </div> </div> } </div> </div> <button scMessageScrollerButton> <svg siArrowDownIcon></svg> <span class="sr-only">Scroll to the latest message</span> </button> </div> </div> `, host: { class: 'mx-auto block w-full max-w-md' }, encapsulation: ViewEncapsulation.None,})export class MessageScrollerUsageDemo { private readonly messages = [ { id: 'q1', role: 'user', text: 'What does a message scroller do?' }, { id: 'a1', role: 'assistant', text: 'It keeps a conversation where you want it: at the end when it opens, on your question while the answer arrives, and on the message you are reading when older ones load.', }, { id: 'q2', role: 'user', text: 'And when I scroll up?' }, { id: 'a2', role: 'assistant', text: 'Then it stops following. New messages arrive without moving you, and a button appears to jump back to the latest one.', }, { id: 'q3', role: 'user', text: 'Can a keyboard scroll it?' }, { id: 'a3', role: 'assistant', text: 'Yes. The conversation is a region you can tab to, and the arrow keys, Page Up, Page Down, Home and End scroll it.', }, { id: 'q4', role: 'user', text: 'Do screen readers hear new messages?' }, { id: 'a4', role: 'assistant', text: 'They do: the messages are a log, so each new one is announced once it is added.', }, ];}Anatomy
Every part is a directive on your element; the messages and the button's icon and name are yours. Its state and commands, through exportAs: scrollable(), visibility(), scrollToEnd(), scrollToStart(), scrollToMessage(id).
import { ScMessageScroller, ScMessageScrollerViewport, ScMessageScrollerContent, ScMessageScrollerItem, ScMessageScrollerButton,} from '@semantic-components/ui';<div scMessageScroller autoScroll #scroller="scMessageScroller"> <div scMessageScrollerViewport aria-label="…"> <div scMessageScrollerContent> <div scMessageScrollerItem messageId="…" scrollAnchor>…</div> </div> </div> <button scMessageScrollerButton>…</button></div>Accessibility
- The viewport is a region you can tab to — name it (aria-label); the arrow keys, Page Up, Page Down, Home and End scroll it.
- The content is a log: each new message is announced once, as it is added. Keep history controls (Load earlier) outside it, or they are read as messages.
- The scroll button is hidden and inert while there is nothing to scroll to; name it in sr-only text.
Examples
Anchoring turns
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScBubble, ScBubbleContent, ScButton, ScFrame, ScMessage, ScMessageContent, ScMessageScroller, ScMessageScrollerButton,Group chat
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScBubble, ScBubbleContent, ScButton, ScFrame, ScMarker, ScMarkerContent, ScMessage, ScMessageContent,Keeping context visible
Peek
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScBubble, ScBubbleContent, ScButton, ScFrame, ScMessage, ScMessageContent, ScMessageScroller, ScMessageScrollerButton,Following the live edge
import { Component, DestroyRef, ViewEncapsulation, inject, signal,} from '@angular/core';import { ScBubble, ScBubbleContent,Opening saved threads
import { Component, ViewEncapsulation } from '@angular/core';import { ScBubble, ScBubbleContent, ScFrame, ScMessage, ScMessageContent, ScMessageScroller, ScMessageScrollerButton, ScMessageScrollerContent,Loading earlier messages
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScBubble, ScBubbleContent, ScButton, ScFrame, ScMessage, ScMessageContent, ScMessageScroller, ScMessageScrollerButton,Animating new messages
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScBubble, ScBubbleContent, ScButton, ScFrame, ScMessage, ScMessageContent, ScMessageScroller, ScMessageScrollerButton,Jumping to messages
Reading: —
import { Component, ViewEncapsulation, computed, viewChild,} from '@angular/core';import { ScBubble, ScBubbleContent, ScButton,Tracking the reader’s position
import { Component, ViewEncapsulation, computed, signal } from '@angular/core';import { ScBubble, ScBubbleContent, ScButton, ScFrame, ScMessage, ScMessageContent, ScMessageScroller, ScMessageScrollerButton,Reading scroll state
At the start · At the end
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScBubble, ScBubbleContent, ScFrame, ScMessage, ScMessageContent, ScMessageScroller, ScMessageScrollerButton, ScMessageScrollerContent,RTL
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import { ScBubble, ScBubbleContent, ScFrame, ScMessage, ScMessageContent, ScMessageScroller, ScMessageScrollerButton,API reference
Each part takes class, merged over the style (cn: a later class wins).
| Part | Selector | Description |
|---|---|---|
ScMessageScroller | div[scMessageScroller] | autoScroll, defaultScrollPosition ('end', 'last-anchor', 'start'), scrollPreviousItemPeek (64), scrollEdgeThreshold, scrollMargin. exportAs: scrollable(), visibility(), scrollToEnd(), scrollToStart(), scrollToMessage(id). |
ScMessageScrollerViewport | div[scMessageScrollerViewport] | The scrolling region (name it); hidden until its opening position is applied. |
ScMessageScrollerContent | div[scMessageScrollerContent] | The rows, a log: each new one announced. |
ScMessageScrollerItem | div[scMessageScrollerItem] | A row: messageId (to jump to it), scrollAnchor (a new turn starts here). |
ScMessageScrollerButton | button[scMessageScrollerButton] | To the end (direction 'end') or the start; the style's button, shown when there is somewhere to go. |