1
1
import type { ComponentProps , FC } from "react" ;
2
- import { useId } from "react" ;
2
+ import { useId , useMemo } from "react" ;
3
3
import { twMerge } from "tailwind-merge" ;
4
4
import { mergeDeep } from "../../helpers/merge-deep" ;
5
5
import { getTheme } from "../../theme-store" ;
@@ -33,11 +33,12 @@ export interface CircularProgressProps extends ComponentProps<"div"> {
33
33
progress : number ;
34
34
textLabel ?: string ;
35
35
theme ?: DeepPartial < FlowbiteCircularProgressTheme > ;
36
+ progressColor ?: keyof CircularProgressColor ;
36
37
}
37
38
38
39
export const CircularProgress : FC < CircularProgressProps > = ( {
39
40
className,
40
- color = "cyan" ,
41
+ progressColor = "cyan" ,
41
42
labelText = false ,
42
43
progress,
43
44
textLabel = "65%" ,
@@ -47,11 +48,14 @@ export const CircularProgress: FC<CircularProgressProps> = ({
47
48
const id = useId ( ) ;
48
49
const theme = mergeDeep ( getTheme ( ) . progress . circular , customTheme ) ;
49
50
50
- // Calculate the circumference of the Circle
51
- const circumference = 2 * Math . PI * 16 ;
51
+ // Memoize calculations for the circumference and stroke offset to avoid recalculating on each render
52
+ const { offset } = useMemo ( ( ) => {
53
+ const circumference = 2 * Math . PI * 16 ; // Fixed radius of 16
52
54
53
- // Calculate the stroke-dashoffset
54
- const offset = circumference * ( 1 - progress / 100 ) ;
55
+ const offset = circumference * ( 1 - progress / 100 ) ; // Stroke dash offset based on progress
56
+
57
+ return { offset } ;
58
+ } , [ progress ] ) ;
55
59
56
60
return (
57
61
< div id = { id } aria-valuenow = { progress } role = "progressbar" { ...props } >
@@ -64,7 +68,7 @@ export const CircularProgress: FC<CircularProgressProps> = ({
64
68
cy = "18"
65
69
r = "16"
66
70
fill = "none"
67
- className = { theme . color . barColor [ color ] }
71
+ className = { theme . color . barColor [ progressColor ] }
68
72
strokeWidth = "2"
69
73
strokeDasharray = "100"
70
74
strokeDashoffset = { offset }
@@ -76,7 +80,7 @@ export const CircularProgress: FC<CircularProgressProps> = ({
76
80
< div className = { theme . label . base } >
77
81
< span
78
82
data-testid = "flowbite-circular-progress-label"
79
- className = { twMerge ( theme . label . text , theme . label . textColor [ color ] ) }
83
+ className = { twMerge ( theme . label . text , theme . label . textColor [ progressColor ] ) }
80
84
>
81
85
{ textLabel }
82
86
</ span >
0 commit comments