Skip to content
Nacre
Color palette
中文

ScrollArea

stableScrollAreaOpen in playground
ScrollArea
Runs the real component in your browser.

A scroll container with the library’s overlay scrollbar; with fade, each edge that can still scroll fades out over 24pt, and no edge fades at the end or when content doesn’t overflow. When using nsh’s Scroll directly, pass scrollbarTheme() to its scrollbar for the same scrollbar.

Status: stable · Components: ScrollArea

import { ScrollArea } from 'nacre'

A fixed-height list: the parent sets the height and ScrollArea fills it with class=“flex-1”; fade hints “there’s more below”, and the bottom edge stops fading at the end.

import { Text, View } from 'nsh'
import { ScrollArea, TEXT_SIZE, TOUCH } from 'nacre'
const topics = () => [
'Account opening status',
'Withdrawal not received',
'Change linked phone number',
'HK IPO subscription rules',
'US pre-market trading',
'Account freeze appeal',
'Commission plan inquiry',
'Fund transfer failed',
'Unusual login device',
'Document reminder',
'Options trading permission',
'Statement download',
'Currency conversion',
'Dividend payment date',
'Tax form',
'Close account',
]
export function ScrollAreaUsage() {
return (
<View class={`${TOUCH ? 'w-full' : 'w-[260px] max-w-full rounded-lg border border-border'} h-[180px]`}>
<ScrollArea testID="demo-scrollarea-v" fade focusable accessibilityLabel="Inquiry topics" class="flex-1">
<View class="p-3 gap-2">
{topics().map((topic) => <Text class={`${TEXT_SIZE.small} text-foreground`}>{topic}</Text>)}
</View>
</ScrollArea>
</View>
)
}

Without fade there’s only the overlay scrollbar: long text in panels, full transcripts.

import { Text, View } from 'nsh'
import { ScrollArea, TEXT_SIZE, TOUCH } from 'nacre'
const transcript = () => [
'Customer: Hi, the withdrawal I requested last Friday still hasn’t arrived.',
'Agent: Hello, let me check. Your withdrawal was approved on Monday, and banks usually take 1–3 business days to process it.',
'Customer: The receiving bank is HSBC. Will it take longer?',
'Agent: HSBC usually credits within 2 business days; if it hasn’t arrived by Thursday, send me a screenshot of your bank statement and I’ll file an urgent ticket for you.',
'Customer: OK, thanks.',
]
export function ScrollAreaPlain() {
return (
<View class={`${TOUCH ? 'w-full' : 'w-[320px] max-w-full rounded-lg border border-border'} h-[160px]`}>
<ScrollArea class="flex-1">
<View class="p-3 gap-2">
{transcript().map((line) => <Text class={`${TEXT_SIZE.small} text-foreground`}>{line}</Text>)}
</View>
</ScrollArea>
</View>
)
}

horizontal + contentClass lays out the content (flex-row by default when horizontal); for content that doesn’t fit on one line, like quick replies and attachment strips.

import { View } from 'nsh'
import { ScrollArea, Tag, TOUCH } from 'nacre'
const replies = () => [
'Hello, how can I help you?',
'One moment, let me look that up',
'I’ve filed a ticket for you',
'Please provide the phone number on your account',
'Thank you for your patience',
'Anything else I can help with?',
]
export function ScrollAreaHorizontal() {
return (
<View class={`${TOUCH ? 'w-full' : 'w-[320px] max-w-full rounded-lg border border-border'}`}>
<ScrollArea horizontal fade contentClass="flex-row gap-2 p-3">
{replies().map((text) => <Tag variant="soft" label={text} />)}
</ScrollArea>
</View>
)
}

Read the offset and content size with onScroll, set its interval with scrollEventThrottle, and use scrollRef to jump to the top or restore an offset.

import { Text, View, signal } from 'nsh'
import type { ScrollRef } from 'nsh'
import { Button, ScrollArea, TEXT_SIZE } from 'nacre'
export function ScrollAreaOnScroll() {
let scroll: ScrollRef | undefined
const offset = signal(0)
return (
<View class="w-[260px] max-w-full gap-2">
<View class="h-[160px] rounded-lg border border-border">
<ScrollArea testID="demo-offset-list" scrollRef={(node) => { scroll = node }}
scrollEventThrottle={50} fade class="flex-1" onScroll={(e) => offset.set(Math.round(e.offset.y))}>
<View class="p-3 gap-2">
{Array.from({ length: 20 }, (_, i) => <Text class={`${TEXT_SIZE.small} text-foreground`}>{`$Chat history ${i + 1}`}</Text>)}
</View>
</ScrollArea>
</View>
<Button testID="demo-scroll-top" size="sm" onPress={() => scroll?.scrollTo({ y: 0, animated: false })}>
Back to top
</Button>
<Text testID="demo-scroll-offset" class={`${TEXT_SIZE.small} text-muted-foreground tabular-nums`}>{offset() > 0 ? `$Scrolled down ${offset()}pt` : 'At the top'}</Text>
</View>
)
}
  • The scroll area itself isn’t a screen reader control; it only wraps content, and rows and buttons inside stay focusable as usual.
  • fade is only a visual mask; screen readers still read the faded content.

Do

  • Use fade for fixed-height lists and popover and dialog bodies
  • Give the parent a definite height (h-* or flex-1)
  • Pass scrollbar={scrollbarTheme()} when using Scroll yourself

Don’t

  • Fade a whole-page scroll
  • Nest a ScrollArea in another ScrollArea scrolling the same way
  • Use the system default scrollbar (it doesn’t match the library’s)

Property descriptions follow the Chinese source comments.

Prop Type Default Description
fade boolean — 还能往哪一侧滚,那一侧的边缘淡出 24pt(coss ScrollArea 的 scrollFade):用在弹层、对话框、定高列表里,提示「下面还有」;整页滚动不用
horizontal boolean —
scrollbar boolean — false 隐藏覆盖式滚动条,仍可滚动。
scrollRef (node: ScrollRef) => void — 读取原生滚动句柄,供定位、恢复位置或回到最新。
scrollEventThrottle number — 两次滚动事件之间的最短毫秒数。
class string —
classList ScrollProps['classList'] —
style StyleProp<ViewStyle> —
contentClass string — 内容容器的 class(横向滚动时默认 flex-row)
focusable boolean — 滚动区本身进 Tab 序列(内描焦点环):里面只有文字、没有可聚焦的控件时打开,键盘才能进来滚动(WCAG 2.1.1); 里面有按钮、输入框时不用开,焦点进到控件上自然会滚到可见。开了要给 accessibilityLabel
accessibilityLabel string — 读屏名(focusable 时必填):「客户备注」
testID string —
onScroll (e: ScrollEvent) => void —
children JSX.Element —

* required