-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Circle support conic color (#253)
* docs: update demo * chore: basic color * feat: support conic * feat: support conic * test: update snapshot
- Loading branch information
Showing
11 changed files
with
466 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as React from 'react'; | ||
|
||
function stripPercentToNumber(percent: string) { | ||
return +percent.replace('%', ''); | ||
} | ||
|
||
export interface ColorGradientProps { | ||
gradientId: string; | ||
gradient?: Record<string, string>; | ||
} | ||
|
||
export default function ColorGradient(props: ColorGradientProps) { | ||
const { gradientId, gradient } = props; | ||
|
||
return ( | ||
<defs> | ||
<linearGradient id={gradientId} x1="100%" y1="0%" x2="0%" y2="0%"> | ||
{Object.keys(gradient) | ||
.sort((a, b) => stripPercentToNumber(a) - stripPercentToNumber(b)) | ||
.map((key, index) => ( | ||
<stop key={index} offset={key} stopColor={gradient[key]} /> | ||
))} | ||
</linearGradient> | ||
</defs> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import * as React from 'react'; | ||
import type { ProgressProps } from '..'; | ||
import type { StrokeColorType } from '../interface'; | ||
|
||
export interface ColorGradientProps { | ||
prefixCls: string; | ||
gradientId: string; | ||
style: React.CSSProperties; | ||
ptg: number; | ||
radius: number; | ||
strokeLinecap: ProgressProps['strokeLinecap']; | ||
strokeWidth: ProgressProps['strokeWidth']; | ||
size: number; | ||
color: StrokeColorType; | ||
conic: boolean; | ||
gapDegree: number; | ||
} | ||
|
||
const PtgCircle = React.forwardRef<SVGCircleElement, ColorGradientProps>((props, ref) => { | ||
const { | ||
prefixCls, | ||
color, | ||
gradientId, | ||
radius, | ||
style: circleStyleForStack, | ||
ptg, | ||
strokeLinecap, | ||
strokeWidth, | ||
size, | ||
conic, | ||
gapDegree, | ||
} = props; | ||
|
||
const isGradient = color && typeof color === 'object'; | ||
|
||
const stroke = React.useMemo(() => { | ||
if (conic) { | ||
return '#FFF'; | ||
} | ||
|
||
return isGradient ? `url(#${gradientId})` : undefined; | ||
}, [gradientId, isGradient, conic]); | ||
|
||
// ========================== Circle ========================== | ||
const halfSize = size / 2; | ||
|
||
const circleNode = ( | ||
<circle | ||
className={`${prefixCls}-circle-path`} | ||
r={radius} | ||
cx={halfSize} | ||
cy={halfSize} | ||
stroke={stroke} | ||
strokeLinecap={strokeLinecap} | ||
strokeWidth={strokeWidth} | ||
opacity={ptg === 0 ? 0 : 1} | ||
style={circleStyleForStack} | ||
ref={ref} | ||
/> | ||
); | ||
|
||
// ========================== Render ========================== | ||
if (!conic) { | ||
return circleNode; | ||
} | ||
|
||
const maskId = `${gradientId}-conic`; | ||
const conicColorKeys = Object.keys(color).filter((key) => key !== 'conic'); | ||
|
||
const fromDeg = gapDegree ? `${180 + gapDegree / 2}deg` : '0deg'; | ||
|
||
const conicColors = conicColorKeys.map((key) => { | ||
const parsedKey = parseFloat(key); | ||
const ptgKey = `${gapDegree ? Math.floor((parsedKey * (360 - gapDegree)) / 360) : parsedKey}%`; | ||
|
||
return `${color[key]} ${ptgKey}`; | ||
}); | ||
|
||
const conicColorBg = `conic-gradient(from ${fromDeg}, ${conicColors.join(', ')})`; | ||
|
||
return ( | ||
<> | ||
<mask id={maskId}>{circleNode}</mask> | ||
|
||
<foreignObject x={0} y={0} width={size} height={size} mask={`url(#${maskId})`}> | ||
<div | ||
style={{ | ||
width: '100%', | ||
height: '100%', | ||
background: conicColorBg, | ||
}} | ||
/> | ||
</foreignObject> | ||
</> | ||
); | ||
}); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
PtgCircle.displayName = 'PtgCircle'; | ||
} | ||
|
||
export default PtgCircle; |
Oops, something went wrong.