Keeps the newest message in view, and lets you scroll back without losing your place.
import { Subscription, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schedule, Schema, Stream as EffectStream } from "effect"import type { Actor } from "./ui/uicontext/components/domain/Actor.ts"import { ConversationViewport } from "./ui/uicontext/components/kit/ConversationViewport.ts"import { Stream } from "./ui/uicontext/components/kit/Stream.ts"import { Thread, ThreadMessage } from "./ui/uicontext/components/kit/Thread.ts"import { Button } from "./ui/uicontext/components/Button.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { space } from "./ui/uicontext/components/tokens.stylex.ts"const ada: Actor = { id: "ada", name: "Ada Lovelace", kind: "person" }const scout: Actor = { id: "scout", name: "Scout", kind: "agent" }const questions = [ "What changed in checkout this week?", "Which of those touch tax?", "Who reviewed the VAT change?", "Did it ship behind a flag?", "How many EU invoices went out since?", "Draft a note to finance about the double VAT.",]const answers = [ "Four changes merged: the wallet button, a retry for declined cards, VAT on invoices, and a new receipt email.", "One: **PAY-415** moved VAT into the invoice line items, and the old total still adds it.", "Mira Patel reviewed it on Tuesday. The tests cover line items, not the total.", "No. It went out to everyone with Tuesday's 5 PM deploy.", "1,284 EU invoices since the deploy. 212 of them were paid by card.",]const history: ReadonlyArray<ThreadMessage> = answers.flatMap((answer, i) => [ { id: `q${i}`, actor: ada, at: `2026-12-01T09:${String(10 + i * 4).padStart(2, "0")}:00Z`, body: questions[i]! }, { id: `a${i}`, actor: scout, at: `2026-12-01T09:${String(11 + i * 4).padStart(2, "0")}:00Z`, body: answer },])const REPLY = `Here's a draft:> **Subject:** Double VAT on EU invoices since Tuesday>> Since Tuesday's 5 PM release, EU invoices have shown VAT twice in the total. 1,284 invoices are affected; 212 were paid by card and were overcharged by the VAT amount.>> We've found the cause and will ship a fix today. We'd like to refund the 212 card payments and reissue the other invoices. Can you confirm the refund approach before we start?Want me to attach the list of invoices?`const CHUNKS = REPLY.match(/[\s\S]{1,5}/gu) ?? []const Message = UiSchema.TaggedEnum({ ReceivedChunk: { index: Schema.Number }, ClickedSendFollowUp: {}, ClickedStop: {},})// Stands in for the model's stream.const tokens = (run: number) => Subscription.make("viewport-tokens", run, () => EffectStream.fromIterable(CHUNKS.map((_, index) => index)).pipe( EffectStream.schedule(Schedule.spaced("60 millis")), EffectStream.map((index) => Message.ReceivedChunk({ index })), ), )const styles = stylex.create({ frame: { display: "grid", gap: space.md, inlineSize: "min(100%, 38rem)" },})export const FollowingReply = Html.component({ name: "FollowingReply", model: Schema.Struct({ messages: Schema.Array(ThreadMessage), received: Schema.Number, streaming: Schema.Boolean, run: Schema.Number, }), message: Message,}).make({ init: () => ({ messages: [...history, { id: "q5", actor: ada, at: "2026-12-01T09:31:00Z", body: questions[5]! }], received: 0, streaming: true, run: 1, }), subscriptions: ($) => ($.model.streaming ? [tokens($.model.run)] : []), update: { ReceivedChunk: ($, { index }) => $.model.streaming ? $.evolve({ received: index + 1, streaming: index + 1 < CHUNKS.length }) : $.model, ClickedStop: ($) => $.evolve({ streaming: false }), // the reply so far joins the thread, then the follow-up, then a fresh reply starts ClickedSendFollowUp: ($) => { if ($.model.received === 0) return $.model const n = $.model.messages.length return $.evolve({ messages: (all) => [ ...all, { id: `r${n}`, actor: scout, at: "2026-12-01T09:32:00Z", body: CHUNKS.slice(0, $.model.received).join("") }, { id: `q${n + 1}`, actor: ada, at: "2026-12-01T09:33:00Z", body: "Yes, attach the list and send it to me first." }, ], received: 0, streaming: true, run: (r) => r + 1, }) }, }, view: (h, model) => { const mine = model.messages.filter((m) => m.actor.id === ada.id).at(-1)?.id return h.div.attrs(sx(styles.frame)).children( ConversationViewport(h, { props: { label: "Conversation with Scout", maxHeight: 380, ...(mine === undefined ? {} : { latest: mine }) }, slots: { content: Thread(h, { messages: model.messages, me: ada.id, timeZone: "UTC", label: "Messages", pending: { actor: scout, children: [ Stream(h, { key: `viewport-reply-${model.run}`, status: model.streaming ? (model.received === 0 ? "waiting" : "streaming") : "succeeded", text: CHUNKS.slice(0, model.received).join(""), onStop: Message.ClickedStop(), }), ], }, }), }, }), // never disabled: focus stays on the button after a send Button(h, {}) .on("click", Message.ClickedSendFollowUp()) .children("Send “Yes, attach the list”"), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/conversation-viewportUsage
import { ConversationViewport } from "./ui/uicontext/components/kit/ConversationViewport.ts"import { Thread, type ThreadMessage } from "./ui/uicontext/components/kit/Thread.ts"export const Transcript = <M>(h: Html.H<M>, messages: ReadonlyArray<ThreadMessage>, me: string) => ConversationViewport(h, { props: { label: "Conversation with Scout", maxHeight: 480, latest: messages.filter((m) => m.actor.id === me).at(-1)?.id, }, slots: { content: Thread(h, { messages, me }) }, })Examples
A long session
Four hundred messages from a day with a coding agent. It opens at the newest, and scrolling back stays smooth.
import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import type { Actor } from "./ui/uicontext/components/domain/Actor.ts"import { ConversationViewport } from "./ui/uicontext/components/kit/ConversationViewport.ts"import { Thread, type ThreadMessage } from "./ui/uicontext/components/kit/Thread.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const grace: Actor = { id: "grace", name: "Grace Hopper", kind: "person" }const forge: Actor = { id: "forge", name: "Forge", kind: "agent" }const asks = [ "Run the billing tests again.", "Why did `invoice.spec.ts` fail?", "Try the fix with the rounding helper instead.", "Show me the diff for `tax.ts`.", "Commit that with a short message.",]const replies = [ "Ran 212 tests: 211 passed, 1 failed in `invoice.spec.ts`.", "The total rounds each line before adding VAT, so 3 lines of €0.335 come to €1.02 instead of €1.01.", "Done. `roundCurrency` now runs once on the total. All 212 tests pass.", "```diff\n- const total = lines.map(round).reduce(add)\n+ const total = round(lines.reduce(add))\n```", "Committed `a91f3c2`: Round invoice totals once.",]// 200 rounds of five kinds of exchange, two minutes apartconst messages: ReadonlyArray<ThreadMessage> = Array.from({ length: 200 }, (_, i) => { const minute = 8 * 60 + i * 2 const at = (offset: number) => `2026-12-01T${String(Math.floor((minute + offset) / 60) % 24).padStart(2, "0")}:${String((minute + offset) % 60).padStart(2, "0")}:00Z` return [ { id: `ask-${i}`, actor: grace, at: at(0), body: asks[i % asks.length]! }, { id: `reply-${i}`, actor: forge, at: at(1), body: replies[i % replies.length]! }, ]}).flat()const styles = stylex.create({ frame: { inlineSize: "min(100%, 38rem)" } })export const LongSession = Html.component({ name: "LongSession", model: Schema.Struct({}), message: Schema.Never }).make({ init: () => ({}), update: ($) => $.model, view: (h) => h.div.attrs(sx(styles.frame)).children( ConversationViewport(h, { props: { label: "Session with Forge", maxHeight: 420 }, slots: { content: Thread(h, { messages, me: grace.id, timeZone: "UTC", label: "Session messages" }) }, }), ),})API reference
Props
| Name | Type | Description |
|---|---|---|
labelrequired | string | The region's accessible name: "Conversation with the agent" |
maxHeight | number | The tallest the viewport grows, in CSS pixels; without it, a constrained parent sizes it |
latest | string | The id of the reader's own newest message. When it changes (they sent something), the view jumps to the bottom and follows again, even if they had scrolled back. |
unread | string | The element id of the unread line: the view opens there, and a pill leads back to it |
unreadLabel | string | The pill's words while the unread line is above the view: "3 new messages" |
Events
| Event | Payload | Description |
|---|---|---|
ReachedStart | {} | The reader scrolled to the top: load older messages, if there are any |
Slots
content
Accessibility
- The conversation is a named region that takes keyboard focus, so the arrow keys and Page Up and Page Down scroll it.
- Scrolling up stops following new messages; Jump to latest scrolls to the end and moves focus back to the conversation.
- If the app already owns the scroll region, mount
AttachConversationViewporton that same named, focusable element. Keep the growing content in its first child and use the mount'scanJumpstate to render the Jump to latest button.