On this page

Message scroller

Install, props, composables and the parts Message scroller is built from.

Install

Run this in the app, after shadcn-vue init and the Beehexa theme (Button › Code):

npx shadcn-vue@latest add https://beehexa.design/r/message-scroller.json

Use

The provider holds the scroll state; put it around the scroller and anything that calls the composables.

<script setup lang="ts">
import { ArrowDown } from '@lucide/vue'
import {
  MessageScroller,
  MessageScrollerButton,
  MessageScrollerContent,
  MessageScrollerItem,
  MessageScrollerProvider,
  MessageScrollerViewport,
} from '@/components/ui/message-scroller'

defineProps<{ messages: { id: string, text: string }[] }>()
</script>

<template>
  <MessageScrollerProvider auto-scroll>
    <MessageScroller class="h-96">
      <MessageScrollerViewport aria-label="Conversation with the warehouse">
        <MessageScrollerContent>
          <MessageScrollerItem v-for="message in messages" :key="message.id" :message-id="message.id">
            {{ message.text }}
          </MessageScrollerItem>
        </MessageScrollerContent>
      </MessageScrollerViewport>
      <MessageScrollerButton>
        <ArrowDown />
        <span class="sr-only">Scroll to the latest message</span>
      </MessageScrollerButton>
    </MessageScroller>
  </MessageScrollerProvider>
</template>

Props

Part Prop Values Notes
MessageScrollerProvider auto-scroll boolean Follows new messages while the reader is at the end. Off by default
MessageScrollerProvider default-scroll-position end (default), start, last-anchor Where the list opens
MessageScrollerProvider scroll-edge-threshold number (px) How close to an edge counts as at the edge. 8
MessageScrollerProvider scroll-previous-item-peek number (px) How much of the previous message stays visible when jumping to one. 64
MessageScrollerProvider scroll-margin number (px) Space kept above a message scrolled to the top. 0
MessageScrollerViewport preserve-scroll-on-prepend boolean Keeps the reader’s place when messages are added above. On
MessageScrollerContent spacer-class string Class for the hidden spacer that lets the last message reach the top
MessageScrollerItem message-id string Needed to scroll to a message and to track which are visible
MessageScrollerItem scroll-anchor boolean Marks a message last-anchor opens at, such as the first unread one
MessageScrollerButton direction end (default), start Which edge it jumps to
MessageScrollerButton behavior smooth (default), auto, instant Scroll behaviour
MessageScrollerButton variant, size Button variants secondary and icon-sm by default

Composables

Call them inside the provider.

Composable Returns
useMessageScroller() scrollToEnd, scrollToStart, scrollToMessage(id, options)
useMessageScrollerScrollable() start and end: whether there is more to read in each direction
useMessageScrollerVisibility() visibleMessageIds and currentAnchorId, for read receipts or a “new messages” marker

Built on

Plain elements and the browser’s own scrolling, with the shadcn-vue Message scroller API and scroll engine, kept as shadcn-vue ships them. Beehexa adds the styles: token scrollbar colours, a focus ring on the viewport (shadcn-vue has none), Beehexa easing for the jump button, and the Beehexa Button for it.

Limitations

  • The viewport’s default name (“Messages”) and the jump button’s hidden text are English. Pass your own, as above.