Skip to content

Commit 0865ccc

Browse files
committed
optimized the CircularProgress Bar Code
1 parent 51edf97 commit 0865ccc

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

packages/ui/src/components/Progress/ProgressCircular.tsx

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ComponentProps, FC } from "react";
2-
import { useId } from "react";
2+
import { useId, useMemo } from "react";
33
import { twMerge } from "tailwind-merge";
44
import { mergeDeep } from "../../helpers/merge-deep";
55
import { 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

3839
export 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

Comments
 (0)