This commit is contained in:
2026-05-25 17:08:18 +08:00
parent df501f6151
commit f1259b71b5
9178 changed files with 1626125 additions and 0 deletions
@@ -0,0 +1,18 @@
import * as React from 'react';
import type { ProgressProps } from '..';
import type { StrokeColorObject } from '../interface';
export interface ColorGradientProps {
prefixCls: string;
className?: string;
gradientId: string;
style: React.CSSProperties;
ptg: number;
radius: number;
strokeLinecap: ProgressProps['strokeLinecap'];
strokeWidth: ProgressProps['strokeWidth'];
size: number;
color: string | StrokeColorObject;
gapDegree: number;
}
declare const PtgCircle: React.ForwardRefExoticComponent<ColorGradientProps & React.RefAttributes<SVGCircleElement>>;
export default PtgCircle;
@@ -0,0 +1,79 @@
import * as React from 'react';
import { clsx } from 'clsx';
const Block = ({
bg,
children
}) => /*#__PURE__*/React.createElement("div", {
style: {
width: '100%',
height: '100%',
background: bg
}
}, children);
function getPtgColors(color, scale) {
return Object.keys(color).map(key => {
const parsedKey = parseFloat(key);
const ptgKey = `${Math.floor(parsedKey * scale)}%`;
return `${color[key]} ${ptgKey}`;
});
}
const PtgCircle = /*#__PURE__*/React.forwardRef((props, ref) => {
const {
prefixCls,
color,
gradientId,
radius,
className,
style: circleStyleForStack,
ptg,
strokeLinecap,
strokeWidth,
size,
gapDegree
} = props;
const isGradient = color && typeof color === 'object';
const stroke = isGradient ? `#FFF` : undefined;
// ========================== Circle ==========================
const halfSize = size / 2;
const circleNode = /*#__PURE__*/React.createElement("circle", {
className: clsx(`${prefixCls}-circle-path`, className),
r: radius,
cx: halfSize,
cy: halfSize,
stroke: stroke,
strokeLinecap: strokeLinecap,
strokeWidth: strokeWidth,
opacity: ptg === 0 ? 0 : 1,
style: circleStyleForStack,
ref: ref
});
// ========================== Render ==========================
if (!isGradient) {
return circleNode;
}
const maskId = `${gradientId}-conic`;
const fromDeg = gapDegree ? `${180 + gapDegree / 2}deg` : '0deg';
const conicColors = getPtgColors(color, (360 - gapDegree) / 360);
const linearColors = getPtgColors(color, 1);
const conicColorBg = `conic-gradient(from ${fromDeg}, ${conicColors.join(', ')})`;
const linearColorBg = `linear-gradient(to ${gapDegree ? 'bottom' : 'top'}, ${linearColors.join(', ')})`;
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("mask", {
id: maskId
}, circleNode), /*#__PURE__*/React.createElement("foreignObject", {
x: 0,
y: 0,
width: size,
height: size,
mask: `url(#${maskId})`
}, /*#__PURE__*/React.createElement(Block, {
bg: linearColorBg
}, /*#__PURE__*/React.createElement(Block, {
bg: conicColorBg
}))));
});
if (process.env.NODE_ENV !== 'production') {
PtgCircle.displayName = 'PtgCircle';
}
export default PtgCircle;
@@ -0,0 +1,4 @@
import * as React from 'react';
import type { ProgressProps } from '../interface';
declare const Circle: React.FC<ProgressProps>;
export default Circle;
+153
View File
@@ -0,0 +1,153 @@
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import * as React from 'react';
import { clsx } from 'clsx';
import { defaultProps, useTransitionDuration } from "../common";
import useId from "@rc-component/util/es/hooks/useId";
import PtgCircle from "./PtgCircle";
import { VIEW_BOX_SIZE, getCircleStyle } from "./util";
import getIndeterminateCircle from "../utils/getIndeterminateCircle";
function toArray(value) {
const mergedValue = value ?? [];
return Array.isArray(mergedValue) ? mergedValue : [mergedValue];
}
const Circle = props => {
const {
id,
prefixCls,
classNames = {},
styles = {},
steps,
strokeWidth,
railWidth,
gapDegree = 0,
gapPosition,
railColor,
strokeLinecap,
style,
className,
strokeColor,
percent,
loading,
...restProps
} = {
...defaultProps,
...props
};
const halfSize = VIEW_BOX_SIZE / 2;
const mergedId = useId(id);
const gradientId = `${mergedId}-gradient`;
const radius = halfSize - strokeWidth / 2;
const perimeter = Math.PI * 2 * radius;
const rotateDeg = gapDegree > 0 ? 90 + gapDegree / 2 : -90;
const perimeterWithoutGap = perimeter * ((360 - gapDegree) / 360);
const {
count: stepCount,
gap: stepGap
} = typeof steps === 'object' ? steps : {
count: steps,
gap: 2
};
const percentList = toArray(percent);
const strokeColorList = toArray(strokeColor);
const gradient = strokeColorList.find(color => color && typeof color === 'object');
const isConicGradient = gradient && typeof gradient === 'object';
const mergedStrokeLinecap = isConicGradient ? 'butt' : strokeLinecap;
const {
indeterminateStyleProps,
indeterminateStyleAnimation
} = getIndeterminateCircle({
id: mergedId,
loading
});
const circleStyle = getCircleStyle(perimeter, perimeterWithoutGap, 0, 100, rotateDeg, gapDegree, gapPosition, railColor, mergedStrokeLinecap, strokeWidth);
const paths = useTransitionDuration();
const getStokeList = () => {
let stackPtg = 0;
return percentList.map((ptg, index) => {
const color = strokeColorList[index] || strokeColorList[strokeColorList.length - 1];
const circleStyleForStack = getCircleStyle(perimeter, perimeterWithoutGap, stackPtg, ptg, rotateDeg, gapDegree, gapPosition, color, mergedStrokeLinecap, strokeWidth);
stackPtg += ptg;
return /*#__PURE__*/React.createElement(PtgCircle, {
key: index,
color: color,
ptg: ptg,
radius: radius,
prefixCls: prefixCls,
gradientId: gradientId,
className: classNames.track,
style: {
...circleStyleForStack,
...indeterminateStyleProps,
...styles.track
},
strokeLinecap: mergedStrokeLinecap,
strokeWidth: strokeWidth,
gapDegree: gapDegree,
ref: elem => {
// https://reactjs.org/docs/refs-and-the-dom.html#callback-refs
// React will call the ref callback with the DOM element when the component mounts,
// and call it with `null` when it unmounts.
// Refs are guaranteed to be up-to-date before componentDidMount or componentDidUpdate fires.
paths[index] = elem;
},
size: VIEW_BOX_SIZE
});
}).reverse();
};
const getStepStokeList = () => {
// only show the first percent when pass steps
const current = Math.round(stepCount * (percentList[0] / 100));
const stepPtg = 100 / stepCount;
let stackPtg = 0;
return new Array(stepCount).fill(null).map((_, index) => {
const color = index <= current - 1 ? strokeColorList[0] : railColor;
const stroke = color && typeof color === 'object' ? `url(#${gradientId})` : undefined;
const circleStyleForStack = getCircleStyle(perimeter, perimeterWithoutGap, stackPtg, stepPtg, rotateDeg, gapDegree, gapPosition, color, 'butt', strokeWidth, stepGap);
stackPtg += (perimeterWithoutGap - circleStyleForStack.strokeDashoffset + stepGap) * 100 / perimeterWithoutGap;
return /*#__PURE__*/React.createElement("circle", {
key: index,
className: clsx(`${prefixCls}-circle-path`, classNames.track),
r: radius,
cx: halfSize,
cy: halfSize,
stroke: stroke,
strokeWidth: strokeWidth,
opacity: 1,
style: {
...circleStyleForStack,
...styles.track
},
ref: elem => {
paths[index] = elem;
}
});
});
};
return /*#__PURE__*/React.createElement("svg", _extends({
className: clsx(`${prefixCls}-circle`, classNames.root, className),
viewBox: `0 0 ${VIEW_BOX_SIZE} ${VIEW_BOX_SIZE}`,
style: {
...styles.root,
...style
},
id: id,
role: "presentation"
}, restProps), !stepCount && /*#__PURE__*/React.createElement("circle", {
className: clsx(`${prefixCls}-circle-rail`, classNames.rail),
r: radius,
cx: halfSize,
cy: halfSize,
stroke: railColor,
strokeLinecap: mergedStrokeLinecap,
strokeWidth: railWidth || strokeWidth,
style: {
...circleStyle,
...styles.rail
}
}), stepCount ? getStepStokeList() : getStokeList(), indeterminateStyleAnimation);
};
if (process.env.NODE_ENV !== 'production') {
Circle.displayName = 'Circle';
}
export default Circle;
@@ -0,0 +1,5 @@
import type { StrokeColorType } from '../interface';
import type { ProgressProps } from '..';
import type React from 'react';
export declare const VIEW_BOX_SIZE = 100;
export declare const getCircleStyle: (perimeter: number, perimeterWithoutGap: number, offset: number, percent: number, rotateDeg: number, gapDegree: number, gapPosition: ProgressProps['gapPosition'] | undefined, strokeColor: StrokeColorType, strokeLinecap: ProgressProps['strokeLinecap'], strokeWidth: number, stepSpace?: number) => React.CSSProperties;
+30
View File
@@ -0,0 +1,30 @@
export const VIEW_BOX_SIZE = 100;
export const getCircleStyle = (perimeter, perimeterWithoutGap, offset, percent, rotateDeg, gapDegree, gapPosition, strokeColor, strokeLinecap, strokeWidth, stepSpace = 0) => {
const offsetDeg = offset / 100 * 360 * ((360 - gapDegree) / 360);
const positionDeg = gapDegree === 0 ? 0 : {
bottom: 0,
top: 180,
left: 90,
right: -90
}[gapPosition];
let strokeDashoffset = (100 - percent) / 100 * perimeterWithoutGap;
// Fix percent accuracy when strokeLinecap is round
// https://github.com/ant-design/ant-design/issues/35009
if (strokeLinecap === 'round' && percent !== 100) {
strokeDashoffset += strokeWidth / 2;
// when percent is small enough (<= 1%), keep smallest value to avoid it's disappearance
if (strokeDashoffset >= perimeterWithoutGap) {
strokeDashoffset = perimeterWithoutGap - 0.01;
}
}
const halfSize = VIEW_BOX_SIZE / 2;
return {
stroke: typeof strokeColor === 'string' ? strokeColor : undefined,
strokeDasharray: `${perimeterWithoutGap}px ${perimeter}`,
strokeDashoffset: strokeDashoffset + stepSpace,
transform: `rotate(${rotateDeg + offsetDeg + positionDeg}deg)`,
transformOrigin: `${halfSize}px ${halfSize}px`,
transition: 'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s, stroke-width .06s ease .3s, opacity .3s ease 0s',
fillOpacity: 0
};
};
+4
View File
@@ -0,0 +1,4 @@
import * as React from 'react';
import type { ProgressProps } from './interface';
declare const Line: React.FC<ProgressProps>;
export default Line;
+105
View File
@@ -0,0 +1,105 @@
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import * as React from 'react';
import { clsx } from 'clsx';
import { useTransitionDuration, defaultProps } from "./common";
import getIndeterminateLine from "./utils/getIndeterminateLine";
import useId from "@rc-component/util/es/hooks/useId";
const Line = props => {
const {
id,
className,
percent,
prefixCls,
strokeColor,
strokeLinecap,
strokeWidth,
style,
railColor,
railWidth,
transition,
loading,
...restProps
} = {
...defaultProps,
...props
};
const mergedId = useId(id);
// eslint-disable-next-line no-param-reassign
delete restProps.gapPosition;
const percentList = Array.isArray(percent) ? percent : [percent];
const strokeColorList = Array.isArray(strokeColor) ? strokeColor : [strokeColor];
const paths = useTransitionDuration();
const center = strokeWidth / 2;
const right = 100 - strokeWidth / 2;
const pathString = `M ${strokeLinecap === 'round' ? center : 0},${center}
L ${strokeLinecap === 'round' ? right : 100},${center}`;
const viewBoxString = `0 0 100 ${strokeWidth}`;
let stackPtg = 0;
const {
indeterminateStyleProps,
indeterminateStyleAnimation
} = getIndeterminateLine({
id: mergedId,
loading,
percent: percentList[0],
strokeLinecap,
strokeWidth
});
return /*#__PURE__*/React.createElement("svg", _extends({
className: clsx(`${prefixCls}-line`, className),
viewBox: viewBoxString,
preserveAspectRatio: "none",
style: style
}, restProps), /*#__PURE__*/React.createElement("path", {
className: `${prefixCls}-line-rail`,
d: pathString,
strokeLinecap: strokeLinecap,
stroke: railColor,
strokeWidth: railWidth || strokeWidth,
fillOpacity: "0"
}), percentList.map((ptg, index) => {
let dashPercent = 1;
switch (strokeLinecap) {
case 'round':
dashPercent = 1 - strokeWidth / 100;
break;
case 'square':
dashPercent = 1 - strokeWidth / 2 / 100;
break;
default:
dashPercent = 1;
break;
}
const pathStyle = {
strokeDasharray: `${ptg * dashPercent}px, 100px`,
strokeDashoffset: `-${stackPtg}px`,
transition: transition || 'stroke-dashoffset 0.3s ease 0s, stroke-dasharray .3s ease 0s, stroke 0.3s linear',
...indeterminateStyleProps
};
const color = strokeColorList[index] || strokeColorList[strokeColorList.length - 1];
stackPtg += ptg;
return /*#__PURE__*/React.createElement("path", {
key: index,
className: `${prefixCls}-line-path`,
d: pathString,
strokeLinecap: strokeLinecap,
stroke: color,
strokeWidth: strokeWidth,
fillOpacity: "0",
ref: elem => {
// https://reactjs.org/docs/refs-and-the-dom.html#callback-refs
// React will call the ref callback with the DOM element when the component mounts,
// and call it with `null` when it unmounts.
// Refs are guaranteed to be up-to-date before componentDidMount or componentDidUpdate fires.
paths[index] = elem;
},
style: pathStyle
});
}), indeterminateStyleAnimation);
};
if (process.env.NODE_ENV !== 'production') {
Line.displayName = 'Line';
}
export default Line;
+3
View File
@@ -0,0 +1,3 @@
import type { ProgressProps } from './interface';
export declare const defaultProps: Partial<ProgressProps>;
export declare const useTransitionDuration: () => SVGPathElement[];
+35
View File
@@ -0,0 +1,35 @@
import { useRef, useEffect } from 'react';
export const defaultProps = {
percent: 0,
prefixCls: 'rc-progress',
strokeColor: '#2db7f5',
strokeLinecap: 'round',
strokeWidth: 1,
railColor: '#D9D9D9',
railWidth: 1,
gapPosition: 'bottom',
loading: false
};
export const useTransitionDuration = () => {
const pathsRef = useRef([]);
const prevTimeStamp = useRef(null);
useEffect(() => {
const now = Date.now();
let updated = false;
pathsRef.current.forEach(path => {
if (!path) {
return;
}
updated = true;
const pathStyle = path.style;
pathStyle.transitionDuration = '.3s, .3s, .3s, .06s';
if (prevTimeStamp.current && now - prevTimeStamp.current < 100) {
pathStyle.transitionDuration = '0s, 0s';
}
});
if (updated) {
prevTimeStamp.current = Date.now();
}
});
return pathsRef.current;
};
+4
View File
@@ -0,0 +1,4 @@
import Line from './Line';
import Circle from './Circle';
export type { ProgressProps } from './interface';
export { Line, Circle };
+3
View File
@@ -0,0 +1,3 @@
import Line from "./Line";
import Circle from "./Circle";
export { Line, Circle };
+30
View File
@@ -0,0 +1,30 @@
/// <reference types="react" />
export type SemanticName = 'root' | 'rail' | 'track';
export interface ProgressProps {
id?: string;
strokeWidth?: number;
railWidth?: number;
className?: string;
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
percent?: number | number[];
strokeColor?: StrokeColorType;
railColor?: string;
strokeLinecap?: StrokeLinecapType;
prefixCls?: string;
style?: React.CSSProperties;
gapDegree?: number;
gapPosition?: GapPositionType;
transition?: string;
onClick?: React.MouseEventHandler;
steps?: number | {
count: number;
gap: number;
};
loading?: boolean;
}
export type StrokeColorObject = Record<string, string | boolean>;
export type BaseStrokeColorType = string | StrokeColorObject;
export type StrokeColorType = BaseStrokeColorType | BaseStrokeColorType[];
export type GapPositionType = 'top' | 'right' | 'bottom' | 'left';
export type StrokeLinecapType = 'round' | 'butt' | 'square';
+1
View File
@@ -0,0 +1 @@
export {};
@@ -0,0 +1,19 @@
import React from 'react';
interface IndeterminateOption {
id: string;
loading: boolean;
}
declare const _default: ({ id, loading }: IndeterminateOption) => {
indeterminateStyleProps: {
transform?: undefined;
animation?: undefined;
};
indeterminateStyleAnimation: any;
} | {
indeterminateStyleProps: {
transform: string;
animation: string;
};
indeterminateStyleAnimation: React.JSX.Element;
};
export default _default;
@@ -0,0 +1,23 @@
import React from 'react';
export default (({
id,
loading
}) => {
if (!loading) {
return {
indeterminateStyleProps: {},
indeterminateStyleAnimation: null
};
}
const animationName = `${id}-indeterminate-animate`;
return {
indeterminateStyleProps: {
transform: 'rotate(0deg)',
animation: `${animationName} 1s linear infinite`
},
indeterminateStyleAnimation: /*#__PURE__*/React.createElement("style", null, `@keyframes ${animationName} {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}`)
};
});
@@ -0,0 +1,25 @@
import type { StrokeLinecapType } from "../interface";
import React from 'react';
interface IndeterminateOption {
id: string;
loading: boolean;
percent: number;
strokeLinecap: StrokeLinecapType;
strokeWidth: number;
}
declare const _default: (options: IndeterminateOption) => {
indeterminateStyleProps: {
strokeDasharray?: undefined;
animation?: undefined;
strokeDashoffset?: undefined;
};
indeterminateStyleAnimation: any;
} | {
indeterminateStyleProps: {
strokeDasharray: string;
animation: string;
strokeDashoffset: number;
};
indeterminateStyleAnimation: React.JSX.Element;
};
export default _default;
@@ -0,0 +1,29 @@
import React from 'react';
export default (options => {
const {
id,
percent,
strokeLinecap,
strokeWidth,
loading
} = options;
if (!loading) {
return {
indeterminateStyleProps: {},
indeterminateStyleAnimation: null
};
}
const animationName = `${id}-indeterminate-animate`;
const strokeDashOffset = 100 - (percent + (strokeLinecap === 'round' ? strokeWidth : 0));
return {
indeterminateStyleProps: {
strokeDasharray: `${percent} 100`,
animation: `${animationName} .6s linear alternate infinite`,
strokeDashoffset: 0
},
indeterminateStyleAnimation: /*#__PURE__*/React.createElement("style", null, `@keyframes ${animationName} {
0% { stroke-dashoffset: 0; }
100% { stroke-dashoffset: -${strokeDashOffset};
}`)
};
});