Message scroller
A chat viewport that follows new messages while the reader is at the bottom. Adapted from beUI (MIT).
Give it a height: the transcript scrolls inside it. It stays at the end while the reader is within followThreshold (56px) of it, and stops following as soon as they scroll further up. With followOutput={false} the position is yours, through viewportRef.
Following new messages
Following the latest
"use client"
import { useState } from "react"
import { Button } from "@/components/ui/button"
import { Message, type MessageFrom } from "@/components/ui/message"
import { MessageScroller } from "@/components/ui/message-scroller"
const opening: [MessageFrom, string][] = [
["user", "What changed in the March release?"],
["assistant", "Twelve tasks shipped. The export moved into the project menu and invoices now show tax per line."],
["user", "Anything still open?"],
["assistant", "Three tasks. Two are in review and one has no owner yet."],
]
const updates = [
"The first review passed. Two tasks are left.",
"The second review passed. One task is left.",
"The last task has an owner and is due on Thursday.",
]
export default function MessageScrollerFollowingNewMessages() {
const [count, setCount] = useState(0)
const [following, setFollowing] = useState(true)
const turns = [...opening, ...updates.slice(0, count).map((text): [MessageFrom, string] => ["assistant", text])]
return (
<div className="flex w-full max-w-md flex-col gap-3">
<MessageScroller
label="Release questions"
className="h-36"
contentClassName="flex flex-col gap-4 py-1"
onFollowChange={setFollowing}
>
{turns.map(([from, text]) => (
<Message key={text} from={from}>
<p className={from === "user" ? "max-w-[85%] rounded-xl bg-muted px-3 py-2" : undefined}>{text}</p>
</Message>
))}
</MessageScroller>
<div className="flex items-center justify-between gap-3">
<p className="text-sm text-muted-foreground">{following ? "Following the latest" : "Paused while you read"}</p>
<Button variant="outline" onClick={() => setCount(count < updates.length ? count + 1 : 0)}>
{count < updates.length ? "Add a reply" : "Start again"}
</Button>
</div>
</div>
)
}
Busy while a reply streams
"use client"
import { useEffect, useState } from "react"
import { Button } from "@/components/ui/button"
import { Message } from "@/components/ui/message"
import { MessageScroller } from "@/components/ui/message-scroller"
const reply =
"The launch plan has four steps. First, freeze the task list on Monday. Second, run the final review on Tuesday and fix what it finds. Third, publish the release notes on Wednesday morning. Fourth, send the invoice for the milestone once the notes are live, and close the project board at the end of the week."
const words = reply.split(" ")
export default function MessageScrollerBusyWhileAReplyStreams() {
const [shown, setShown] = useState(words.length)
const busy = shown < words.length
useEffect(() => {
if (!busy) return
const timer = setTimeout(() => setShown((count) => count + 1), 60)
return () => clearTimeout(timer)
}, [busy, shown])
return (
<div className="flex w-full max-w-md flex-col gap-3">
<MessageScroller label="Launch plan" busy={busy} className="h-32" contentClassName="flex flex-col gap-4 py-1">
<Message from="user">
<p className="max-w-[85%] rounded-xl bg-muted px-3 py-2">Write the launch plan for this week.</p>
</Message>
<Message from="assistant">
<p>{words.slice(0, shown).join(" ")}</p>
</Message>
</MessageScroller>
<Button variant="outline" className="self-end" onClick={() => setShown(busy ? words.length : 0)}>
{busy ? "Finish now" : "Write it again"}
</Button>
</div>
)
}
Without following
"use client"
import { useRef } from "react"
import { Button } from "@/components/ui/button"
import { Message } from "@/components/ui/message"
import { MessageScroller } from "@/components/ui/message-scroller"
const steps = [
"Open the project and choose Export from its menu.",
"Pick the tasks to include. Closed tasks are left out unless you tick them.",
"Choose a format. A spreadsheet keeps one row per task, a document groups tasks by milestone.",
"Select Export. The file is saved to the project and a link is sent to you.",
"To share it, copy the link from the Files tab of the project.",
]
export default function MessageScrollerWithoutFollowing() {
const viewport = useRef<HTMLElement>(null)
return (
<div className="flex w-full max-w-md flex-col gap-3">
<MessageScroller
label="Export steps"
followOutput={false}
viewportRef={viewport}
className="h-40"
contentClassName="flex flex-col gap-4 py-1"
>
<Message from="user">
<p className="max-w-[85%] rounded-xl bg-muted px-3 py-2">How do I export a project?</p>
</Message>
<Message from="assistant" className="gap-2">
{steps.map((step) => (
<p key={step}>{step}</p>
))}
</Message>
</MessageScroller>
<Button
variant="outline"
className="self-end"
onClick={() => viewport.current?.scrollTo({ top: viewport.current.scrollHeight })}
>
Go to the end
</Button>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/message-scrollerbunx --bun shadcn@latest add @khaeli/message-scrollerInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- motion ^12.23.12
Keyboard
Tab- Moves focus to the transcript, which is always a tab stop, then on to what follows it.
Arrow UpArrow Down- Scroll the focused transcript by a line. Scrolling up past the threshold stops following new messages; scrolling back to the end resumes it.
Page UpPage DownHomeEnd- Scroll the focused transcript by a page, or to its start or end.
Accessible name
- The scrolling region is a
sectionnamed bylabel, Conversation unless you set it. Give each transcript on a page its own label. - The content is a
logwitharia-live="polite", so a message added to it is announced. - Set
busywhile a reply streams: it putsaria-busyon the log, so the reply is announced when it is complete and not word by word.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/message-scroller.json