Progress 进度条
展示操作进度或数据占比,有线形、环形、仪表盘三种样式。
引入
import { HuiProgress } from '@nscode/handy-ui'
基础用法
percentage 是百分比(0~100),默认是线形进度条,右侧显示百分比文字。超出 0~100 时按边界显示,例如 120 显示成 100%。
<template>
<HuiSpace direction="vertical" fill>
<HuiProgress :percentage="0" />
<HuiProgress :percentage="50" />
<HuiProgress :percentage="100" />
<HuiProgress :percentage="120" />
</HuiSpace>
</template>
类型
type 有三种:
line(默认):线形,宽度撑满父元素,文字在右侧。circle:完整的圆环,从顶部顺时针增长,文字在中间。dashboard:仪表盘,底部留出 1/4 缺口,从缺口左侧顺时针增长。
<template>
<HuiSpace direction="vertical" fill>
<div class="row">
<span class="row-label">line</span>
<HuiProgress :percentage="75" />
</div>
<HuiSpace wrap size="large">
<div class="cell">
<HuiProgress type="circle" :percentage="75" />
<span class="row-label">circle</span>
</div>
<div class="cell">
<HuiProgress type="dashboard" :percentage="75" />
<span class="row-label">dashboard</span>
</div>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row {
display: flex;
align-items: center;
gap: var(--hui-spacing-3);
}
.cell {
display: flex;
flex-direction: column;
align-items: center;
gap: var(--hui-spacing-2);
}
.row-label {
flex-shrink: 0;
width: 72px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell .row-label {
width: auto;
}
</style>
状态
status 设为 success、warning、exception 时,进度条和外置文字(环形、仪表盘是中间的文字)使用对应的成功色、警告色、危险色;不设时是主色。三种类型都支持:
<template>
<HuiSpace direction="vertical" fill>
<div class="row">
<span class="row-label">不设置</span>
<HuiProgress :percentage="50" />
</div>
<div class="row">
<span class="row-label">success</span>
<HuiProgress :percentage="70" status="success" />
</div>
<div class="row">
<span class="row-label">warning</span>
<HuiProgress :percentage="50" status="warning" />
</div>
<div class="row">
<span class="row-label">exception</span>
<HuiProgress :percentage="30" status="exception" />
</div>
<HuiSpace wrap size="large">
<div class="cell">
<HuiProgress type="circle" :percentage="50" :width="100" />
<span class="row-label">不设置</span>
</div>
<div class="cell">
<HuiProgress type="circle" :percentage="70" :width="100" status="success" />
<span class="row-label">success</span>
</div>
<div class="cell">
<HuiProgress type="circle" :percentage="50" :width="100" status="warning" />
<span class="row-label">warning</span>
</div>
<div class="cell">
<HuiProgress type="circle" :percentage="30" :width="100" status="exception" />
<span class="row-label">exception</span>
</div>
</HuiSpace>
<HuiSpace wrap size="large">
<div class="cell">
<HuiProgress type="dashboard" :percentage="50" :width="100" />
<span class="row-label">不设置</span>
</div>
<div class="cell">
<HuiProgress type="dashboard" :percentage="70" :width="100" status="success" />
<span class="row-label">success</span>
</div>
<div class="cell">
<HuiProgress type="dashboard" :percentage="50" :width="100" status="warning" />
<span class="row-label">warning</span>
</div>
<div class="cell">
<HuiProgress type="dashboard" :percentage="30" :width="100" status="exception" />
<span class="row-label">exception</span>
</div>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row {
display: flex;
align-items: center;
gap: var(--hui-spacing-3);
}
.cell {
display: flex;
flex-direction: column;
align-items: center;
gap: var(--hui-spacing-2);
}
.row-label {
flex-shrink: 0;
width: 72px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell .row-label {
width: auto;
}
</style>
文字位置
线形进度条的文字有三种显示方式:
- 外置(默认):在进度条右侧,给文字留出“100%”的宽度,几条进度条上下排列时长度一致。
- 内置:
text-inside把文字放进已完成那一段的末端,文字是白色。需要配合较粗的stroke-width(建议不小于 16),否则文字放不下。设置了status时文字仍是白色,进度条换成状态色。 - 不显示:
show-text设为false。
环形和仪表盘的文字固定在中间,text-inside 对它们无效,show-text 设为 false 时不显示。
<template>
<HuiSpace direction="vertical" fill>
<div class="row">
<span class="row-label">外置(默认)</span>
<HuiProgress :percentage="70" />
</div>
<div class="row">
<span class="row-label">内置</span>
<HuiProgress :percentage="70" text-inside :stroke-width="20" />
</div>
<div class="row">
<span class="row-label">内置 + 状态</span>
<HuiProgress :percentage="70" text-inside :stroke-width="20" status="success" />
</div>
<div class="row">
<span class="row-label">不显示</span>
<HuiProgress :percentage="70" :show-text="false" />
</div>
<HuiSpace wrap size="large">
<div class="cell">
<HuiProgress type="circle" :percentage="70" :width="100" />
<span class="row-label">环形</span>
</div>
<div class="cell">
<HuiProgress type="circle" :percentage="70" :width="100" :show-text="false" />
<span class="row-label">环形不显示文字</span>
</div>
<div class="cell">
<HuiProgress type="dashboard" :percentage="70" :width="100" :show-text="false" />
<span class="row-label">仪表盘不显示文字</span>
</div>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row {
display: flex;
align-items: center;
gap: var(--hui-spacing-3);
}
.cell {
display: flex;
flex-direction: column;
align-items: center;
gap: var(--hui-spacing-2);
}
.row-label {
flex-shrink: 0;
width: 88px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell .row-label {
width: auto;
}
</style>
粗细和大小
stroke-width 在线形里是进度条的高度,在环形、仪表盘里是圆环的线宽,默认 6px。width 是环形、仪表盘的画布宽度(也是高度),默认 126px,对线形无效。
<template>
<HuiSpace direction="vertical" fill>
<div class="row">
<span class="row-label">4px</span>
<HuiProgress :percentage="60" :stroke-width="4" />
</div>
<div class="row">
<span class="row-label">6px(默认)</span>
<HuiProgress :percentage="60" />
</div>
<div class="row">
<span class="row-label">10px</span>
<HuiProgress :percentage="60" :stroke-width="10" />
</div>
<div class="row">
<span class="row-label">16px</span>
<HuiProgress :percentage="60" :stroke-width="16" />
</div>
<HuiSpace wrap size="large" align="end">
<HuiProgress type="circle" :percentage="60" :width="80" :stroke-width="4" />
<HuiProgress type="circle" :percentage="60" />
<HuiProgress type="circle" :percentage="60" :width="180" :stroke-width="12" />
<HuiProgress type="dashboard" :percentage="60" :width="180" :stroke-width="16" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row {
display: flex;
align-items: center;
gap: var(--hui-spacing-3);
}
.row-label {
flex-shrink: 0;
width: 72px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
自定义颜色
color 有三种写法:
- 字符串:固定颜色。
- 函数
(percentage) => string:按当前百分比返回颜色。 - 颜色数组:按百分比在相邻两个颜色之间渐变。例如两个颜色时,0% 是第一个、100% 是第二个、50% 是两者的中间色。只有 6 位或 3 位 hex 颜色能渐变;其他写法(
'red'、CSS 变量)在两个颜色之间按位置取其一。只有一个颜色时直接用它,空数组时用主色。
设置了 status 时,状态色优先于 color。
<script setup lang="ts">
const color = (percentage: number) => {
if (percentage < 30) return 'var(--hui-color-danger)'
if (percentage < 70) return 'var(--hui-color-warning)'
return 'var(--hui-color-success)'
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiProgress :percentage="60" color="#ff6b6b" />
<HuiProgress :percentage="50" :color="color" />
<HuiProgress :percentage="80" :color="['#ff6b6b', '#4ecdc4']" />
</HuiSpace>
</template>
环形和仪表盘也一样:
<script setup lang="ts">
const color = (percentage: number) => {
if (percentage < 30) return 'var(--hui-color-danger)'
if (percentage < 70) return 'var(--hui-color-warning)'
return 'var(--hui-color-success)'
}
</script>
<template>
<HuiSpace wrap size="large">
<HuiProgress type="circle" :percentage="60" color="#ff6b6b" />
<HuiProgress type="circle" :percentage="20" :color="color" />
<HuiProgress type="dashboard" :percentage="80" :color="['#ff6b6b', '#ffd166', '#4ecdc4']" />
</HuiSpace>
</template>
自定义文字
format 接收当前百分比(已截断到 0~100),返回要显示的文字;showText 为 false 时不显示文字。
<script setup lang="ts">
const format = (percentage: number) => (percentage === 100 ? '已完成' : `${percentage} 分`)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiProgress :percentage="60" :format="format" />
<HuiProgress :percentage="40" :show-text="false" />
<HuiSpace wrap size="large">
<HuiProgress type="circle" :percentage="100" status="success" :format="format" />
<HuiProgress type="dashboard" :percentage="85" :format="format" />
</HuiSpace>
</HuiSpace>
</template>
动态进度
点按钮增减进度,宽度和弧长变化时有 0.3 秒的过渡动画。这里到 100% 时把 status 设为 success,仪表盘用颜色数组从红色渐变到绿色。
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiButton, HuiButtonGroup, HuiProgress, HuiSpace } from '@nscode/handy-ui'
import type { ProgressStatus } from '@nscode/handy-ui'
const percentage = ref(20)
const status = computed<ProgressStatus | undefined>(() =>
percentage.value === 100 ? 'success' : undefined
)
const decrease = () => {
percentage.value = Math.max(0, percentage.value - 10)
}
const increase = () => {
percentage.value = Math.min(100, percentage.value + 10)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiProgress :percentage="percentage" :status="status" />
<HuiSpace wrap size="large">
<HuiProgress type="circle" :percentage="percentage" :status="status" />
<HuiProgress type="dashboard" :percentage="percentage" :color="['#ff6b6b', '#4ecdc4']" />
</HuiSpace>
<div>
<HuiButtonGroup>
<HuiButton icon="minus" :disabled="percentage <= 0" @click="decrease">减少 10%</HuiButton>
<HuiButton icon="plus" :disabled="percentage >= 100" @click="increase">增加 10%</HuiButton>
</HuiButtonGroup>
</div>
</HuiSpace>
</template>
多段进度与图例
segments 把一条线形进度分成多段,每段 value 都是百分比。传入后忽略 percentage、color、status,右侧文字显示各段总和。没写段颜色时按主题的 primary、success、warning、danger、help 循环;段自己的 color 优先。任一段写了 label 就显示默认图例,图例与条共用颜色和有效值。
<script setup lang="ts">
import { HuiProgress, HuiSpace } from '@nscode/handy-ui'
import type { ProgressSegment } from '@nscode/handy-ui'
const segments: ProgressSegment[] = [
{ value: 30, label: '文档' },
{ value: 42, label: '图片', color: 'var(--hui-color-help)' },
{ value: 0, label: '待分配' }
]
const unlabeled: ProgressSegment[] = [{ value: 25 }, { value: 20 }, { value: 15 }]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiProgress :segments="segments" aria-label="存储空间占用" />
<HuiProgress :segments="unlabeled" :show-text="false" aria-label="批量任务进度" />
<HuiProgress :segments="segments" text-inside :stroke-width="24" aria-label="存储空间占用" />
</HuiSpace>
</template>
多段边界与总和文字
总和超过 100 时按顺序截断:80、60、5 显示为 80、20、0,并在开发环境提示一次。负数、NaN、Infinity 按 0;零值色段不占宽度,默认图例仍保留该项。segments=[] 也是多段模式,显示 0,而不是退回 percentage。
format 收到截断后的总和。textInside 把总和文字放在已用区域末端,showText=false 仅隐藏这段文字,不隐藏图例。
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiProgress, HuiButton, HuiSpace } from '@nscode/handy-ui'
const overflow = ref(false)
const inside = ref(false)
const showText = ref(true)
const segments = computed(() => [
{ value: 80, label: '成功' },
{ value: overflow.value ? 60 : 15, label: '失败' },
{ value: 5, label: '处理中' }
])
const invalid = [
{ value: -5, label: '负数' },
{ value: NaN, label: '非有限数' },
{ value: 35, label: '有效部分' }
]
const format = (total: number) => `已处理 ${total}/100`
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @click="overflow = !overflow">模拟溢出:{{ overflow }}</HuiButton>
<HuiButton @click="inside = !inside">内置文字:{{ inside }}</HuiButton>
<HuiButton @click="showText = !showText">显示文字:{{ showText }}</HuiButton>
</HuiSpace>
<HuiProgress
:segments="segments"
:format="format"
:text-inside="inside"
:stroke-width="inside ? 24 : 8"
:show-text="showText"
aria-label="批量任务"
/>
<HuiProgress :segments="invalid" aria-label="异常数据示例" />
<HuiProgress :segments="[]" :percentage="60" aria-label="空多段进度" />
</HuiSpace>
</template>
自定义多段图例
legend 插槽收到规范化的 segments 副本和 total:每段已补默认颜色、截断数值,不修改传入数组。即使各段没有 label,写了插槽也会显示图例区域。
<script setup lang="ts">
import { HuiProgress, HuiSpace } from '@nscode/handy-ui'
const segments = [
{ value: 30, label: '已用' },
{ value: 20, label: '冻结' }
]
</script>
<template>
<HuiProgress :segments="segments" aria-label="预算使用情况">
<template #legend="{ segments: effective, total }">
<HuiSpace wrap>
<span v-for="(item, index) in effective" :key="index" :style="{ color: item.color }">
{{ item.label }}:{{ item.value }}%
</span>
<strong>尚余 {{ 100 - total }}%</strong>
</HuiSpace>
</template>
</HuiProgress>
</template>
绝对值换算为多段占比
容量、预算等绝对值由使用方除以总量,换成百分比。可通过 format 显示绝对数量,通过 aria-label 为进度命名;需要读出绝对数量时显式提供 aria-valuetext。
<script setup lang="ts">
import { computed } from 'vue'
import { HuiProgress } from '@nscode/handy-ui'
const capacity = 200
const usage = [
{ label: '文档', amount: 60 },
{ label: '图片', amount: 84 }
]
const segments = computed(() =>
usage.map((item) => ({ label: item.label, value: (item.amount / capacity) * 100 }))
)
const used = usage.reduce((sum, item) => sum + item.amount, 0)
const format = (total: number) => `${(total / 100) * capacity}GB / ${capacity}GB`
</script>
<template>
<HuiProgress
:segments="segments"
:format="format"
aria-label="存储空间占用"
:aria-valuetext="`已使用 ${used}GB,总容量 ${capacity}GB`"
/>
</template>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| percentage | number | 0 | 百分比,超出 0~100 时按边界显示 |
| segments | ProgressSegment[] | - | 仅线形多段模式;空数组也生效,忽略 percentage/color/status,总和按顺序截断到100 |
| type | 'line' | 'circle' | 'dashboard' | 'line' | 类型 |
| status | 'success' | 'warning' | 'exception' | - | 状态,设置后使用对应的功能色,优先于 color |
| strokeWidth | number | 6 | 线形是进度条的高度,环形和仪表盘是圆环的线宽(px) |
| textInside | boolean | false | 文字是否显示在进度条里面,只对线形有效 |
| color | string | string[] | ((percentage: number) => string) | - | 进度条颜色,数组表示在多个颜色之间按百分比渐变,见“自定义颜色” |
| width | number | 126 | 环形和仪表盘的画布宽度(px) |
| showText | boolean | true | 是否显示文字 |
| format | (percentage: number) => string | - | 自定义文字内容,参数是截断到 0~100 之后的百分比 |
Slots
| 插槽 | 作用域参数 | 说明 |
|---|---|---|
| legend | { segments: ProgressSegment[], total: number } | 线形多段的图例,收到规范化副本和总和;优先于默认图例 |
类型
import type {
ProgressProps,
ProgressType,
ProgressStatus,
ProgressColor,
ProgressSegment,
ProgressSlots
} from '@nscode/handy-ui'
type ProgressType = 'line' | 'circle' | 'dashboard'
type ProgressStatus = 'success' | 'warning' | 'exception'
type ProgressColorFn = (percentage: number) => string
type ProgressColor = string | string[] | ProgressColorFn
interface ProgressSegment {
value: number
color?: string
label?: string
}
interface ProgressProps {
percentage?: number
segments?: ProgressSegment[]
type?: ProgressType
status?: ProgressStatus
strokeWidth?: number
textInside?: boolean
color?: ProgressColor
width?: number
showText?: boolean
format?: (percentage: number) => string
}