|
| 1 | +import * as React from 'react'; |
| 2 | +import type { ProgressProps } from '..'; |
| 3 | +import type { StrokeColorType } from '../interface'; |
| 4 | + |
| 5 | +export interface ColorGradientProps { |
| 6 | + prefixCls: string; |
| 7 | + gradientId: string; |
| 8 | + style: React.CSSProperties; |
| 9 | + ptg: number; |
| 10 | + radius: number; |
| 11 | + strokeLinecap: ProgressProps['strokeLinecap']; |
| 12 | + strokeWidth: ProgressProps['strokeWidth']; |
| 13 | + size: number; |
| 14 | + color: StrokeColorType; |
| 15 | + conic: boolean; |
| 16 | + gapDegree: number; |
| 17 | +} |
| 18 | + |
| 19 | +const PtgCircle = React.forwardRef<SVGCircleElement, ColorGradientProps>((props, ref) => { |
| 20 | + const { |
| 21 | + prefixCls, |
| 22 | + color, |
| 23 | + gradientId, |
| 24 | + radius, |
| 25 | + style: circleStyleForStack, |
| 26 | + ptg, |
| 27 | + strokeLinecap, |
| 28 | + strokeWidth, |
| 29 | + size, |
| 30 | + conic, |
| 31 | + gapDegree, |
| 32 | + } = props; |
| 33 | + |
| 34 | + const isGradient = color && typeof color === 'object'; |
| 35 | + |
| 36 | + const stroke = React.useMemo(() => { |
| 37 | + if (conic) { |
| 38 | + return '#FFF'; |
| 39 | + } |
| 40 | + |
| 41 | + return isGradient ? `url(#${gradientId})` : undefined; |
| 42 | + }, [gradientId, isGradient, conic]); |
| 43 | + |
| 44 | + // ========================== Circle ========================== |
| 45 | + const halfSize = size / 2; |
| 46 | + |
| 47 | + const circleNode = ( |
| 48 | + <circle |
| 49 | + className={`${prefixCls}-circle-path`} |
| 50 | + r={radius} |
| 51 | + cx={halfSize} |
| 52 | + cy={halfSize} |
| 53 | + stroke={stroke} |
| 54 | + strokeLinecap={strokeLinecap} |
| 55 | + strokeWidth={strokeWidth} |
| 56 | + opacity={ptg === 0 ? 0 : 1} |
| 57 | + style={circleStyleForStack} |
| 58 | + ref={ref} |
| 59 | + /> |
| 60 | + ); |
| 61 | + |
| 62 | + // ========================== Render ========================== |
| 63 | + if (!conic) { |
| 64 | + return circleNode; |
| 65 | + } |
| 66 | + |
| 67 | + const maskId = `${gradientId}-conic`; |
| 68 | + const conicColorKeys = Object.keys(color).filter((key) => key !== 'conic'); |
| 69 | + |
| 70 | + const fromDeg = gapDegree ? `${180 + gapDegree / 2}deg` : '0deg'; |
| 71 | + |
| 72 | + const conicColors = conicColorKeys.map((key) => { |
| 73 | + const parsedKey = parseFloat(key); |
| 74 | + const ptgKey = `${gapDegree ? Math.floor((parsedKey * (360 - gapDegree)) / 360) : parsedKey}%`; |
| 75 | + |
| 76 | + return `${color[key]} ${ptgKey}`; |
| 77 | + }); |
| 78 | + |
| 79 | + const conicColorBg = `conic-gradient(from ${fromDeg}, ${conicColors.join(', ')})`; |
| 80 | + |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <mask id={maskId}>{circleNode}</mask> |
| 84 | + |
| 85 | + <foreignObject x={0} y={0} width={size} height={size} mask={`url(#${maskId})`}> |
| 86 | + <div |
| 87 | + style={{ |
| 88 | + width: '100%', |
| 89 | + height: '100%', |
| 90 | + background: conicColorBg, |
| 91 | + }} |
| 92 | + /> |
| 93 | + </foreignObject> |
| 94 | + </> |
| 95 | + ); |
| 96 | +}); |
| 97 | + |
| 98 | +if (process.env.NODE_ENV !== 'production') { |
| 99 | + PtgCircle.displayName = 'PtgCircle'; |
| 100 | +} |
| 101 | + |
| 102 | +export default PtgCircle; |
0 commit comments