Files
heroui-vue/apps/docs/components/progress-bar.md
T
2026-10-22 14:33:15 +08:00

207 lines
7.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: ProgressBar 进度条
description: 进度条用于展示某项操作随时间变化的确定或不确定进度。
component: progress-bar
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
Label,
ProgressBar,
ProgressBarFill,
ProgressBarOutput,
ProgressBarTrack,
} from "@heroui-vue/components";
</script>
<template>
<ProgressBar aria-label="加载中" class="w-64" :value="60">
<Label>加载中</Label>
<ProgressBarOutput />
<ProgressBarTrack>
<ProgressBarFill />
</ProgressBarTrack>
</ProgressBar>
</template>
```
<ComponentPreview name="progress-bar-basic" />
## 组件结构
```vue
<script setup lang="ts">
import {
Label,
ProgressBar,
ProgressBarFill,
ProgressBarOutput,
ProgressBarTrack,
} from "@heroui-vue/components";
</script>
<template>
<ProgressBar :value="60">
<Label>Loading</Label>
<ProgressBarOutput />
<ProgressBarTrack>
<ProgressBarFill />
</ProgressBarTrack>
</ProgressBar>
</template>
```
`ProgressBar` 渲染为 `role="progressbar"` 的容器。默认插槽为作用域插槽,可读取 `percentage`、`valueText`、`isIndeterminate`。`ProgressBarOutput` 在未提供内容时展示格式化后的数值文本。
## 示例
### 尺寸
<ComponentPreview name="progress-bar-sizes" />
### 颜色
<ComponentPreview name="progress-bar-colors" />
### 无可见标签
无需可见标签时,使用 `aria-label` 提供无障碍标签。
<ComponentPreview name="progress-bar-without-label" />
### 不确定进度
无法确定进度时使用 `isIndeterminate`。此时不输出 `aria-valuenow`,填充条进入不确定动画。
<ComponentPreview name="progress-bar-indeterminate" />
### 自定义数值范围
使用 `minValue`、`maxValue` 与 `formatOptions` 自定义取值范围与显示格式。
<ComponentPreview name="progress-bar-custom-value" />
> 基线差异:上游示例使用 `NumberField` 与 `Select` 调节数值与格式;这两个组件尚未迁移。本示例以原生 `input[type=number]` / `select` 搭配 `hv-input` 样式临时替代,交互与布局对齐基线,控件本身不是本库已发布的 NumberField/Select。
## 自定义样式
### Tailwind CSS
<ComponentPreview name="progress-bar-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-progress-bar {
@apply w-full gap-2;
}
.hv-progress-bar__track {
@apply h-3 rounded-full;
}
.hv-progress-bar__fill {
@apply rounded-full;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
ProgressBar 使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/progress-bar.css)):
#### 基础与元素类 [!toc]
- `.hv-progress-bar` - 基础容器(网格布局)
- `.hv-progress-bar__output` - 数值文本展示
- `.hv-progress-bar__track` - 轨道背景
- `.hv-progress-bar__fill` - 轨道上已填充部分
#### 尺寸类 [!toc]
- `.hv-progress-bar--sm` - 小尺寸变体(更细的轨道)
- `.hv-progress-bar--md` - 中等尺寸变体(默认)
- `.hv-progress-bar--lg` - 大尺寸变体(更粗的轨道)
#### 颜色类 [!toc]
- `.hv-progress-bar--default` - 默认颜色变体
- `.hv-progress-bar--accent` - 强调色变体
- `.hv-progress-bar--success` - 成功色变体
- `.hv-progress-bar--warning` - 警告色变体
- `.hv-progress-bar--danger` - 危险色变体
## API 参考
### ProgressBar
进度取值与格式化语义对齐 React Aria ProgressBar:`value` 会夹在 `[minValue, maxValue]`;`formatOptions.style` 为 `percent` 时格式化 0–1 比例,否则格式化夹紧后的原值。
| 属性 | 类型 | 默认值 | 描述 |
| ----------------- | ------------------------------------------------------------- | ---------------------- | -------------------------------------------------- |
| `value` | `number` | `0` | 当前值 |
| `minValue` | `number` | `0` | 最小值 |
| `maxValue` | `number` | `100` | 最大值 |
| `isIndeterminate` | `boolean` | `false` | 是否为不确定进度;为 true 时不输出 `aria-valuenow` |
| `size` | `"sm" \| "md" \| "lg"` | `"md"` | 进度轨道尺寸 |
| `color` | `"default" \| "accent" \| "success" \| "warning" \| "danger"` | `"accent"` | 填充条颜色 |
| `formatOptions` | `Intl.NumberFormatOptions` | `{ style: "percent" }` | 数值展示的数字格式 |
| `valueLabel` | `string \| number` | - | 自定义数值标签内容,覆盖默认格式化文本 |
| `class` | `string \| Array \| Object` | - | 合并到根元素 |
| 默认插槽 | `{ percentage, valueText, isIndeterminate }` | - | 内容或作用域插槽 |
`$attrs` 落到根元素。无 `aria-label` / `aria-labelledby` 时,根元素通过 `aria-labelledby` 关联内部 `Label`。
### ProgressBarRenderProps
默认作用域插槽提供:
| 属性 | 类型 | 描述 |
| ----------------- | --------------------- | ------------------------------------------- |
| `percentage` | `number \| undefined` | 进度百分比(0–100);不确定时为 `undefined` |
| `valueText` | `string \| undefined` | 格式化后的数值文本 |
| `isIndeterminate` | `boolean` | 是否为不确定进度 |
### ProgressBarOutput
| 属性 | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | --------------------------- |
| `class` | `string \| Array \| Object` | - | 合并到 `span` |
| 默认插槽 | - | - | 覆盖默认的 `valueText` 展示 |
渲染为 `span[data-slot="progress-bar-output"]`。
### ProgressBarTrack
| 属性 | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ------------- |
| `class` | `string \| Array \| Object` | - | 合并到 `div` |
| 默认插槽 | - | - | 通常放置 Fill |
渲染为 `div[data-slot="progress-bar-track"]`。
### ProgressBarFill
| 属性 | 类型 | 默认值 | 描述 |
| ------- | --------------------------- | ------ | -------------- |
| `class` | `string \| Array \| Object` | - | 合并到 `div` |
| `style` | `StyleValue` | - | 与计算宽度合并 |
渲染为 `div[data-slot="progress-bar-fill"]`。确定进度时宽度为 `percentage%`;不确定进度时不设置宽度,由 CSS 动画驱动。
## 相关组件
- [ProgressCircle 环形进度条](./progress-circle)
- [Meter 计量条](./meter)