Every example is a complete Vue file. Copy it into an app that has the registry installed. Click into a list, or Tab to it, and scroll with the arrow keys.
Follow new messages
auto-scroll keeps the list at the latest message while you are at the end. Scroll up and press Get a reply: the list stays where you are, and the round button takes you back to the end.
MessageScrollerChat.vue
<script setup lang="ts">
import { ArrowDown, MessageSquarePlus } from '@lucide/vue'
import { ref } from 'vue'
import { Bubble, BubbleContent } from '@/components/ui/bubble'
import { Button } from '@/components/ui/button'
import { Message, MessageContent, MessageHeader } from '@/components/ui/message'
import {
MessageScroller,
MessageScrollerButton,
MessageScrollerContent,
MessageScrollerItem,
MessageScrollerProvider,
MessageScrollerViewport,
} from '@/components/ui/message-scroller'
interface ChatMessage { id: string, mine: boolean, text: string }
const lines = [
'Morning. The courier picked up 48 parcels at 08:10.',
'Thanks. Did order 10482 go with them?',
'Yes, it was in the first batch.',
'Good. The customer asked for the tracking number.',
'It is in the order notes in the ERP now.',
'Got it. Can you also check the stock for the blue mugs?',
'We have 36 left. The online store shows 40.',
'I will start a stock sync so the store matches.',
]
const replies = [
'Sync looks done on my side: 36 in both.',
'The next pickup is at 15:00.',
'Two returns came in. I put them in the CRM.',
]
const messages = ref<ChatMessage[]>(lines.map((text, index) => ({ id: `m${index}`, mine: index % 2 === 1, text })))
function addReply() {
const text = replies[messages.value.length % replies.length]!
messages.value.push({ id: `m${messages.value.length}`, mine: false, text })
}
</script>
<template>
<div class="flex w-full max-w-120 flex-col gap-(--bh-space-100)">
<MessageScrollerProvider auto-scroll>
<MessageScroller class="h-80 rounded-(--bh-radius-card) border-(length:--bh-border-width-default) border-(--bh-color-border) bg-(--bh-color-surface)">
<MessageScrollerViewport aria-label="Conversation with the warehouse">
<MessageScrollerContent class="gap-(--bh-space-200) p-(--bh-space-200)">
<MessageScrollerItem v-for="message in messages" :key="message.id" :message-id="message.id">
<Message :align="message.mine ? 'end' : 'start'">
<MessageContent>
<MessageHeader>{{ message.mine ? 'You' : 'Warehouse' }}</MessageHeader>
<Bubble :variant="message.mine ? 'default' : 'secondary'">
<BubbleContent>{{ message.text }}</BubbleContent>
</Bubble>
</MessageContent>
</Message>
</MessageScrollerItem>
</MessageScrollerContent>
</MessageScrollerViewport>
<MessageScrollerButton>
<ArrowDown />
<span class="sr-only">Scroll to the latest message</span>
</MessageScrollerButton>
</MessageScroller>
</MessageScrollerProvider>
<div>
<Button variant="outline" @click="addReply"><MessageSquarePlus />Get a reply</Button>
</div>
</div>
</template>Load older messages
Older notes are added above. The list keeps what you were reading in place instead of jumping. The button at the top goes back to the first note.
MessageScrollerHistory.vue
<script setup lang="ts">
import { ArrowDown, History } from '@lucide/vue'
import { ref } from 'vue'
import { Bubble, BubbleContent } from '@/components/ui/bubble'
import { Button } from '@/components/ui/button'
import { Message, MessageContent, MessageFooter } from '@/components/ui/message'
import {
MessageScroller,
MessageScrollerButton,
MessageScrollerContent,
MessageScrollerItem,
MessageScrollerProvider,
MessageScrollerViewport,
} from '@/components/ui/message-scroller'
interface Note { id: number, text: string, day: string }
// Notes on a CRM account, oldest first. Older pages load above without moving what you are reading.
const older: Note[][] = [
[
{ id: 1, text: 'Account opened. Pays by bank transfer, 30 days.', day: '3 March' },
{ id: 2, text: 'First wholesale order: 120 mugs.', day: '10 March' },
],
]
const notes = ref<Note[]>([
{ id: 3, text: 'Asked for a price list for cafés.', day: '2 April' },
{ id: 4, text: 'Price list sent. Follow up next week.', day: '4 April' },
{ id: 5, text: 'Second order placed in the online store.', day: '12 April' },
{ id: 6, text: 'Moved to the wholesale price group in the ERP.', day: '15 April' },
])
const loaded = ref(0)
function loadOlder() {
const page = older[loaded.value]
if (!page) return
notes.value = [...page, ...notes.value]
loaded.value += 1
}
</script>
<template>
<MessageScrollerProvider default-scroll-position="end">
<MessageScroller class="h-72 w-full max-w-120 rounded-(--bh-radius-card) border-(length:--bh-border-width-default) border-(--bh-color-border) bg-(--bh-color-surface)">
<MessageScrollerViewport aria-label="Account notes">
<MessageScrollerContent class="gap-(--bh-space-150) p-(--bh-space-200)">
<div class="flex justify-center">
<Button v-if="loaded < older.length" variant="ghost" size="sm" @click="loadOlder"><History />Load older notes</Button>
</div>
<MessageScrollerItem v-for="note in notes" :key="note.id" :message-id="String(note.id)">
<Message>
<MessageContent>
<Bubble variant="muted">
<BubbleContent>{{ note.text }}</BubbleContent>
</Bubble>
<MessageFooter>{{ note.day }}</MessageFooter>
</MessageContent>
</Message>
</MessageScrollerItem>
</MessageScrollerContent>
</MessageScrollerViewport>
<MessageScrollerButton direction="start">
<ArrowDown />
<span class="sr-only">Scroll to the first note</span>
</MessageScrollerButton>
</MessageScroller>
</MessageScrollerProvider>
</template>