Arco Design React Progress 进度条组件全指南:类型、状态、动画与源码原理详解

原创2026-10-13 21:49:531,668 阅读
文章标签:UI组件前端设计系统

Arco Design React Progress 进度条组件全指南:类型、状态、动画与源码原理详解

Progress 是 Arco Design React 组件库中的反馈类组件,用于向用户展示当前系统正在执行任务的运行状态,常见于上传、下载、表单提交、数据加载等需要等待一段时间的场景,帮助缓解用户在等待过程中产生的焦虑。本文基于当前仓库中 Progress 组件文档 展开,结合 组件入口实现、线形进度条实现、环形进度条实现、步骤进度条实现 以及 单元测试 等仓库源码,系统讲解该组件的全部 API、三种形态、状态与配色机制、缓冲与动画效果,并深入其底层渲染原理,帮助读者在实际项目中熟练使用并能自行排查问题。

一、组件概览:给等待一个明确反馈

Progress 组件通过视觉化的进度表达,将「正在进行、完成多少、是否成功/失败」等状态直观呈现给用户。它支持三种展示形态:

  • 线形进度条(type="line",默认):横向进度条,适合宽度受限的内容区域;
  • 环形进度条(type="circle"):基于 SVG 绘制的圆形进度,适合需要突出视觉焦点或空间狭小的场景;
  • 步骤进度条(传入 steps 后自动启用):以离散的步骤块展示进度,适合分步骤流程类任务。

组件默认导出位于 components/index.tsx,可通过 import { Progress } from '@arco-design/web-react'; 引入,其属性类型 ProgressProps 定义在 components/Progress/interface.ts 中。

二、API 属性全表

以下为 组件文档 中给出的完整 API 表格,与 interface.ts 的类型定义一一对应:

属性 描述 类型 默认值 版本
animation 动画效果,仅在 type="line" 时可用 boolean - -
buffer 加载中的进度条是否显示缓冲区,仅对 type="line" 且加载中的进度条有效 boolean - -
showText 是否展示文本 boolean true -
percent 百分比 number (必填) 0 -
steps 显示步骤进度条 number - 2.10.0
strokeWidth 进度条线的宽度 number - -
trailColor 剩余进度条颜色 string - -
size 不同尺寸的进度条 'small' | 'default' | 'mini' | 'large' default -
status 进度条状态 'success' | 'error' | 'normal' | 'warning' - 2.16.0
type 进度条类型 'line' | 'circle' line -
bufferColor 缓冲区的颜色 string | object - -
className 节点类名 string | string[] - -
color 进度条颜色,优先级高于 status;传入对象时显示渐变色 string | { [key: string]: string } - 2.10.0
style 节点样式 CSSProperties - -
width 进度条的宽度。circle 类型的进度条仅支持数字类型的 width string | number - -
formatText 进度条文本函数 (percent: number) => ReactNode - -

从 index.tsx 可以看到组件入口定义的默认值:type: 'line'、showText: true、percent: 0、size: 'default'。同时,该组件通过 useMergeProps 支持 ConfigProvider 级别的全局组件配置覆盖(componentConfig?.Progress)。

三、快速上手:基础用法

最简单的用法是只传入必填的 percent 属性:

import { Progress } from '@arco-design/web-react';

const App = () => {
  return (
    <div>
      <Progress percent={30} width="40%" />
    </div>
  );
};

export default App;

其中 width 用于控制整个组件的宽度(既支持 "40%" 这样的字符串,也支持 200 这样的数字),percent 表示当前进度值。仓库的 单元测试 验证了 percent 与 width 的渲染行为:

  • percent 变化时,文本节点 .arco-progress-line-text 显示对应的百分比,进度条内部元素 .arco-progress-line-inner 的 width 同步变为对应的百分比值;
  • width="100%" 时根节点 .arco-progress 的样式宽度为 100%;
  • width={200} 时根节点宽度为 200px。

四、三种形态的实现原理与使用

4.1 形态选择逻辑

组件入口在渲染前会对 type 做一次归一化处理(index.tsx):

const type = steps && props.type !== 'circle' ? 'steps' : props.type;

也就是说:只要传入了 steps 且 type 不是 circle,组件就会优先渲染为步骤进度条;type="circle" 则渲染环形进度条;其余情况渲染线形进度条。此外,当 size="mini" 且形态为 line 时,组件会内部复用一个「圆环」来呈现微型进度(index.tsx),并在 width/height 上给出默认值 16。

4.2 线形进度条(line)

线形进度条由 line-progess.tsx 渲染,DOM 结构分为外层轨道(.arco-progress-line-outer)与内层进度(.arco-progress-line-inner),并为无障碍访问设置了 role="progressbar"、aria-valuemin={0}、aria-valuemax={100}、aria-valuenow={percent}。外层轨道的 backgroundColor 即 trailColor(剩余部分颜色),内层宽度按 percent 百分比计算,且进度条有 transition: width 0.6s 的平滑过渡(见 style/index.less)。

未显式指定 strokeWidth 时,线形进度条会按 size 使用默认线宽(line-progess.tsx):small 为 3、default 为 4、large 为 8。

4.3 环形进度条(circle)

设置 type="circle" 后展示环形进度条,由 circle-progress.tsx 基于 SVG 渲染:

import { Progress, Slider } from '@arco-design/web-react';

function App() {
  const [value, setValue] = React.useState(20);
  return (
    <div>
      <Progress type="circle" percent={value} style={{ marginRight: 80 }} />
      <Progress type="circle" percent={value} status="warning" style={{ marginRight: 80 }} />
      <Progress type="circle" percent={value} status="error" style={{ marginRight: 80 }} />
      <Progress type="circle" percent={value} status="success" style={{ marginRight: 80 }} />
    </div>
  );
}

其实现要点(circle-progress.tsx):

  • 几何计算:radius = (width - strokeWidth) / 2,perimeter = 2π × radius,进度弧线通过 strokeDasharray: perimeter 与 strokeDashoffset: (1 - percent / 100) * perimeter 控制;
  • 默认尺寸:mini 16、small 48、default 64、large 80;默认线宽 mini 4、small 3、default 4、large 4(circle-progress.tsx);
  • 环形 mask:底层圆环(.arco-progress-circle-mask)的 stroke 即 trailColor,用于表示剩余部分;
  • 进度弧动画:stroke-dashoffset 与 stroke 均带 0.6s 过渡(style/index.less),因此 percent 变化时环会平滑转动;
  • 文本定位:百分比文本通过绝对定位居中于圆环中心(style/index.less)。

4.4 步骤进度条(steps)

通过设置 steps 展示步骤进度条(自 2.10.0 版本起支持),由 steps-progress.tsx 渲染:

<Progress steps={3} percent={30} style={{ marginBottom: 20 }} />
<Progress steps={5} percent={100} status="warning" style={{ marginBottom: 20 }} />
<Progress steps={5} size="small" percent={50} status="success" />

实现上,组件根据 steps 数量生成对应个数的块(.arco-progress-steps-item),isActive 的判定条件为 percent > (100 / steps) * index(steps-progress.tsx),激活块的背景色取自 color,未激活块的背景色取自 trailColor。样式上,步骤块通过 flex: 1 平均分配宽度,首尾两端的块带 100px 圆角(style/index.less);当 size="small" 时每块固定为 2px 宽的细条形态。

五、状态:success / error / warning / normal

status 属性(自 2.16.0 版本起支持)用于呈现内置状态样式,可选值为 'success' | 'error' | 'normal' | 'warning'。如果不传 status,组件会依据 percent 自动推导(index.tsx):

const status = 'status' in props ? props.status : percent >= 100 ? 'success' : 'normal';

即 进度达到 100% 时自动进入 success 状态,否则为 normal。

各状态在样式上对应不同的主题色(style/token.less):success 使用 @color-success-6、error 使用 @color-danger-6、warning 使用 @color-warning-6、normal 使用主题色 @color-primary-6。文本部分的状态差异体现在 line-progess.tsx 与 circle-progress.tsx 的 getText 逻辑中:

  • 线形/步骤形态下,status="error" 时文本显示 percent% 并追加一个感叹号图标 IconExclamationCircleFill;
  • 环形形态下,status="success" 显示对勾图标 IconCheck,status="error" 显示感叹号图标 IconExclamation;
  • 其余情况显示纯百分比文本。

配合 showText={false} 可以隐藏百分比文本,仅保留进度条本身:

<Progress percent={value} status="error" style={{ marginBottom: 20 }} />
<Progress percent={value} status="success" style={{ marginBottom: 20 }} />
<Progress percent={value} showText={false} />

六、文本定制:formatText 与 showText

formatText 接收 (percent: number) => ReactNode,用于完全自定义文本内容。它在线形(line-progess.tsx)、环形(circle-progress.tsx)与步骤(steps-progress.tsx)三种形态中都会优先生效。常见用法:

<Progress
  percent={value}
  color="#C9CDD4"
  formatText={() => 'Waiting...'}
/>
<Progress
  percent={value}
  status="warning"
  formatText={(val) => `${val} / 100`}
/>

showText 默认值为 true;当设置为 false 时,三种形态均不再渲染文本节点。需要注意,环形形态下 size="mini" 时文本默认不显示(circle-progress.tsx),而是通过 Tooltip 在悬停时展示百分比(circle-progress.tsx)。

七、尺寸体系:mini / small / default / large

size 支持 'small' | 'default' | 'mini' | 'large' 四种取值,默认 default。它同时影响线形与环形两种形态的默认线宽、默认宽度和字号:

  • 线形:small 线宽 3、default 线宽 4、large 线宽 8,字号 large 为 16px、small/default 为正文字号(token.less);
  • 环形:默认宽度 mini 16 / small 48 / default 64 / large 80,字号与图标大小也随尺寸调整(token.less)。

在 size="mini" 时,即使 type="line",组件也会渲染成一个 16×16 的迷你圆环(index.tsx),进度状态以颜色表达;当 status="success" 时直接显示一个对勾图标(circle-progress.tsx)。展示示例可参考 微型进度条 Demo 与 不同尺寸 Demo。

八、颜色定制:color、trailColor 与渐变色

8.1 进度条颜色 color

color 属性(自 2.10.0 起支持)用于设置进度条颜色,优先级高于 status。传入字符串时作为单色使用:

<Progress percent={value} color="#C9CDD4" />

线形进度条在收到字符串颜色时设置 backgroundColor(line-progess.tsx)。

8.2 渐变色进度条

当 color 传入对象 { [key: string]: string } 时,会生成 linear-gradient 渐变色。对象的键为渐变位置(如 '0%'、'100%'),值为该位置的颜色:

<Progress
  percent={80}
  color={{
    '0%': 'rgb(var(--primary-6))',
    '100%': 'rgb(var(--success-6))',
  }}
  animation
  width={300}
/>

线形形态下,渐变色通过 linear-gradient(to right, color0% 0%, color100% 100%) 生成,并且当传入 percent 时还会设置 backgroundSize,保证渐变随进度宽度铺满整个进度区域(line-progess.tsx)。

环形形态下,渐变通过 SVG 内部的 <linearGradient> 定义,并在圆环的 <circle> 上以 stroke: url(#id) 引用(circle-progress.tsx):

<Progress
  type="circle"
  color={{
    '0%': 'rgb(var(--primary-6))',
    '100%': 'rgb(var(--success-6))',
  }}
  percent={80}
/>

8.3 剩余进度条颜色 trailColor

trailColor 用于设置剩余进度条(轨道)的颜色:

<Progress percent={30} trailColor="var(--color-primary-light-1)" />
<Progress steps={4} percent={30} trailColor="var(--color-primary-light-1)" />
<Progress percent={30} type="circle" trailColor="var(--color-primary-light-1)" />

其生效路径为:线形形态下设置外层轨道 .arco-progress-line-outer 的 backgroundColor(line-progess.tsx),环形形态下设置底层圆环 .arco-progress-circle-mask 的 stroke(circle-progress.tsx),步骤形态下设置未激活步骤块的背景色(steps-progress.tsx)。相关行为也有单元测试覆盖(index.test.tsx)。

九、缓冲区效果:buffer 与 bufferColor

buffer 用于模拟「加载中」的缓冲效果,仅对 type="line" 且处于加载中的进度条有效。开启后,组件会在进度条内层后方渲染一个缓冲区(.arco-progress-line-inner-buffer),宽度为 percent + 10%(line-progess.tsx):

<Progress percent={value} buffer />

缓冲区默认颜色为 --color-primary-light-3 对应的浅色(token.less),可通过 bufferColor 自定义,支持字符串或对象(传入对象时同样按渐变色处理,复用 getBackground 逻辑)。注意:当进度进入 success/error 或达到 100% 时,缓冲区会隐藏(line-progess.tsx 与 isFinish 判断)。

十、动画效果:animation

设置 animation 为 true 时,进度条内层会出现一个从左向右循环扫过的白色高光带,模拟「加载中」的动态效果。该属性仅在 type="line" 时生效:

<Progress percent={80} animation width={300} />
<Progress percent={80} status="success" animation width={300} />

实现上,.arco-progress-line-inner-animate::after 伪元素使用 linear-gradient(90deg, transparent 25%, rgba(255,255,255,50%) 50%, transparent 75%) 配合 background-size: 400% 100% 与 @keyframes progress-loading(1.5s 循环,背景位置从 100% 扫到 0%),形成高光扫过效果(style/index.less 与 L362-L370)。

十一、无障碍与国际化支持

  • 无障碍:线形与环形进度条均设置 role="progressbar" 与 aria-valuemin={0}、aria-valuemax={100}、aria-valuenow={percent},便于屏幕阅读器读取进度(line-progess.tsx、circle-progress.tsx);
  • RTL:组件根节点在 ConfigContext 的 rtl 为真时添加 .arco-progress-rtl 类(index.tsx),样式层针对文本边距、缓冲区位置、步骤块间距做了镜像处理(style/index.less);
  • 组件级配置:Progress 通过 useMergeProps 支持来自 ConfigProvider 的全局默认属性覆盖(index.tsx),可在应用层面统一设置 size、showText 等默认值。

十二、样式定制指引

组件的样式入口为 components/Progress/style/index.ts,其引入了全局基础样式、自身样式以及 Tooltip 样式(mini 形态依赖 Tooltip 展示文本)。全部样式变量集中在 components/Progress/style/token.less,主要包括:

  • 线形:轨道背景 @progress-line-color-line-bg、内层背景 @progress-line-color-inner-bg(及其 _success/_error/_warning 变体)、缓冲背景 @progress-line-color-buffer-bg、文本颜色与间距、图标颜色与大小;
  • 环形:mask 描边 @progress-circle-color-mask-stroke、路径描边 @progress-circle-color-path-stroke(及状态变体)、文本字号与颜色、mini 形态的状态浅色描边;
  • 步骤:步骤块默认背景 @progress-steps-color-item-bg、激活背景 _normal/_success/_error/_warning 变体、块间距与 small 形态的块宽 @progress-steps-size-small-steps-item-width。

十三、版本演进与验证

从 Progress 变更日志 可以看到该组件的主要能力演进:

  • 2.10.0:支持渐变色进度条与步骤进度条(steps、color 对象语法);
  • 2.13.2:优化渐变色的显示,修正 formatText 的 TypeScript 定义;
  • 2.16.0:新增 warning 状态;
  • 2.24.0:新增 trailColor 属性;
  • 2.25.0:修复 trailColor 在环形与步骤进度条上不生效的问题,修复步骤显示错乱问题。

上述行为均有对应的 单元测试 与 快照测试 保障:测试覆盖了 percent 文本与内层宽度联动、width 字符串/数字两种取值、trailColor 在线形与环形上的渲染位置,以及 mountTest(挂载/卸载冒烟测试)与 componentConfigTest(ConfigProvider 全局配置生效验证)。

结语

通过本文,读者已完整掌握 Arco Design React Progress 组件的全部 16 个公开属性、三种形态的切换规则(steps 优先于 circle、mini 复用圆环)、状态自动推导逻辑(percent >= 100 自动 success)、颜色优先级(color 高于 status)以及线形渐变与环形 SVG 渐变两种不同的实现路径。在实际项目中,推荐的做法是:普通场景直接使用 type="line" + percent;需要展示具体阶段进度时使用 steps;需要强调任务状态(如上传完成/失败)时配合 status 与 showText;需要品牌化视觉时通过 color(含渐变对象)与 trailColor 定制,并可在 ConfigProvider 中统一设置默认尺寸,保证整个应用进度视觉的一致性。

登录后查看全文
arco-design