← Components

Communication

Message scroller

The conversation container. Sticks to the bottom while text streams in; if you scroll up, it never steals your place and shows a “New messages” pill instead.

Examples and variants

Live

Scroll up, then add a message.

Hello!
How can I help?
Example
function ScrollerLive() {
  const [lines, setLines] = useState(["Hello!", "How can I help?"])
  return (
    <div className="flex h-64 w-full max-w-sm flex-col rounded-(--shape-lg) bg-canvas p-3 shadow-1">
      <MessageScroller>
        {lines.map((line, i) => (
          <Bubble key={i} from={i % 2 ? "agent" : "user"}>
            {line}
          </Bubble>
        ))}
      </MessageScroller>
      <Button size="sm" variant="secondary" onClick={() => setLines((l) => [...l, `Message ${l.length + 1}`])}>
        Add a message (scroll up first to see the pill)
      </Button>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/message-scroller.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn lucide-react
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/message-scroller.tsxmessage-scroller
      components/ui/message-scroller.tsx
      "use client"
      
      import * as React from "react"
      import { ArrowDownIcon } from "lucide-react"
      import { cn } from "cn"
      
      /*
        Message scroller: the conversation container. Sticks to the bottom while
        new content streams in (smoothly, never jumping). If you scroll up to read,
        it never steals your place: a "New messages" pill bounces in instead, and
        pressing it glides back down.
      */
      function MessageScroller({ className, children, ...props }: React.ComponentProps<"div">) {
        const ref = React.useRef<HTMLDivElement>(null)
        const pinned = React.useRef(true)
        const [unseen, setUnseen] = React.useState(false)
      
        React.useEffect(() => {
          const node = ref.current
          if (!node) return
          const content = node.firstElementChild as HTMLElement | null
          const onScroll = () => {
            pinned.current = node.scrollHeight - node.scrollTop - node.clientHeight < 24
            if (pinned.current) setUnseen(false)
          }
          const onGrow = () => {
            if (pinned.current) node.scrollTo({ top: node.scrollHeight, behavior: "smooth" })
            else setUnseen(true)
          }
          node.addEventListener("scroll", onScroll, { passive: true })
          const observer = new ResizeObserver(onGrow)
          if (content) observer.observe(content)
          node.scrollTop = node.scrollHeight
          return () => {
            node.removeEventListener("scroll", onScroll)
            observer.disconnect()
          }
        }, [])
      
        return (
          <div className="relative min-h-0 flex-1">
            <div
              ref={ref}
              role="log"
              aria-live="polite"
              data-slot="message-scroller"
              className={cn("size-full overflow-y-auto overscroll-contain", className)}
              {...props}
            >
              <div className="flex flex-col gap-stack-lg py-4">{children}</div>
            </div>
            {unseen ? (
              <button
                type="button"
                onClick={() => ref.current?.scrollTo({ top: ref.current.scrollHeight, behavior: "smooth" })}
                className="scroller-pill liquid-press absolute bottom-3 left-1/2 inline-flex -translate-x-1/2 items-center gap-1 rounded-(--shape-full) bg-(--scroller-pill) px-3 py-1.5 text-xs font-medium text-(--scroller-pill-label) shadow-2 outline-none focus-visible:shadow-(--focus-ring)"
              >
                New messages
                <ArrowDownIcon className="size-(--size-icon-sm)" />
              </button>
            ) : null}
          </div>
        )
      }
      
      export { MessageScroller }
      

Imports

What the examples on this page import:

Imports
import { Attachment } from "@/components/ui/attachment"
import { Avatar, AvatarFallback } from "@/components/ui/avatar"
import { Bubble, BubbleGroup, BubbleReactions } from "@/components/ui/bubble"
import { Button } from "@/components/ui/button"
import { CodeBlock } from "@/components/ui/code-block"
import { Marker } from "@/components/ui/marker"
import { Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader } from "@/components/ui/message"
import { MessageScroller } from "@/components/ui/message-scroller"
import { PromptInput } from "@/components/ui/prompt-input"
import { Questionnaire } from "@/components/ui/questionnaire"
import { StreamingText } from "@/components/ui/streaming-text"
import { Trace, TraceItem } from "@/components/ui/trace"
import { VoiceInput } from "@/components/ui/voice-input"
import { tokenMs } from "@/lib/motion/token-ms"

Anatomy

  1. 1Log · role=log, polite.
  2. 2Pill · --scroller-pill, --scroller-pill-label.

Props

PropTypeDefaultDescription
childrenMessage[]—The thread.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--scroller-pillvar(--surface-inverse)oklch(0.22 0.01 260)oklch(0.95 0 0)
--scroller-pill-labelvar(--on-surface-inverse)oklch(0.98 0.004 95)oklch(0.14 0 0)

Motion

TriggerWhat happensTokens
New contentSmooth scroll if pinned; otherwise the pill bounces in.--duration-medium

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Never moves the reader's position while they read.
  • New messages are announced politely.

Usage

Do

  • Give it a height (flex-1 in a column).

Don’t

  • Don't use for non-chat lists.

Code

components/ui/message-scroller.tsx
"use client"

import * as React from "react"
import { ArrowDownIcon } from "lucide-react"
import { cn } from "cn"

/*
  Message scroller: the conversation container. Sticks to the bottom while
  new content streams in (smoothly, never jumping). If you scroll up to read,
  it never steals your place: a "New messages" pill bounces in instead, and
  pressing it glides back down.
*/
function MessageScroller({ className, children, ...props }: React.ComponentProps<"div">) {
  const ref = React.useRef<HTMLDivElement>(null)
  const pinned = React.useRef(true)
  const [unseen, setUnseen] = React.useState(false)

  React.useEffect(() => {
    const node = ref.current
    if (!node) return
    const content = node.firstElementChild as HTMLElement | null
    const onScroll = () => {
      pinned.current = node.scrollHeight - node.scrollTop - node.clientHeight < 24
      if (pinned.current) setUnseen(false)
    }
    const onGrow = () => {
      if (pinned.current) node.scrollTo({ top: node.scrollHeight, behavior: "smooth" })
      else setUnseen(true)
    }
    node.addEventListener("scroll", onScroll, { passive: true })
    const observer = new ResizeObserver(onGrow)
    if (content) observer.observe(content)
    node.scrollTop = node.scrollHeight
    return () => {
      node.removeEventListener("scroll", onScroll)
      observer.disconnect()
    }
  }, [])

  return (
    <div className="relative min-h-0 flex-1">
      <div
        ref={ref}
        role="log"
        aria-live="polite"
        data-slot="message-scroller"
        className={cn("size-full overflow-y-auto overscroll-contain", className)}
        {...props}
      >
        <div className="flex flex-col gap-stack-lg py-4">{children}</div>
      </div>
      {unseen ? (
        <button
          type="button"
          onClick={() => ref.current?.scrollTo({ top: ref.current.scrollHeight, behavior: "smooth" })}
          className="scroller-pill liquid-press absolute bottom-3 left-1/2 inline-flex -translate-x-1/2 items-center gap-1 rounded-(--shape-full) bg-(--scroller-pill) px-3 py-1.5 text-xs font-medium text-(--scroller-pill-label) shadow-2 outline-none focus-visible:shadow-(--focus-ring)"
        >
          New messages
          <ArrowDownIcon className="size-(--size-icon-sm)" />
        </button>
      ) : null}
    </div>
  )
}

export { MessageScroller }