Progress
显示一项任务的进度,或者在进度未知时显示不确定状态。
import {
Progress,
ProgressIndicator,
ProgressLabel,
ProgressTrack,
ProgressValue,
} from "@ui-sdk/react/progress"
function Example() {
return (
<Progress value={60}>
<ProgressLabel>Exporting video</ProgressLabel>
<ProgressValue />
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</Progress>
)
}
export { Example }安装
npm install @ui-sdk/react @ui-sdk/core组合
用下面的组合构建一个 Progress:
Progress
├── ProgressLabel
├── ProgressValue
└── ProgressTrack
└── ProgressIndicatorProgress保存值和范围。ProgressLabel给它一个可见的名称。也可以在根上使用aria-label。ProgressValue显示格式化后的值。ProgressTrack决定进度条的大小,ProgressIndicator填充它。
值已经是根的无障碍描述的一部分。可见的值对屏幕阅读器隐藏,避免重复朗读。
用法
导入各个部件,把它们组合起来:
<Progress value={40}>
<ProgressLabel />
<ProgressValue />
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</Progress>自己构建
@ui-sdk/react/progress 自带一个现成的 Progress。如果你想用自己的名字构建一个,从 @ui-sdk/core 里的 progress 原语(primitive)开始:
"use client"
import { progress } from "@ui-sdk/core/progress"
import { createParts } from "@ui-sdk/react/primitive"
const {
Root: Upload,
Label: UploadLabel,
Value: UploadValue,
Track: UploadTrack,
Indicator: UploadBar,
} = createParts(progress, {
root: "div",
label: "span",
value: "span",
track: "div",
indicator: "div",
})
export { Upload, UploadBar, UploadLabel, UploadTrack, UploadValue }<Upload value={40}>
<UploadLabel>Uploading photos</UploadLabel>
<UploadValue>{(state) => state.text}</UploadValue>
<UploadTrack>
<UploadBar />
</UploadTrack>
</Upload>ProgressValue 在没有 children 时显示格式化后的值。createParts 生成的部件只渲染它的 children,所以传入 (state) => state.text 来显示同样的文字。
每个部件都保留其现成对应部件的行为、状态和数据属性:根是 progressbar,指示器根据值设置它的宽度。更多内容见渲染原语和自定义部件。
不确定
不知道完成了多少工作时,省略 value。每个部件都会得到 data-indeterminate,根上没有 aria-valuenow。
在这种状态下,指示器没有内联宽度。这个示例让一段短高亮扫过轨道,并在开启减少动态效果时显示一个静止的、占满全宽的填充。
import {
Progress,
ProgressIndicator,
ProgressLabel,
ProgressTrack,
} from "@ui-sdk/react/progress"
function Example() {
return (
<Progress value={null}>
<ProgressLabel>Connecting to server</ProgressLabel>
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</Progress>
)
}
export { Example }改变值
任务推进时传入最新的 value。值低于 max 时,部件会得到 data-progressing;到达终点时得到 data-complete。
"use client"
import {
Progress,
ProgressIndicator,
ProgressLabel,
ProgressTrack,
ProgressValue,
} from "@ui-sdk/react/progress"
import * as React from "react"
import { useLog } from "./log"
const total = 5
function Example() {
const log = useLog()
const [uploaded, setUploaded] = React.useState(1)
function uploadNext() {
if (uploaded >= total) {
return
}
setUploaded(uploaded + 1)
log(`Uploaded ${uploaded + 1} of ${total} photos`)
}
function reset() {
setUploaded(0)
log("Reset to 0 photos")
}
return (
<div data-slot="stack">
<div data-slot="controls">
<button
type="button"
aria-disabled={uploaded >= total}
onClick={uploadNext}
>
Upload Next
</button>
<button type="button" onClick={reset}>
Reset
</button>
</div>
<Progress value={(uploaded / total) * 100}>
<ProgressLabel>Uploading photos</ProgressLabel>
<ProgressValue />
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</Progress>
</div>
)
}
export { Example }Progress 显示你的状态;它没有 onValueChange 回调。
范围
min 和 max 默认是 0 和 100。超出这个范围的值在填充、百分比和 aria-valuenow 上会被钳制,但状态的 value 和百分比以外的格式会按你传入的值保留。在 1 到 5 的范围内,值 3 会填充轨道的一半。
import {
Progress,
ProgressIndicator,
ProgressLabel,
ProgressTrack,
ProgressValue,
} from "@ui-sdk/react/progress"
function Example() {
return (
<Progress value={3} min={1} max={5}>
<ProgressLabel>Account setup, step 3 of 5</ProgressLabel>
<ProgressValue />
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</Progress>
)
}
export { Example }格式与 locale
把 Intl.NumberFormat 选项传给 format,以显示单位或小数位。函数可以返回自定义文字,比如剩余时间。
import {
Progress,
ProgressIndicator,
ProgressLabel,
ProgressTrack,
ProgressValue,
} from "@ui-sdk/react/progress"
const bars = [
{
label: "Download",
value: 120,
max: 250,
format: { style: "unit", unit: "gigabyte" },
},
{
label: "Video render",
value: 87.25,
format: { style: "percent", maximumFractionDigits: 1 },
},
{
label: "Backup",
value: 45,
max: 60,
format: (minutes: number) => `${60 - minutes} min left`,
},
{ label: "Synchronisierung", value: 40, locale: "de-DE" },
] satisfies {
label: string
value: number
max?: number
format?: Intl.NumberFormatOptions | ((value: number) => string)
locale?: string
}[]
function Example() {
return (
<div data-slot="stack">
{bars.map(({ label, ...options }) => (
<Progress key={label} {...options}>
<ProgressLabel>{label}</ProgressLabel>
<ProgressValue />
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</Progress>
))}
</div>
)
}
export { Example }百分比格式报告值在 min 和 max 之间的位置。其他格式直接使用值本身。
locale 控制数字和分隔符。省略它时,组件在挂载后读取页面语言,并以浏览器语言作为回退。最初的服务器和客户端渲染使用英文。
在周围的内容上设置 dir="rtl",让轨道从右侧填充。
值标签
用 getValueLabel 给屏幕阅读器比可见数字更多的上下文。它的详情包括 formatted、percent、min 和 max。任务可能处于不确定状态时,要处理 null 值;那时 percent 也是 null,formatted 为空。
import {
Progress,
ProgressIndicator,
ProgressLabel,
ProgressTrack,
ProgressValue,
} from "@ui-sdk/react/progress"
function Example() {
return (
<Progress
value={3}
max={12}
getValueLabel={(value, details) =>
`${value} of ${details.max} files, ${details.formatted}`
}
>
<ProgressLabel>Copying files</ProgressLabel>
<ProgressValue>3 of 12</ProgressValue>
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</Progress>
)
}
export { Example }如果显示的是已用存储这类读数,而不是会结束的任务,用 Meter。
自定义渲染
要更改部件的元素或读取它的状态,见部件与组合。对于可见值和屏幕阅读器应该共用的文字,用 format。
样式
给轨道一个高度、背景和宽度。指示器以轨道的内联百分比设置自己的宽度。进度未知的任务要设置 data-indeterminate 的样式,因为那时指示器没有宽度;任务结束后设置 data-complete 的样式。两种样式选项都显示在每个示例的代码标签页里。
每个部件都有一个 data-slot,例如 progress-indicator,它的状态属性列在数据属性下。
Progress
根暴露一个 CSS 变量,用来把其他内容和读数并排定位。
| 变量 | 内容 |
|---|---|
--progress-value | Where the value sits between min and max, as a percentage, like 40%. Unset while indeterminate. |
数据属性
Progress
[data-slot="progress"]
| 属性 | 含义 |
|---|---|
data-indeterminate | Present while the value is null: the task is under way, but how far is unknown. |
data-progressing | Present while the value is below max. |
data-complete | Present once the value reaches max. |
ProgressLabel
[data-slot="progress-label"]
| 属性 | 含义 |
|---|---|
data-indeterminate | Present while the value is null: the task is under way, but how far is unknown. |
data-progressing | Present while the value is below max. |
data-complete | Present once the value reaches max. |
ProgressValue
[data-slot="progress-value"]
| 属性 | 含义 |
|---|---|
data-indeterminate | Present while the value is null: the task is under way, but how far is unknown. |
data-progressing | Present while the value is below max. |
data-complete | Present once the value reaches max. |
ProgressTrack
[data-slot="progress-track"]
| 属性 | 含义 |
|---|---|
data-indeterminate | Present while the value is null: the task is under way, but how far is unknown. |
data-progressing | Present while the value is below max. |
data-complete | Present once the value reaches max. |
ProgressIndicator
[data-slot="progress-indicator"]
| 属性 | 含义 |
|---|---|
data-indeterminate | Present while the value is null: the task is under way, but how far is unknown. |
data-progressing | Present while the value is below max. |
data-complete | Present once the value reaches max. |
API 参考
props 与下面相同,作为 React props 传入。
Progress
min?number- The lowest value.
max?number- The highest value.
format?NumberFormat- How the value reads, for screen readers and the Value part:
Intl.NumberFormatoptions, or a function of the value. A percent style shows where the value sits betweenminandmax; any other style shows the value itself, like{ style: "unit", unit: "gigabyte" }. locale?string- The locale the value is formatted in. Defaults to the page's
lang, or the browser's language without one, once the page is interactive. Server markup uses English until then, so it never disagrees with the client. getValueLabel?((value: number | null, details: GaugeLabelDetails<number | null>) => string)- The text screen readers announce for the value, when the formatted value says too little, like "3 of 12 files". It receives the value, null while a progress bar is indeterminate, and details: the
formattedvalue, itspercent,minandmax. value?number | null- How far the task has come, between
minandmax. Null means it's under way but how far is unknown, and the bar is indeterminate. debug?boolean- Logs every state change with its reason to the console, in development builds.
状态
渲染函数、基于状态的类名和槽位收到的内容。每个部件收到相同的状态。
valuenumber | null- The value, or null while indeterminate.
percentnumber | null- Where the value sits between
minandmax, from 0 to 100, or null while indeterminate. statusProgressStatus- Whether the progress is unknown, under way, or done.
ProgressValue 额外提供格式化后的文字:
valuenumber | null- The value, or null while indeterminate.
percentnumber | null- Where the value sits between
minandmax, from 0 to 100, or null while indeterminate. statusProgressStatus- Whether the progress is unknown, under way, or done.
textstring- The formatted value, like "40%". Empty while indeterminate.
无障碍
用 ProgressLabel 或 aria-label 给组件命名。它通过 progressbar 角色暴露范围和格式化后的值。值需要更多解释时,用 getValueLabel。
经过测试的行为
下面每一行都是一条自动化测试。
- A progress bar is named by its label and reads its value.
- The value part shows the formatted value.
- The indicator is as wide as the value.
- A new value updates the bar.
- Every part says whether the task is under way, done, or unknown.
- An indeterminate bar has no value.
- A unit format reads the value itself.
- A value label replaces the formatted value for screen readers.
- A value above the maximum reads as the maximum.
运行于 React、Vue 和 web components。