1
- import React from "react" ;
1
+ import React , { type ReactNode , useMemo } from "react" ;
2
2
3
3
interface StakingCapBarProps {
4
4
totalLength ?: number ;
@@ -19,76 +19,64 @@ export default function StakingCapBar({
19
19
secondFillLabel,
20
20
totalLabel,
21
21
} : 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 (
26
24
100 - clampedFillPercentage ,
27
25
Math . max ( 0 , secondFillPercentage )
28
- ) ;
26
+ ) , [ clampedFillPercentage , secondFillPercentage ] ) ;
29
27
const totalFillPercentage =
30
- clampedFillPercentage + clampedSecondFillPercentage ;
28
+ useMemo ( ( ) => clampedFillPercentage + clampedSecondFillPercentage , [ clampedFillPercentage , clampedSecondFillPercentage ] ) ;
31
29
32
30
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 > }
86
50
</ 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
- ) }
92
51
</ div >
93
52
) ;
94
53
}
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