Progress
显示一项任务的进度,或者在进度未知时显示不确定状态。
import "@ui-sdk/web/progress"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-progress value="60">
<ui-progress-label>Exporting video</ui-progress-label>
<ui-progress-value></ui-progress-value>
<ui-progress-track>
<ui-progress-indicator></ui-progress-indicator>
</ui-progress-track>
</ui-progress>
`
}
export { mount }安装
npm install @ui-sdk/web @ui-sdk/core导入组件会定义它的元素。在服务器上,导入什么也不做。
import "@ui-sdk/web/progress"组合
用下面的组合构建一个 ui-progress:
ui-progress
├── ui-progress-label
├── ui-progress-value
└── ui-progress-track
└── ui-progress-indicatorui-progress保存值和范围。ui-progress-label给它一个可见的名称。也可以在根上使用aria-label。ui-progress-value显示格式化后的值。ui-progress-track决定进度条的大小,ui-progress-indicator填充它。
值已经是根的无障碍描述的一部分。可见的值对屏幕阅读器隐藏,避免重复朗读。
用法
导入各个部件,把它们组合起来:
<ui-progress value="40">
<ui-progress-label></ui-progress-label>
<ui-progress-value></ui-progress-value>
<ui-progress-track>
<ui-progress-indicator></ui-progress-indicator>
</ui-progress-track>
</ui-progress>自己构建
@ui-sdk/web/progress 自带现成的 ui-progress 元素。如果你想用自己的元素名构建自己的版本,从 @ui-sdk/core 里的 progress 原语(primitive)开始:
import { progress } from "@ui-sdk/core/progress"
import { createParts } from "@ui-sdk/web"
createParts(progress, {
root: { tag: "upload-progress", display: "block" },
label: "upload-label",
value: "upload-value",
track: { tag: "upload-track", display: "block" },
indicator: { tag: "upload-bar", display: "block" },
})<upload-progress value="40">
<upload-label>Uploading photos</upload-label>
<upload-value>40%</upload-value>
<upload-track>
<upload-bar></upload-bar>
</upload-track>
</upload-progress>ui-progress-value 会为你填入格式化后的值。createParts 生成的元素只显示你放进去的内容,所以在设置值时自己写文字,就像这里的 40%。
每个元素都保留其现成对应元素的行为、状态和数据属性:根是 progressbar,指示器根据值设置它的宽度。更多内容见渲染原语和自定义部件。
不确定
不知道完成了多少工作时,省略 value。每个部件都会得到 data-indeterminate,根上没有 aria-valuenow。
在这种状态下,指示器没有内联宽度。这个示例让一段短高亮扫过轨道,并在开启减少动态效果时显示一个静止的、占满全宽的填充。
import "@ui-sdk/web/progress"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-progress>
<ui-progress-label>Connecting to server</ui-progress-label>
<ui-progress-track>
<ui-progress-indicator></ui-progress-indicator>
</ui-progress-track>
</ui-progress>
`
}
export { mount }改变值
任务推进时传入最新的 value。值低于 max 时,部件会得到 data-progressing;到达终点时得到 data-complete。
import "@ui-sdk/web/progress"
import { log } from "./log"
const total = 5
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="stack">
<div data-slot="controls">
<button type="button">Upload Next</button>
<button type="button">Reset</button>
</div>
<ui-progress>
<ui-progress-label>Uploading photos</ui-progress-label>
<ui-progress-value></ui-progress-value>
<ui-progress-track>
<ui-progress-indicator></ui-progress-indicator>
</ui-progress-track>
</ui-progress>
</div>
`
const progress = root.querySelector("ui-progress")!
const [next, reset] = root.querySelectorAll("button")
let uploaded = 1
const show = () => {
progress.setAttribute("value", String((uploaded / total) * 100))
next.setAttribute("aria-disabled", String(uploaded >= total))
}
next.addEventListener("click", () => {
if (uploaded >= total) {
return
}
uploaded++
show()
log(root, `Uploaded ${uploaded} of ${total} photos`)
})
reset.addEventListener("click", () => {
uploaded = 0
show()
log(root, "Reset to 0 photos")
})
show()
}
export { mount }Progress 显示你的状态;它不触发 valuechange 事件。
范围
min 和 max 默认是 0 和 100。超出这个范围的值在填充、百分比和 aria-valuenow 上会被钳制,但状态的 value 和百分比以外的格式会按你传入的值保留。在 1 到 5 的范围内,值 3 会填充轨道的一半。
import "@ui-sdk/web/progress"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-progress value="3" min="1" max="5">
<ui-progress-label>Account setup, step 3 of 5</ui-progress-label>
<ui-progress-value></ui-progress-value>
<ui-progress-track>
<ui-progress-indicator></ui-progress-indicator>
</ui-progress-track>
</ui-progress>
`
}
export { mount }格式与 locale
把 Intl.NumberFormat 选项传给 format,以显示单位或小数位。函数可以返回自定义文字,比如剩余时间。
import "@ui-sdk/web/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 mount(root: HTMLElement) {
const items = bars.map(
(bar) => `
<ui-progress value="${bar.value}" ${"max" in bar ? `max="${bar.max}"` : ""} ${"locale" in bar ? `locale="${bar.locale}"` : ""}>
<ui-progress-label>${bar.label}</ui-progress-label>
<ui-progress-value></ui-progress-value>
<ui-progress-track>
<ui-progress-indicator></ui-progress-indicator>
</ui-progress-track>
</ui-progress>`
)
root.innerHTML = `<div data-slot="stack">${items.join("")}</div>`
root
.querySelectorAll<HTMLElement & { format?: unknown }>("ui-progress")
.forEach((progress, index) => {
const bar = bars[index]
if ("format" in bar) {
progress.format = bar.format
}
})
}
export { mount }百分比格式报告值在 min 和 max 之间的位置。其他格式直接使用值本身。
locale 控制数字和分隔符。省略它时,组件在挂载后读取页面语言,并以浏览器语言作为回退。最初的服务器和客户端渲染使用英文。
在周围的内容上设置 dir="rtl",让轨道从右侧填充。
把 format 和 getValueLabel 设为元素 property,因为它们接受对象或函数。locale 是属性。
值标签
用 getValueLabel 给屏幕阅读器比可见数字更多的上下文。它的详情包括 formatted、percent、min 和 max。任务可能处于不确定状态时,要处理 null 值;那时 percent 也是 null,formatted 为空。
import type { GaugeLabelDetails } from "@ui-sdk/core/progress"
import "@ui-sdk/web/progress"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-progress value="3" max="12">
<ui-progress-label>Copying files</ui-progress-label>
<ui-progress-value>3 of 12</ui-progress-value>
<ui-progress-track>
<ui-progress-indicator></ui-progress-indicator>
</ui-progress-track>
</ui-progress>
`
const progress = root.querySelector("ui-progress") as HTMLElement & {
getValueLabel: (value: number | null, details: GaugeLabelDetails) => string
}
progress.getValueLabel = (value, details) =>
`${value} of ${details.max} files, ${details.formatted}`
}
export { mount }如果显示的是已用存储这类读数,而不是会结束的任务,用 Meter。
自定义渲染
见部件与组合,了解如何为部件选择绑定的元素。对于可见值和屏幕阅读器应该共用的文字,用 format。
样式
给轨道一个高度、背景和宽度。指示器以轨道的内联百分比设置自己的宽度。进度未知的任务要设置 data-indeterminate 的样式,因为那时指示器没有宽度;任务结束后设置 data-complete 的样式。两种样式选项都显示在每个示例的代码标签页里。
每个部件都有一个 data-slot,例如 progress-indicator,它的状态属性列在数据属性下。
<ui-progress>
根暴露一个 CSS 变量,用来把其他内容和读数并排定位。
| 变量 | 内容 |
|---|---|
--progress-value | Where the value sits between min and max, as a percentage, like 40%. Unset while indeterminate. |
数据属性
<ui-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. |
<ui-progress-label>
[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. |
<ui-progress-value>
[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. |
<ui-progress-track>
[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. |
<ui-progress-indicator>
[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 是属性,比如 value、min、max 和 locale。format 和 getValueLabel 是 property。
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.
ui-progress-value 额外提供格式化后的文字:
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.
无障碍
用 ui-progress-label 或 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。