A scroll container for chat transcripts that opens at the latest message and keeps scroll position stable.
Message Scroller is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
import { Bubble, BubbleContent } from "@/components/ui/bubble"import { Message, MessageContent } from "@/components/ui/message"import { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport,} from "@/components/ui/message-scroller"const messages = [ { id: "1", from: "them", text: "Hey, do you have a minute?" }, { id: "2", from: "me", text: "Sure, what's up?" }, { id: "3",
Message Scroller manages scrolling for conversation views. MessageScrollerProvider creates the scroll controller and holds options such as the initial position and auto-scroll. Inside it, MessageScroller is the root, MessageScrollerViewport is the scrollable region, and MessageScrollerContent lists one MessageScrollerItem per message. MessageScrollerButton sits inside the root and jumps to the start or end.
Use it for chat and activity feeds; use Scroll Area for general scrollable content.
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
$ pnpm dlx @esuiss/ui@latest add message-scroller
Install the following dependencies and add the cn helper:
$ pnpm add @base-ui/react @phosphor-icons/react
Add the pieces it builds on: Button, Scroll Area, use-message-scroller.
Copy and paste the following code into your project.
components/ui/message-scroller.tsx
"use client"import { mergeProps } from "@base-ui/react/merge-props"import { ScrollArea as ScrollAreaPrimitive } from "@base-ui/react/scroll-area"import { useRender } from "@base-ui/react/use-render"import { ArrowDownIcon } from "@phosphor-icons/react"import * as React from "react"import { Button } from "@/components/ui/button"import { ScrollBar } from "@/components/ui/scroll-area"import { MessageScrollerContext, type MessageScrollerOptions, type MessageScrollerScrollDirection, useChildListObserver, useFrameThrottledResizeObserver, useMergedRefs,
Update the import paths to match your project setup.
Usage
import { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport,} from "@/components/ui/message-scroller"import { Message, MessageContent } from "@/components/ui/message"import { Bubble, BubbleContent } from "@/components/ui/bubble"
Set defaultScrollPosition="start" on the provider to open at the first message. The example adds a MessageScrollerButton for each direction.
import { Bubble, BubbleContent } from "@/components/ui/bubble"import { Message, MessageContent } from "@/components/ui/message"import { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport,} from "@/components/ui/message-scroller"const messages = [ { id: "1", from: "them", text: "Hey, do you have a minute?" }, { id: "2", from: "me", text: "Sure, what's up?" }, { id: "3",
Accessibility
The viewport has role="region", an aria-label of Messages by default, and tabIndex={0} so it can be scrolled with the keyboard.
The content has role="log" with aria-relevant="additions", so assistive technology can announce new messages.
MessageScrollerButton includes a visually hidden label, Scroll to end or Scroll to start. While it cannot scroll, the button is inert and removed from the tab order.
Wheel, touch and scrolling keys stop auto-follow so the user's reading position is respected.
API Reference
MessageScrollerProvider
Creates the scroll controller and provides it to the parts and hooks inside.
Prop
Type
Default
children
React.ReactNode
-
defaultScrollPosition
"start" | "end" | "last-anchor"
"end"
autoScroll
boolean
false
scrollEdgeThreshold
number
8
scrollPreviousItemPeek
number
64
scrollMargin
number
0
Place every part inside MessageScrollerProvider; the components and hooks throw outside of it. defaultScrollPosition sets where the scroller opens: the first item, the last item, or the last item marked with scrollAnchor. When autoScroll is true, the scroller follows new content at the bottom until the user scrolls away.
scrollEdgeThreshold is the distance in pixels from an edge that still counts as being at that edge. scrollPreviousItemPeek is how many pixels of the previous item stay visible above an anchored item, and scrollMargin adds an offset in pixels from the top of the viewport when anchoring.
The exported useMessageScroller hook returns scrollToEnd, scrollToStart and scrollToMessage. useMessageScrollerScrollable reports whether each edge can scroll, and useMessageScrollerVisibility reports the visible message IDs.
MessageScroller
The root container, built on a scroll area.
Also accepts the props of React.ComponentProps<"div">.
MessageScrollerViewport
The focusable scrollable region, rendered with a vertical scrollbar.
Prop
Type
Default
preserveScrollOnPrepend
boolean
true
Also accepts the props of React.ComponentProps<"div">.
preserveScrollOnPrepend keeps the visible messages in place when items are inserted before the first one, such as when loading history. Until the initial position is applied, the root and viewport receive data-pending-scroll and the viewport is invisible. They also get data-scrollable with the scrollable edges, and data-autoscrolling while a programmatic scroll is running.
MessageScrollerContent
The live log that holds the message items.
Prop
Type
Default
spacerClassName
string
-
Also accepts the props of React.ComponentProps<"div">.
spacerClassName styles the hidden spacer element the scroller appends after the items.
MessageScrollerItem
Wraps one message and registers it with the controller.
Prop
Type
Default
messageId
string
-
scrollAnchor
boolean
false
Also accepts the props of React.ComponentProps<"div">.
Give each item a stable messageId; it is used for scrollToMessage, visibility tracking and the data-message-id attribute. When an item with scrollAnchor is added, the scroller aligns it to the top of the viewport instead of the bottom, and the item sets data-scroll-anchor. Items use content-visibility: auto to keep long transcripts fast.
MessageScrollerButton
A floating button that appears when the scroller can move toward its direction and jumps there on click.
Also accepts the props of useRender.ComponentProps<"button">.
Prop
Type
Default
asChild
boolean
false
Also accepts the props of React.ComponentProps<"button">.
Prop
Type
Default
render
(props) => React.ReactNode
-
Also accepts the props of React.ComponentProps<"button">.
direction sets the edge the button scrolls to, as well as its position and icon rotation. behavior is the scroll behavior used when jumping, such as smooth or auto. variant and size are passed to Button. The button exposes data-active, data-direction, data-variant and data-size.
Each part sets a data-slot, such as message-scroller-viewport or message-scroller-item, for styling.
Web
Web preview
import { ArrowUpIcon, ChecksIcon, XIcon } from "phosphor-react-native"import * as React from "react"import { View } from "react-native"import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentLabel, AttachmentMedia, AttachmentProgress, AttachmentTitle,} from "@/components/ui/attachment"import { Avatar, AvatarFallback } from "@/components/ui/avatar"import { BottomBar, BottomBarContent } from "@/components/ui/bottom-bar"
import { ArrowUpIcon, ChecksIcon, XIcon } from "phosphor-react-native"import * as React from "react"import { View } from "react-native"import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentLabel, AttachmentMedia, AttachmentProgress, AttachmentTitle,} from "@/components/ui/attachment"import { Avatar, AvatarFallback } from "@/components/ui/avatar"import { BottomBar, BottomBarContent } from "@/components/ui/bottom-bar"
import PhosphorSwiftimport SwiftUIstruct MessageScrollerExample: View { var body: some View { MessageScrollerChat(initial: MessageScrollerData.thread) }}struct MessageScrollerAttachment: Hashable { let name: String let size: String var progress: Double?}struct MessageScrollerMessage: Identifiable, Hashable {
MessageScroller is a View that hosts a scroll controller. MessageScrollerViewport renders your messages in an inverted FlatList, so the newest message sits at the bottom and new messages keep the list anchored there through maintainVisibleContentPosition. You pass data in chronological order; the viewport reverses it internally and maps indexes back for renderItem and keyExtractor.
MessageScrollerButton is a floating icon button that animates in with Reanimated when there is more content in its direction. When the user has scrolled up and new messages arrive, it shows an unread count badge.
Copy and paste the following code into components/ui/message-scroller.tsx:
uniwind/components/ui/message-scroller.tsx
import { cn } from "cn"import { ArrowDownIcon, ArrowUpIcon } from "phosphor-react-native"import * as React from "react"import { FlatList, type FlatListProps, Platform, View, type ViewProps,} from "react-native"import { Easing, ReduceMotion, useAnimatedStyle, useSharedValue, withTiming,
Update the import paths to match your project setup.
Copy and paste the following code into components/ui/message-scroller.tsx:
nativewind/components/ui/message-scroller.tsx
import { cn } from "cn"import { ArrowDownIcon, ArrowUpIcon } from "phosphor-react-native"import * as React from "react"import { FlatList, type FlatListProps, Platform, View, type ViewProps,} from "react-native"import { Easing, ReduceMotion, useAnimatedStyle, useSharedValue, withTiming,
Update the import paths to match your project setup.
Copy and paste the following code into components/ui/message-scroller.tsx:
stylesheet/components/ui/message-scroller.tsx
import { ArrowDownIcon, ArrowUpIcon } from "phosphor-react-native"import * as React from "react"import { FlatList, type FlatListProps, Platform, StyleSheet, View, type ViewProps,} from "react-native"import { Easing, ReduceMotion, useAnimatedStyle, useSharedValue, withTiming,
Update the import paths to match your project setup.
import CoreTextimport SwiftUIimport UIKitstruct RGBA: Sendable { let red: CGFloat let green: CGFloat let blue: CGFloat let alpha: CGFloat init(_ hex: UInt32, _ alpha: CGFloat = 1) { red = CGFloat((hex >> 16) & 0xFF) / 255 green = CGFloat((hex >> 8) & 0xFF) / 255 blue = CGFloat(hex & 0xFF) / 255 self.alpha = alpha }
swift/Theme/Tokens.swift
import SwiftUIpublic enum EsuissColor { public static let background = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x000000)) public static let foreground = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7)) public static let card = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x1D1D1F)) public static let cardForeground = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7)) public static let popover = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0x1D1D1F)) public static let popoverForeground = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7)) public static let primary = Color.adaptive(light: .init(0x0071E3), dark: .init(0x0A84FF)) public static let primaryForeground = Color.adaptive(light: .init(0xFFFFFF), dark: .init(0xFFFFFF)) public static let secondary = Color.adaptive(light: .init(0x000000, 0.04), dark: .init(0xFFFFFF, 0.1)) public static let secondaryForeground = Color.adaptive(light: .init(0x1D1D1F), dark: .init(0xF5F5F7)) public static let muted = Color.adaptive(light: .init(0xF5F5F7), dark: .init(0x161617)) public static let mutedForeground = Color.adaptive(light: .init(0x6E6E73), dark: .init(0x86868B)) public static let accent = Color.adaptive(light: .init(0x000000, 0.06), dark: .init(0xFFFFFF, 0.1))
Copy the source:
swift/Components/MessageScroller.swift
import PhosphorSwiftimport SwiftUIpublic enum MessageScrollerDefaultScrollPosition: CaseIterable, Sendable { case start, end}public enum MessageScrollerScrollDirection: CaseIterable, Sendable, Hashable { case start, end}struct MessageScrollerChange: Equatable { var appended = 0 var prepended = 0 var reset = false}
Add the files to your app target.
Apply the Compose compiler plugin, enable Compose, set compileSdk to 37 or higher and minSdk to 24 or higher, and add the dependencies:
package com.esuiss.ui.themeimport androidx.compose.runtime.Immutableimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.text.font.FontWeightimport androidx.compose.ui.unit.dp@Immutabledata class Colors( val background: Color, val foreground: Color, val card: Color, val cardForeground: Color, val popover: Color, val popoverForeground: Color, val primary: Color,
Set onLoadOlder to load earlier messages when the user scrolls near the top.
React Native
import { ChecksIcon, XIcon } from "phosphor-react-native"import * as React from "react"import { View } from "react-native"import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentLabel, AttachmentMedia, AttachmentProgress, AttachmentTitle,} from "@/components/ui/attachment"import { Avatar, AvatarFallback } from "@/components/ui/avatar"import { Bubble, BubbleContent, BubbleReactions } from "@/components/ui/bubble"
import { ChecksIcon, XIcon } from "phosphor-react-native"import * as React from "react"import { View } from "react-native"import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentLabel, AttachmentMedia, AttachmentProgress, AttachmentTitle,} from "@/components/ui/attachment"import { Avatar, AvatarFallback } from "@/components/ui/avatar"import { Bubble, BubbleContent, BubbleReactions } from "@/components/ui/bubble"
import PhosphorSwiftimport SwiftUIstruct MessageScrollerAttachment: Hashable { let name: String let size: String var progress: Double?}struct MessageScrollerMessage: Identifiable, Hashable { let id: String let fromMe: Bool let texts: [String] var time: String? var status: String? var reaction: String?
The button has a default label, "Scroll to end" or "Scroll to start", and includes the number of new messages when there are any.
While the button is hidden it ignores touches and is hidden from VoiceOver and TalkBack.
The button animation respects the system reduce motion setting.
API Reference
MessageScrollerProvider
Shares one scroll controller with components outside the scroller.
Prop
Type
Default
children
React.ReactNode
-
scrollEdgeThreshold
number
8
Also accepts the props of MessageScrollerOptions.
Wrap everything in MessageScrollerProvider when components outside the scroller, such as a composer, need to call the scroll hooks. scrollEdgeThreshold is the distance in points from an edge that still counts as being at that edge.
useMessageScroller returns scrollToEnd, scrollToStart and scrollToMessage(id, { align, animated }). useMessageScrollerScrollable, useMessageScrollerUnreadCount and useMessageScrollerVisibility read whether each edge is scrollable, the unread count, and the visible message ids.
MessageScroller
Container that clips the list and positions the button.
Also accepts the props of ViewProps.
Place MessageScrollerViewport and MessageScrollerButton inside it.
Also accepts the props of MessageScrollerViewportProps<T>.
Pass data in chronological order; the viewport reverses it internally and maps indexes back for renderItem and keyExtractor, which should return a stable key for each message. onLoadOlder is called near the top so older messages can be loaded. Other FlatList props pass through. The keyboard dismisses interactively on iOS and on drag on Android, and taps on handled children keep it open.
MessageScrollerItem
Row wrapper with horizontal padding.
Also accepts the props of ViewProps.
MessageScrollerButton
Floating button that scrolls to the newest or oldest message.
Prop
Type
Default
children
React.ReactNode
-
direction
MessageScrollerScrollDirection
"end"
animated
boolean
true
Also accepts the props of React.ComponentProps<typeof Button>.
direction="end" scrolls to the newest message and start to the oldest. animated animates the scroll. The button uses bg-card with border-border, and the unread badge uses bg-primary with text-primary-foreground.
MessageScrollerDefaultScrollPosition
Values: .start, .end.
MessageScrollerScrollDirection
Values: .start, .end.
MessageScrollerController
Takes no parameters.
Property
Type
unreadCount
Int
isAtEnd
Bool
isAtStart
Bool
MessageScrollerController.canScroll
Parameter
Type
Default
_ direction
MessageScrollerScrollDirection: .start | .end
-
Returns Bool.
MessageScrollerController.scrollToEnd
Parameter
Type
Default
animated
Bool
true
MessageScrollerController.scrollToStart
Parameter
Type
Default
animated
Bool
true
MessageScrollerController.scrollToMessage
Parameter
Type
Default
_ id
ID
-
anchor
UnitPoint
.top
animated
Bool
true
MessageScroller
Container that clips the list and positions the button.