Skip to content

Commit 146f2df

Browse files
committed
Fix staking cap bar issues
1 parent 5f40c42 commit 146f2df

File tree

1 file changed

+53
-65
lines changed

1 file changed

+53
-65
lines changed

components/StakingCapBar.tsx

Lines changed: 53 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { type ReactNode, useMemo } from "react";
22

33
interface StakingCapBarProps {
44
totalLength?: number;
@@ -19,76 +19,64 @@ export default function StakingCapBar({
1919
secondFillLabel,
2020
totalLabel,
2121
}: StakingCapBarProps) {
22-
const borderWidth = 4;
23-
const gapWidth = 2;
24-
const clampedFillPercentage = Math.min(100, Math.max(0, fillPercentage));
25-
const clampedSecondFillPercentage = Math.min(
22+
const clampedFillPercentage = useMemo(() => Math.min(100, Math.max(0, fillPercentage)), [fillPercentage]);
23+
const clampedSecondFillPercentage = useMemo(() => Math.min(
2624
100 - clampedFillPercentage,
2725
Math.max(0, secondFillPercentage)
28-
);
26+
), [clampedFillPercentage, secondFillPercentage]);
2927
const totalFillPercentage =
30-
clampedFillPercentage + clampedSecondFillPercentage;
28+
useMemo(() => clampedFillPercentage + clampedSecondFillPercentage, [clampedFillPercentage, clampedSecondFillPercentage]);
3129

3230
return (
33-
<div className="flex flex-col items-start">
34-
<div
35-
className="rounded-lg overflow-hidden relative"
36-
style={{
37-
width: `${totalLength}px`,
38-
height: `${height}px`,
39-
border: `${borderWidth}px solid #7142CF`,
40-
backgroundColor: "#FFFFFF",
41-
padding: `${gapWidth}px`,
42-
}}
43-
role="progressbar"
44-
aria-valuenow={totalFillPercentage}
45-
aria-valuemin={0}
46-
aria-valuemax={100}
47-
>
48-
<div className="flex h-full">
49-
<div
50-
className="h-full transition-all duration-300 ease-in-out relative"
51-
style={{
52-
width: `${clampedFillPercentage}%`,
53-
backgroundColor: "#5c48e4",
54-
}}
55-
>
56-
{firstFillLabel && (
57-
<div
58-
className="absolute top-full left-full transform -translate-x-1/2 mt-2 text-xs font-medium text-gray-700 whitespace-nowrap"
59-
style={{
60-
transition: "left 300ms ease-in-out",
61-
}}
62-
>
63-
{firstFillLabel}
64-
</div>
65-
)}
66-
</div>
67-
<div
68-
className="h-full transition-all duration-300 ease-in-out relative"
69-
style={{
70-
width: `${clampedSecondFillPercentage}%`,
71-
backgroundColor: "#f0b6bb",
72-
}}
73-
>
74-
{secondFillLabel && (
75-
<div
76-
className="absolute top-full left-full transform -translate-x-1/2 mt-2 text-xs font-medium text-gray-700 whitespace-nowrap"
77-
style={{
78-
transition: "left 300ms ease-in-out",
79-
}}
80-
>
81-
{secondFillLabel}
82-
</div>
83-
)}
84-
</div>
85-
</div>
31+
<div
32+
className="rounded-lg relative border border-4 border-[#7142CF] p-1 bg-white whitespace-nowrap"
33+
style={{
34+
width: `${totalLength}px`,
35+
height: `${height}px`,
36+
}}
37+
role="progressbar"
38+
aria-valuenow={totalFillPercentage}
39+
aria-valuemin={0}
40+
aria-valuemax={100}
41+
>
42+
<Bar color="#5c48e4" fillPercent={clampedFillPercentage}>
43+
{firstFillLabel}
44+
</Bar>
45+
<Bar color="#f0b6bb" fillPercent={clampedSecondFillPercentage}>
46+
{secondFillLabel}
47+
</Bar>
48+
<div className="absolute right-0 bottom-full mb-[1.65rem]">
49+
{totalLabel && <Label>{totalLabel}</Label>}
8650
</div>
87-
{totalLabel && (
88-
<div className="absolute top-full right-0 transform translate-x-1/2 mt-2 text-xs font-medium text-gray-700 whitespace-nowrap">
89-
{totalLabel}
90-
</div>
91-
)}
9251
</div>
9352
);
9453
}
54+
55+
type BarProps = {
56+
fillPercent: number;
57+
children?: ReactNode | undefined;
58+
color: string;
59+
}
60+
61+
const Bar = ({ fillPercent, children, color }: BarProps) => (
62+
<div
63+
className="inline-block h-full transition-all duration-300 ease-in-out relative"
64+
style={{
65+
backgroundColor: color,
66+
width: `${fillPercent}%`,
67+
}}
68+
>
69+
{children && <Label>{children}</Label>}
70+
</div>
71+
);
72+
73+
const Label = ({ children }: { children: ReactNode }) => (
74+
<div
75+
className="absolute top-full left-full transform -translate-x-1/2 mt-2 text-xs font-medium text-gray-800 dark:text-gray-200 whitespace-nowrap"
76+
style={{
77+
transition: "left 300ms ease-in-out",
78+
}}
79+
>
80+
{children}
81+
</div>
82+
);

0 commit comments

Comments
 (0)