Message Scroller
shadcn/uiScrollable message viewport with sticky-to-bottom tracking and scroll-to-latest control.
Message Scroller
Welcome! Scroll up to reveal the scroll-to-latest control.
How does sticky-to-bottom tracking work when new items arrive?
When you stay at the bottom with autoScroll enabled, new messages keep the viewport pinned.
What happens if I scroll up to read earlier messages?
Auto-scrolling pauses automatically so your reading position stays fixed.
And the floating button lets me jump right back down?
Yes. Click it anytime to smoothly scroll back to the latest message.
import { useState } from "react";import { Plus } from "lucide-react";import { Button } from "@/components/optics/button";import { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport,} from "@/components/optics/message-scroller";const [messages, setMessages] = useState(initialMessages);function handleAddMessage() { setMessages((prev) => [ ...prev, { id: `msg-${prev.length + 1}`, sender: prev.length % 2 === 0 ? "Assistant" : "You", text: `Message #${prev.length + 1} added to test sticky scrolling.`, }, ]);}<div className="flex w-full max-w-md flex-col items-center gap-4"> <MessageScrollerProvider autoScroll> <MessageScroller className="h-64 w-full max-w-md rounded-xl border bg-background"> <MessageScrollerViewport> <MessageScrollerContent className="gap-3 p-4"> <MessageScrollerItem className="flex flex-col" messageId="msg-1" > <div className="max-w-[80%] self-start rounded-xl bg-muted px-3 py-2 text-sm text-foreground"> Welcome! Scroll up to reveal the scroll-to-latest control. </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-2" > <div className="max-w-[80%] self-end rounded-xl bg-primary px-3 py-2 text-sm text-primary-foreground"> How does sticky-to-bottom tracking work when new items arrive? </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-3" > <div className="max-w-[80%] self-start rounded-xl bg-muted px-3 py-2 text-sm text-foreground"> When you stay at the bottom with autoScroll enabled, new messages keep the viewport pinned. </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-4" > <div className="max-w-[80%] self-end rounded-xl bg-primary px-3 py-2 text-sm text-primary-foreground"> What happens if I scroll up to read earlier messages? </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-5" > <div className="max-w-[80%] self-start rounded-xl bg-muted px-3 py-2 text-sm text-foreground"> Auto-scrolling pauses automatically so your reading position stays fixed. </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-6" > <div className="max-w-[80%] self-end rounded-xl bg-primary px-3 py-2 text-sm text-primary-foreground"> And the floating button lets me jump right back down? </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-7" > <div className="max-w-[80%] self-start rounded-xl bg-muted px-3 py-2 text-sm text-foreground"> Yes. Click it anytime to smoothly scroll back to the latest message. </div> </MessageScrollerItem> </MessageScrollerContent> </MessageScrollerViewport> <MessageScrollerButton /> </MessageScroller> </MessageScrollerProvider> <Button onClick={onClick} size="sm" variant="outline" > <Plus /> Add message </Button></div>import { useState } from "react";import { Plus } from "lucide-react";import { Button } from "@/components/optics/button";import { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport,} from "@/components/optics/message-scroller";const [messages, setMessages] = useState(initialMessages);function handleAddMessage() { setMessages((prev) => [ ...prev, { id: `msg-${prev.length + 1}`, sender: prev.length % 2 === 0 ? "Assistant" : "You", text: `Message #${prev.length + 1} added to test sticky scrolling.`, }, ]);}<div className="flex w-full max-w-md flex-col items-center gap-4"> <MessageScrollerProvider autoScroll> <MessageScroller className="h-64 w-full max-w-md rounded-xl border bg-background"> <MessageScrollerViewport> <MessageScrollerContent className="gap-3 p-4"> <MessageScrollerItem className="flex flex-col" messageId="msg-1" > <div className="max-w-[80%] self-start rounded-xl bg-muted px-3 py-2 text-sm text-foreground"> Welcome! Scroll up to reveal the scroll-to-latest control. </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-2" > <div className="max-w-[80%] self-end rounded-xl bg-primary px-3 py-2 text-sm text-primary-foreground"> How does sticky-to-bottom tracking work when new items arrive? </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-3" > <div className="max-w-[80%] self-start rounded-xl bg-muted px-3 py-2 text-sm text-foreground"> When you stay at the bottom with autoScroll enabled, new messages keep the viewport pinned. </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-4" > <div className="max-w-[80%] self-end rounded-xl bg-primary px-3 py-2 text-sm text-primary-foreground"> What happens if I scroll up to read earlier messages? </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-5" > <div className="max-w-[80%] self-start rounded-xl bg-muted px-3 py-2 text-sm text-foreground"> Auto-scrolling pauses automatically so your reading position stays fixed. </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-6" > <div className="max-w-[80%] self-end rounded-xl bg-primary px-3 py-2 text-sm text-primary-foreground"> And the floating button lets me jump right back down? </div> </MessageScrollerItem> <MessageScrollerItem className="flex flex-col" messageId="msg-7" > <div className="max-w-[80%] self-start rounded-xl bg-muted px-3 py-2 text-sm text-foreground"> Yes. Click it anytime to smoothly scroll back to the latest message. </div> </MessageScrollerItem> </MessageScrollerContent> </MessageScrollerViewport> <MessageScrollerButton /> </MessageScroller> </MessageScrollerProvider> <Button onClick={onClick} size="sm" variant="outline" > <Plus /> Add message </Button></div>Installation
pnpm dlx shadcn@latest add @optics/message-scroller
Props
<MessageScrollerItem />
Name | Type | |
|---|---|---|
scrollAnchor | boolean(default: false) |
<MessageScrollerButton />
Name | Type | |
|---|---|---|
direction | MessageScrollerButtonDirection(default: end) | |
render | RenderProp<MessageScrollerButtonRenderState> | |
variant | "link" | "outline" | "default"(default: secondary) |