---
title: "Progress Bar"
description: "Displays the completion progress of a task."
url: https://weme-ui.github.io/slim/components/progress-bar.md
---
# Progress Bar

进度条。表示一项任务走到哪了。基于 Reka UI `Progress`。可以带标题、当前值和一句说明。

## Installation

```bash
pnpm dlx @weme-ui/weme-ui add progress-bar
```

## Usage

```vue
<script lang="ts" setup>
import ProgressBar from '../progress-bar.vue'
</script>

<template>
  <div class="w-full">
    <ProgressBar label="Uploading" :model-value="64" />
  </div>
</template>
```

## Props

| 属性            | 说明                                                                                                                                                    | 类型                                                                       | 默认值                                                                                          |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `as`            | 根节点渲染为的元素或组件（Reka UI `Primitive`）                                                                                                         | `AsTag \| Component`                                                       | `'div'`                                                                                         |
| `asChild`       | 合并到子元素上渲染（Reka UI composition）                                                                                                               | `boolean`                                                                  | `false`                                                                                         |
| `modelValue`    | 当前进度，可用 `v-model`。空值时 `data-state` 为 `indeterminate`。指示条按 `translateX(-(100 - (modelValue ?? 0))%)` 位移，不除以 `max`                 | `number \| null`                                                           | —                                                                                               |
| `max`           | 最大值。用于 `aria-valuemax`，并作为百分比文案的分母                                                                                                    | `number`                                                                   | `100`                                                                                           |
| `getValueLabel` | 无障碍名称（`aria-label`）。值为数字时默认读成相对 `max` 的整数百分比，否则不设                                                                         | `(value: number \| null \| undefined, max: number) => string \| undefined` | `(value, max) => typeof value === 'number' ? Math.round((value / max) * 100) + '%' : undefined` |
| `getValueText`  | 无障碍数值文本（`aria-valuetext`）。未传时不设                                                                                                          | `(value: number \| null \| undefined, max: number) => string \| undefined` | —                                                                                               |
| `color`         | 指示条颜色                                                                                                                                              | `'default' \| 'accent' \| 'info' \| 'success' \| 'warning' \| 'error'`     | `'default'`                                                                                     |
| `size`          | 尺寸                                                                                                                                                    | `'sm' \| 'md' \| 'lg'`                                                     | `'md'`                                                                                          |
| `label`         | 标题。无 `label` 插槽时作为默认内容。无插槽且无 prop 时不渲染标题行，数值也不显示                                                                       | `string`                                                                   | —                                                                                               |
| `description`   | 说明。无 `description` 插槽时作为默认内容                                                                                                               | `string`                                                                   | —                                                                                               |
| `hideValue`     | 为 `true` 时不渲染数值                                                                                                                                  | `boolean`                                                                  | —                                                                                               |
| `formatOptions` | 数值文案的 `Intl.NumberFormat` 选项。`style` 为 `'percent'` 时格式化 `(modelValue ?? 0) / (max ?? 100)`，否则格式化 `modelValue ?? 0`。只在有标题时出现 | `Intl.NumberFormatOptions`                                                 | `{ style: 'percent' }`                                                                          |
| `class`         | 轨道额外 class（加在 track 上）                                                                                                                         | `any`                                                                      | —                                                                                               |
| `ui`            | 覆盖 slots 样式（`root` / `track` / `indicator` / `caption` / `label` / `description` / `value`）                                                       | `Partial<ProgressBarStyleSlots>`                                           | —                                                                                               |

## Events

| 事件                | 说明                                                              | 参数                           |
| ------------------- | ----------------------------------------------------------------- | ------------------------------ |
| `update:modelValue` | 值变更时触发（含 `v-model`）；由 Reka `ProgressRoot` 转发         | `value: string[] \| undefined` |
| `update:max`        | `max` 变更时触发（含 `v-model:max`）；由 Reka `ProgressRoot` 转发 | `value: number`                |

## Slots

| 插槽          | 说明                                                                                                                                     |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `label`       | 标题内容。未传时回退到 `label`。无插槽且无 prop 时不渲染标题行                                                                           |
| `value`       | 数值文案。作用域参数 `{ modelValue, value }`。`modelValue` 是当前进度，`value` 是格式化后的字符串。仅在有标题且 `hideValue` 不为真时渲染 |
| `description` | 说明内容。未传时回退到 `description`。无插槽且无 prop 时不渲染                                                                           |

## Accessibility

基于 Reka UI `ProgressRoot`（内含 `ProgressIndicator`）。根节点 `role="progressbar"`，`aria-valuemin` 为 `0`，`aria-valuemax` 为 `max`。值为数字时设置 `aria-valuenow`，`aria-label` 来自 `getValueLabel`，`aria-valuetext` 来自 `getValueText`。`data-state` 为空值时是 `indeterminate`，等于 `max` 时是 `complete`，否则是 `loading`。根节点还有 `data-value`、`data-max`，以及 `data-slot="progress-bar"`。

## Examples

### Sizes

```vue
<script lang="ts" setup>
import ProgressBar from '../progress-bar.vue'
</script>

<template>
  <div class="flex flex-col gap-3 w-full">
    <ProgressBar label="Small" size="sm" :model-value="40" />
    <ProgressBar label="Medium" size="md" :model-value="40" />
    <ProgressBar label="Large" size="lg" :model-value="40" />
  </div>
</template>
```

### Color

```vue
<script lang="ts" setup>
import ProgressBar from '../progress-bar.vue'

const colors = ['default', 'accent', 'info', 'success', 'warning', 'error'] as const
</script>

<template>
  <div class="flex flex-col gap-3 w-full">
    <ProgressBar
      v-for="color in colors"
      :key="color"
      :color="color"
      :label="color"
      :model-value="64"
    />
  </div>
</template>
```

### With Description

```vue
<script lang="ts" setup>
import ProgressBar from '../progress-bar.vue'
</script>

<template>
  <div class="w-full">
    <ProgressBar description="64 GB of 100 GB used" label="Storage" :model-value="64" />
  </div>
</template>
```

### Without Label

```vue
<script lang="ts" setup>
import ProgressBar from '../progress-bar.vue'
</script>

<template>
  <div class="w-full">
    <ProgressBar :model-value="64" />
  </div>
</template>
```

### Format Options

```vue
<script lang="ts" setup>
import ProgressBar from '../progress-bar.vue'

const formats = [
  {
    label: 'Percent',
    formatOptions: { style: 'percent' },
  },
  {
    label: 'Decimal',
    formatOptions: { style: 'decimal' },
  },
  {
    label: 'USD',
    formatOptions: { style: 'currency', currency: 'USD' },
  },
] as const
</script>

<template>
  <div class="flex flex-col gap-3 w-full">
    <ProgressBar
      v-for="format in formats"
      :key="format.label"
      :format-options="format.formatOptions"
      :label="format.label"
      :model-value="64"
    />
  </div>
</template>
```

### Indeterminate

```vue
<script lang="ts" setup>
import ProgressBar from '../progress-bar.vue'
</script>

<template>
  <div class="w-full">
    <ProgressBar aria-label="Loading" :model-value="null" />
  </div>
</template>
```