# Progress

显示一项任务的进度，或者在进度未知时显示不确定状态。

```svelte title="basic.svelte"
<script lang="ts">
  import {
    Progress,
    ProgressIndicator,
    ProgressLabel,
    ProgressTrack,
    ProgressValue,
  } from "@ui-sdk/svelte/progress"
</script>

<Progress value={60}>
  <ProgressLabel>Exporting video</ProgressLabel>
  <ProgressValue />
  <ProgressTrack>
    <ProgressIndicator />
  </ProgressTrack>
</Progress>
```

读取导出进度条。它的标签和 60% 的值既能直接看到，也能被屏幕阅读器读到。

## 安装
```bash
npm install @ui-sdk/svelte @ui-sdk/core
```

## 组合
用下面的组合构建一个 `Progress`：

```text
Progress
├── ProgressLabel
├── ProgressValue
└── ProgressTrack
    └── ProgressIndicator
```

- `Progress` 保存值和范围。
- `ProgressLabel` 给它一个可见的名称。也可以在根上使用 `aria-label`。
- `ProgressValue` 显示格式化后的值。
- `ProgressTrack` 决定进度条的大小，`ProgressIndicator` 填充它。

值已经是根的无障碍描述的一部分。可见的值对屏幕阅读器隐藏，避免重复朗读。

## 用法
导入各个部件，把它们组合起来：

```svelte
<Progress value={40}>
  <ProgressLabel />
  <ProgressValue />
  <ProgressTrack>
    <ProgressIndicator />
  </ProgressTrack>
</Progress>
```

## 自己构建
`@ui-sdk/svelte/progress` 自带一个现成的 Progress。如果你想用自己的名字构建一个，从 `@ui-sdk/core` 里的 `progress` 原语（primitive）开始：

```ts title="upload.ts"
import { progress } from "@ui-sdk/core/progress"
import { createParts } from "@ui-sdk/svelte/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 }
```

```svelte
<Upload value={40}>
  <UploadLabel>Uploading photos</UploadLabel>
  <UploadValue>
    {#snippet children(state)}{state.text}{/snippet}
  </UploadValue>
  <UploadTrack>
    <UploadBar />
  </UploadTrack>
</Upload>
```

`ProgressValue` 在没有 children 时显示格式化后的值。`createParts` 生成的部件只渲染它的 `children` 片段，所以在里面渲染 `state.text` 来显示同样的文字。

每个部件都保留其现成对应部件的行为、状态和数据属性：根是 `progressbar`，指示器根据值设置它的宽度。更多内容见[渲染原语](/docs/svelte/rendering)和[自定义部件](/docs/svelte/custom-parts)。

## 不确定
不知道完成了多少工作时，省略 `value`。每个部件都会得到 `data-indeterminate`，根上没有 `aria-valuenow`。

在这种状态下，指示器没有内联宽度。这个示例让一段短高亮扫过轨道，并在开启减少动态效果时显示一个静止的、占满全宽的填充。

```svelte title="indeterminate.svelte"
<script lang="ts">
  import {
    Progress,
    ProgressIndicator,
    ProgressLabel,
    ProgressTrack,
  } from "@ui-sdk/svelte/progress"
</script>

<Progress value={null}>
  <ProgressLabel>Connecting to server</ProgressLabel>
  <ProgressTrack>
    <ProgressIndicator />
  </ProgressTrack>
</Progress>
```

读取 Connecting to server 进度条。它展示一项正在进行的任务，但不播报百分比。

## 改变值
任务推进时传入最新的 `value`。值低于 `max` 时，部件会得到 `data-progressing`；到达终点时得到 `data-complete`。

```svelte title="controlled.svelte"
<script lang="ts">
  import {
    Progress,
    ProgressIndicator,
    ProgressLabel,
    ProgressTrack,
    ProgressValue,
  } from "@ui-sdk/svelte/progress"
  import { useLog } from "./log"

  const total = 5

  const log = useLog()
  let uploaded = $state(1)

  function uploadNext() {
    if (uploaded >= total) {
      return
    }
    uploaded++
    log(`Uploaded ${uploaded} of ${total} photos`)
  }

  function reset() {
    uploaded = 0
    log("Reset to 0 photos")
  }
</script>

<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>
```

点击 Upload Next 添加一张照片，即 20%，并观察控制台的计数。到 100% 时，按钮不再推进进度条。点击 Reset 从 0% 开始。

Progress 显示你的状态；它没有 `onValueChange` 回调。

## 范围
`min` 和 `max` 默认是 0 和 100。超出这个范围的值在填充、百分比和 `aria-valuenow` 上会被钳制，但状态的 `value` 和百分比以外的格式会按你传入的值保留。在 1 到 5 的范围内，值 3 会填充轨道的一半。

```svelte title="min-max.svelte"
<script lang="ts">
  import {
    Progress,
    ProgressIndicator,
    ProgressLabel,
    ProgressTrack,
    ProgressValue,
  } from "@ui-sdk/svelte/progress"
</script>

<Progress value={3} min={1} max={5}>
  <ProgressLabel>Account setup, step 3 of 5</ProgressLabel>
  <ProgressValue />
  <ProgressTrack>
    <ProgressIndicator />
  </ProgressTrack>
</Progress>
```

对比 5 步中的第 3 步和填充。因为范围从 1 开始，这个值正好在范围的中点。

## 格式与 locale
把 `Intl.NumberFormat` 选项传给 `format`，以显示单位或小数位。函数可以返回自定义文字，比如剩余时间。

```svelte title="format.svelte"
<script lang="ts">
  import {
    Progress,
    ProgressIndicator,
    ProgressLabel,
    ProgressTrack,
    ProgressValue,
  } from "@ui-sdk/svelte/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
  }[]
</script>

<div data-slot="stack">
  {#each bars as { label, ...options } (label)}
    <Progress {...options}>
      <ProgressLabel>{label}</ProgressLabel>
      <ProgressValue />
      <ProgressTrack>
        <ProgressIndicator />
      </ProgressTrack>
    </Progress>
  {/each}
</div>
```

对比这几个进度读数。它们分别显示已下载的数据、精确的百分比、剩余时间和本地化的百分比。

百分比格式报告值在 `min` 和 `max` 之间的位置。其他格式直接使用值本身。

`locale` 控制数字和分隔符。省略它时，组件在挂载后读取页面语言，并以浏览器语言作为回退。最初的服务器和客户端渲染使用英文。

在周围的内容上设置 `dir="rtl"`，让轨道从右侧填充。

## 值标签
用 `getValueLabel` 给屏幕阅读器比可见数字更多的上下文。它的详情包括 `formatted`、`percent`、`min` 和 `max`。任务可能处于不确定状态时，要处理 `null` 值；那时 `percent` 也是 `null`，`formatted` 为空。

```svelte title="value-label.svelte"
<script lang="ts">
  import {
    Progress,
    ProgressIndicator,
    ProgressLabel,
    ProgressTrack,
    ProgressValue,
    type GaugeLabelDetails,
  } from "@ui-sdk/svelte/progress"

  const valueLabel = (value: number | null, details: GaugeLabelDetails) =>
    `${value} of ${details.max} files, ${details.formatted}`
</script>

<Progress value={3} max={12} getValueLabel={valueLabel}>
  <ProgressLabel>Copying files</ProgressLabel>
  <ProgressValue>3 of 12</ProgressValue>
  <ProgressTrack>
    <ProgressIndicator />
  </ProgressTrack>
</Progress>
```

用屏幕阅读器读取 Copying files。它播报 12 个文件中的 3 个和 25%，而可见的值保持紧凑。

如果显示的是已用存储这类读数，而不是会结束的任务，用 [Meter](/docs/svelte/components/meter)。

## 自定义渲染
要更改部件的元素或读取它的状态，见[部件与组合](/docs/svelte/parts-and-composition)。对于可见值和屏幕阅读器应该共用的文字，用 `format`。

## 样式
给轨道一个高度、背景和宽度。指示器以轨道的内联百分比设置自己的宽度。进度未知的任务要设置 `data-indeterminate` 的样式，因为那时指示器没有宽度；任务结束后设置 `data-complete` 的样式。两种样式选项都显示在每个示例的代码标签页里。

每个部件都有一个 `data-slot`，例如 `progress-indicator`，它的状态属性列在[数据属性](https://ui-sdk.cn/docs/svelte/components/progress#data-attributes)下。

### 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 与下面相同，作为 Svelte 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.NumberFormat` options, or a function of the value. A percent style shows where the value sits between `min` and `max`; 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 `formatted` value, its `percent`, `min` and `max`.
- `value?` (`number | null`): How far the task has come, between `min` and `max`. 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.

### 状态
渲染函数、基于状态的类名和槽位收到的内容。每个部件收到相同的状态。

- `value` (`number | null`): The value, or null while indeterminate.
- `percent` (`number | null`): Where the value sits between `min` and `max`, from 0 to 100, or null while indeterminate.
- `status` (`ProgressStatus`): Whether the progress is unknown, under way, or done.

`ProgressValue` 额外提供格式化后的文字：

- `value` (`number | null`): The value, or null while indeterminate.
- `percent` (`number | null`): Where the value sits between `min` and `max`, from 0 to 100, or null while indeterminate.
- `status` (`ProgressStatus`): Whether the progress is unknown, under way, or done.
- `text` (`string`): 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.

Runs in React、Vue 和 web components.
