Progress 表示任务进度。额度、评分分布等只读占比用 RatioBar。
状态:experimental · 组件:Progress、RatioBar
引入
定位到「引入」import { Progress, RatioBar } from 'nacre'用法
定位到「用法」value / max 给进度(max 默认 100),label 写在做什么;横向撑满父容器,上面一行文字写「第 3 / 8 项」这类具体说明。
import { Text, View, signal } from 'nsh'import { Button, Progress, TEXT_SIZE } from 'nacre'
export function ProgressUsage() { const done = signal(3) const total = 8 return ( <View class="w-[280px] max-w-full gap-2"> <Text testID="progress-usage-text" class={`${TEXT_SIZE.small} text-foreground tabular-nums`}> {'正在检测第 {n} / {total} 项' .replace('{n}', String(Math.min(done() + 1, total))) .replace('{total}', String(total))} </Text> <Progress testID="progress-usage" value={done()} max={total} label="环境自检" /> <View class="flex-row gap-2"> <Button testID="progress-usage-next" size="xs" onPress={() => done.update((n) => Math.min(n + 1, total))}> 下一项 </Button> <Button size="xs" variant="ghost" onPress={() => done.set(0)}> 重新开始 </Button> </View> </View> )}示例
定位到「示例」只读比例
定位到「只读比例」参照 HTML meter(https://developer.mozilla.org/docs/Web/HTML/Element/meter):表示一个范围内的量,不表示任务进度。原生接口没有 meter 角色,读屏提供名称和百分比;不接受键盘调节。阈值、语气和旁边的文案由调用方决定。
import { Text, View } from 'nsh'import { RatioBar, TEXT_SIZE } from 'nacre'
export function RatioReading() { const label = '剩余额度' return ( <View class="w-[200px] gap-2"> <Text class={`${TEXT_SIZE.small} text-foreground`}>{`${label} 25%`}</Text> <RatioBar testID="ratio-reading" label={label} size="sm" tone="warning" ratio={0.25} /> </View> )}尺寸
定位到「尺寸」sm 2pt(默认)放在对话框、卡片里一行文字下面;md 4pt 给单独一块的上传、导出。
手机:手机上 sm 4pt(默认)、md 6pt(iOS 进度条),轨道深一档,户外光线下也看得出。
import { View } from 'nsh'import { Progress } from 'nacre'
export function ProgressSizes() { return ( <View class="w-[280px] max-w-full gap-4"> <Progress value={40} label="导入" /> <Progress testID="progress-md" size="md" value={72} label="上传附件" /> </View> )}不定进度
定位到「不定进度」indeterminate:不知道总量(等服务端、排队)时整条缓慢呼吸,不画具体进度;减少动态效果时静止。知道总量就别用。
import { Text, View } from 'nsh'import { Progress, TEXT_SIZE } from 'nacre'
export function ProgressIndeterminate() { return ( <View class="w-[280px] max-w-full gap-2"> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>正在排队,等待服务端处理…</Text> <Progress testID="progress-indeterminate" indeterminate value={0} label="导出会话记录" /> </View> )}无障碍
定位到「无障碍」- role=“progressbar”,读 label 和百分比;不定进度读「忙碌」,不读数值。
- 进度变化有 150ms 过渡;减少动态效果时直接落位,不定进度不呼吸。
该用 / 不该用
定位到「该用 / 不该用」该用
- 几秒以上、能数清步数的任务(自检、批量导出)
- 旁边用文字写清做到第几项
- label 写在做什么(「环境自检」)
不该用
- 一两秒就完成的请求(用按钮的 loading)
- 首屏加载占位(用 Skeleton)
- 知道总量时用 indeterminate
- 自己写 h-0.5 bg-secondary 的进度条
API
定位到「API」Progress
定位到「Progress」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
value * |
number |
— | 已完成的量,0..max;超出两端按两端算 |
max |
number |
— | 总量,默认 100 |
label * |
string |
— | 读屏名:在做什么(「环境自检」),读屏同时念进度值 |
indeterminate |
boolean |
— | 不知道总量(等服务端、排队中):整条缓慢呼吸,不画具体进度;减少动态效果时静止 |
size |
ProgressSize |
— | sm 2pt(默认,放在对话框、卡片里一行文字下面);md 4pt(单独一块的上传、导出) |
class |
string |
— | 布局类(宽度、外边距);默认横向撑满父容器 |
testID |
string |
— |
* 必填
RatioBar
定位到「RatioBar」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
ratio * |
number |
— | 部分占整体的比例,0–1;超界时裁到轨道内 |
tone |
RatioTone |
'neutral' |
|
label * |
string |
— | 比例的名称;数值单独提供给读屏 |
size |
'sm' | 'md' |
— | sm 6pt,md 8pt |
class |
string |
— | |
testID |
string |
— |
* 必填