用 label 说明正在进行的工作,例如“已校对的页数”。已知总页数时,传入相同单位的 value 和 max。例如完成 3 页、共 8 页时,读屏软件可以获取范围 0 至 8 和当前值 3,旁边的文字显示 38%。
Live example
Open playground ↗App.tsx
'use client'
import { useState } from 'react'
import { Button, Progress } from '@ancrel/ui'
import '@ancrel/tokens/css'
import '@ancrel/ui/styles.css'
export default function App() {
const [pages, setPages] = useState<number>()
return <>
<Progress label="Pages reviewed" value={pages} max={4} />
<Button disabled={pages === 4} onClick={() => setPages((pages ?? 0) + 1)}>Review one page</Button>
</>
}
Expected behavior
The label remains visible before any pages have been reviewed. Review one page advances the percentage by 25 points. After four pages the button is disabled.
Build @ancrel/tokens and @ancrel/ui from the workspace root, then run pnpm --filter @ancrel/example-progress test.
范围与未知进度
默认上限为 100。组件将有限的当前值限制在 0 到上限之间。上限为零、负数或非有限数时,按 100 计算。
省略 value、传入非有限值或设定 indeterminate,都会进入进度未知状态。此时保留标签,隐藏百分比,并移除 aria-valuenow。确定进度后更新数值即可。开启减少动态效果时,填充立即更新。
Props
| Prop | Type | Required | Default |
|---|---|---|---|
className | string | No | — |
indeterminate | boolean | No | — |
key | React.Key | null | undefined | No | — |
label | string | Yes | — |
max | number | No | — |
ref | React.Ref<HTMLProgressElement> | undefined | No | — |
value | number | No | — |
--ac-sys-motion-slowConstraints
- Use a visible label describing the work.
- Supply a positive finite max in the same units as value.
Anti-patterns
- Reporting zero while the amount completed is unknown.
- Passing a rounded percentage as value when max counts individual units.