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 就顯示預設圖例。
總和依順序截斷至 100:80、60、5 會顯示為 80、20、0,並發出開發提示。負數與非有限數視為零;零值不占寬度,預設圖例仍保留。空陣列維持多段模式並顯示零。
format 收到有效總和;textInside 將文字放在已用區域末端,showText=false 隱藏總和文字但不隱藏圖例。legend 插槽取得已補色、截斷的段落副本和總和,不修改輸入;即使各段沒有標籤,提供插槽也會顯示圖例區域。
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiButton, HuiProgress, HuiSpace } from '@nscode/handy-ui'
import type { ProgressSegment } from '@nscode/handy-ui'
const overflow = ref(false)
const inside = ref(false)
const showText = ref(true)
const segments = computed<ProgressSegment[]>(() => [
{ value: 80, label: '成功' },
{ value: overflow.value ? 60 : 15, label: '失敗', color: 'var(--hui-color-danger)' },
{ value: 5, label: '處理中' }
])
const capacity = 200
const usage = [
{ label: '文件', amount: 60 },
{ label: '圖片', amount: 84 }
]
const storage = usage.map((item) => ({
label: item.label,
value: (item.amount / capacity) * 100
}))
const used = usage.reduce((sum, item) => sum + item.amount, 0)
const storageFormat = (total: number) => `${(total / 100) * capacity}GB / ${capacity}GB`
</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"
:text-inside="inside"
:stroke-width="inside ? 24 : 8"
:show-text="showText"
aria-label="批次任務"
/>
<HuiProgress
:segments="[
{ value: -5, label: '負數' },
{ value: NaN, label: '非有限數' },
{ value: 35, label: '有效部分' }
]"
aria-label="異常資料"
/>
<HuiProgress :segments="[]" :percentage="60" aria-label="空多段進度" />
<HuiProgress :segments="[{ value: 30 }, { value: 20 }]" aria-label="預算使用情形">
<template #legend="{ segments: effective, total }">
<HuiSpace wrap>
<span v-for="(item, index) in effective" :key="index" :style="{ color: item.color }"
>{{ item.value }}%</span
>
<strong>尚餘:{{ 100 - total }}%</strong>
</HuiSpace>
</template>
</HuiProgress>
<HuiProgress
:segments="storage"
:format="storageFormat"
aria-label="儲存空間占用"
:aria-valuetext="`已使用 ${used}GB,總容量 ${capacity}GB`"
/>
</HuiSpace>
</template>
容量或預算等絕對數量由使用方換算為百分比。以 format 顯示絕對總量;需要輔助技術讀出數量時,明確提供 aria-valuetext。
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| percentage | number | 0 | 百分比,超出 0~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 之後的百分比 |
| segments | ProgressSegment[] | - | 線形多段模式,空陣列亦生效;忽略 percentage/color/status,總和依序截斷至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 ProgressSlots {
legend?: (props: { segments: ProgressSegment[]; total: number }) => unknown
}
interface ProgressProps {
percentage?: number
segments?: ProgressSegment[]
type?: ProgressType
status?: ProgressStatus
strokeWidth?: number
textInside?: boolean
color?: ProgressColor
width?: number
showText?: boolean
format?: (percentage: number) => string
}