跳转到内容
Nacre
组件配色
EN

滚动区 ScrollArea

stableScrollArea在画廊运行
ScrollArea
在浏览器里运行真实组件。

带库的覆盖式滚动条的滚动容器;fade 时还能往哪一侧滚,那一侧边缘淡出 24pt,滚到头或内容不溢出时不淡出。直接用 nsh 的 Scroll 时,把 scrollbarTheme() 传给它的 scrollbar 得到同一套滚动条。

状态:stable · 组件:ScrollArea

import { ScrollArea } from 'nacre'

定高列表:父容器给高,ScrollArea 用 class=“flex-1” 撑满;fade 提示「下面还有」,滚到底时下边缘不再淡出。

import { Text, View } from 'nsh'
import { ScrollArea, TEXT_SIZE, TOUCH } from 'nacre'
const topics = () => [
'开户进度查询',
'出金未到账',
'修改绑定手机号',
'港股打新规则',
'美股盘前交易',
'账户冻结申诉',
'佣金套餐咨询',
'资金划转失败',
'登录设备异常',
'资料补充提醒',
'期权权限开通',
'结单下载',
'汇率换算',
'分红到账时间',
'税表填写',
'注销账户',
]
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="咨询主题" class="flex-1">
<View class="p-3 gap-2">
{topics().map((topic) => <Text class={`${TEXT_SIZE.small} text-foreground`}>{topic}</Text>)}
</View>
</ScrollArea>
</View>
)
}

不给 fade 就只有覆盖式滚动条:面板里的长文本、转写全文。

import { Text, View } from 'nsh'
import { ScrollArea, TEXT_SIZE, TOUCH } from 'nacre'
const transcript = () => [
'客户:你好,我上周五申请的出金到现在还没到账。',
'坐席:您好,我帮您查一下。出金申请已于周一审核通过,银行处理一般需要 1–3 个工作日。',
'客户:收款银行是汇丰,会不会更慢?',
'坐席:汇丰通常在 2 个工作日内入账;如果周四还没收到,请把银行流水截图发给我,我为您提交加急工单。',
'客户:好的,谢谢。',
]
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 排内容(横向默认 flex-row);快捷回复、附件条这类一行放不下的内容。

import { View } from 'nsh'
import { ScrollArea, Tag, TOUCH } from 'nacre'
const replies = () => [
'您好,请问有什么可以帮您?',
'请稍等,我帮您查询一下',
'已为您提交工单',
'请提供开户手机号',
'感谢您的耐心等待',
'还有其他问题吗?',
]
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>
)
}

onScroll 回传偏移与内容尺寸,scrollEventThrottle 控制频率,scrollRef 提供原生滚动句柄,可回顶或恢复位置。

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`}>{`$会话记录 ${i + 1}`}</Text>)}
</View>
</ScrollArea>
</View>
<Button testID="demo-scroll-top" size="sm" onPress={() => scroll?.scrollTo({ y: 0, animated: false })}>
回到顶部
</Button>
<Text testID="demo-scroll-offset" class={`${TEXT_SIZE.small} text-muted-foreground tabular-nums`}>{offset() > 0 ? `$已向下滚动 ${offset()}pt` : '在顶部'}</Text>
</View>
)
}
  • 滚动区本身不是读屏控件,只包内容;里面的行、按钮照常可聚焦。
  • fade 只是视觉遮罩,不影响读屏读到被淡出的内容。

该用

  • 定高列表、弹层与对话框正文用 fade
  • 父容器给确定高度(h-* 或 flex-1)
  • 自己用 Scroll 时传 scrollbar={scrollbarTheme()}

不该用

  • 整页滚动加渐隐
  • 在 ScrollArea 里再套同方向的 ScrollArea
  • 用系统默认滚动条(和库的不一致)
属性 类型 默认 说明
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 —

* 必填