11import type { ComponentProps , FC } from "react" ;
2- import { useId } from "react" ;
2+ import { useId , useMemo } from "react" ;
33import { twMerge } from "tailwind-merge" ;
44import { mergeDeep } from "../../helpers/merge-deep" ;
55import { getTheme } from "../../theme-store" ;
@@ -33,11 +33,12 @@ export interface CircularProgressProps extends ComponentProps<"div"> {
3333 progress : number ;
3434 textLabel ?: string ;
3535 theme ?: DeepPartial < FlowbiteCircularProgressTheme > ;
36+ progressColor ?: keyof CircularProgressColor ;
3637}
3738
3839export const CircularProgress : FC < CircularProgressProps > = ( {
3940 className,
40- color = "cyan" ,
41+ progressColor = "cyan" ,
4142 labelText = false ,
4243 progress,
4344 textLabel = "65%" ,
@@ -47,11 +48,14 @@ export const CircularProgress: FC<CircularProgressProps> = ({
4748 const id = useId ( ) ;
4849 const theme = mergeDeep ( getTheme ( ) . progress . circular , customTheme ) ;
4950
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
5254
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 ] ) ;
5559
5660 return (
5761 < div id = { id } aria-valuenow = { progress } role = "progressbar" { ...props } >
@@ -64,7 +68,7 @@ export const CircularProgress: FC<CircularProgressProps> = ({
6468 cy = "18"
6569 r = "16"
6670 fill = "none"
67- className = { theme . color . barColor [ color ] }
71+ className = { theme . color . barColor [ progressColor ] }
6872 strokeWidth = "2"
6973 strokeDasharray = "100"
7074 strokeDashoffset = { offset }
@@ -76,7 +80,7 @@ export const CircularProgress: FC<CircularProgressProps> = ({
7680 < div className = { theme . label . base } >
7781 < span
7882 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 ] ) }
8084 >
8185 { textLabel }
8286 </ span >
0 commit comments