11import * as React from 'react' ;
22import type { ProgressProps } from '..' ;
3- import type { StrokeColorType } from '../interface' ;
3+ import type { StrokeColorObject } from '../interface' ;
4+
5+ interface BlockProps {
6+ bg : string ;
7+ children ?: React . ReactNode ;
8+ }
9+
10+ const Block = ( { bg, children } : BlockProps ) => (
11+ < div
12+ style = { {
13+ width : '100%' ,
14+ height : '100%' ,
15+ background : bg ,
16+ } }
17+ >
18+ { children }
19+ </ div >
20+ ) ;
21+
22+ function getPtgColors ( color : Record < string , string > , scale : number ) {
23+ return Object . keys ( color ) . map ( ( key ) => {
24+ const parsedKey = parseFloat ( key ) ;
25+ const ptgKey = `${ Math . floor ( parsedKey * scale ) } %` ;
26+
27+ return `${ color [ key ] } ${ ptgKey } ` ;
28+ } ) ;
29+ }
430
531export interface ColorGradientProps {
632 prefixCls : string ;
@@ -11,8 +37,7 @@ export interface ColorGradientProps {
1137 strokeLinecap : ProgressProps [ 'strokeLinecap' ] ;
1238 strokeWidth : ProgressProps [ 'strokeWidth' ] ;
1339 size : number ;
14- color : StrokeColorType ;
15- conic : boolean ;
40+ color : string | StrokeColorObject ;
1641 gapDegree : number ;
1742}
1843
@@ -27,19 +52,12 @@ const PtgCircle = React.forwardRef<SVGCircleElement, ColorGradientProps>((props,
2752 strokeLinecap,
2853 strokeWidth,
2954 size,
30- conic,
3155 gapDegree,
3256 } = props ;
3357
3458 const isGradient = color && typeof color === 'object' ;
3559
36- const stroke = React . useMemo ( ( ) => {
37- if ( conic ) {
38- return '#FFF' ;
39- }
40-
41- return isGradient ? `url(#${ gradientId } )` : undefined ;
42- } , [ gradientId , isGradient , conic ] ) ;
60+ const stroke = isGradient ? `#FFF` : undefined ;
4361
4462 // ========================== Circle ==========================
4563 const halfSize = size / 2 ;
@@ -60,36 +78,30 @@ const PtgCircle = React.forwardRef<SVGCircleElement, ColorGradientProps>((props,
6078 ) ;
6179
6280 // ========================== Render ==========================
63- if ( ! conic ) {
81+ if ( ! isGradient ) {
6482 return circleNode ;
6583 }
6684
6785 const maskId = `${ gradientId } -conic` ;
68- const conicColorKeys = Object . keys ( color ) . filter ( ( key ) => key !== 'conic' ) ;
6986
7087 const fromDeg = gapDegree ? `${ 180 + gapDegree / 2 } deg` : '0deg' ;
7188
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- } ) ;
89+ const conicColors = getPtgColors ( color , ( 360 - gapDegree ) / 360 ) ;
90+ const linearColors = getPtgColors ( color , 1 ) ;
7891
7992 const conicColorBg = `conic-gradient(from ${ fromDeg } , ${ conicColors . join ( ', ' ) } )` ;
93+ const linearColorBg = `linear-gradient(to ${ gapDegree ? 'bottom' : 'top' } , ${ linearColors . join (
94+ ', ' ,
95+ ) } )`;
8096
8197 return (
8298 < >
8399 < mask id = { maskId } > { circleNode } </ mask >
84100
85101 < 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- />
102+ < Block bg = { linearColorBg } >
103+ < Block bg = { conicColorBg } />
104+ </ Block >
93105 </ foreignObject >
94106 </ >
95107 ) ;
0 commit comments