@@ -31,17 +31,32 @@ const ColorBarSection: React.FC<ColorBarSectionProps> = (props) => {
31
31
} ) ;
32
32
33
33
const percentage = useMemo ( ( ) => {
34
+ if ( total . isZero ( ) ) {
35
+ return 0 ;
36
+ }
34
37
return `${ data . value . div ( total ) . shiftedBy ( 2 ) . toFixed ( 1 ) } %` ;
35
38
} , [ data , total ] ) ;
36
39
37
40
const handlePercentageDisplay = useCallback ( ( ) => {
38
41
if ( percentageEl ) {
39
42
const parentEle = percentageEl . parentElement ;
43
+
44
+ if (
45
+ parentEle &&
46
+ parentEle . offsetWidth === 0 &&
47
+ percentageEl . offsetWidth !== 0
48
+ ) {
49
+ // Component is still being initialized
50
+ return ;
51
+ }
52
+
40
53
if (
41
54
! showPercentage ||
42
55
( parentEle && parentEle . offsetWidth < percentageEl . offsetWidth )
43
56
) {
44
57
percentageEl . hidden = true ;
58
+ } else {
59
+ percentageEl . hidden = false ;
45
60
}
46
61
}
47
62
} , [ percentageEl , showPercentage ] ) ;
@@ -108,34 +123,52 @@ const ColorBarSection: React.FC<ColorBarSectionProps> = (props) => {
108
123
interface ColorBarProps {
109
124
className ?: string ;
110
125
showPercentage ?: boolean ;
126
+ total ?: BigNumber ;
111
127
data : ColorBarData [ ] ;
112
128
}
113
129
114
130
const ColorBar : React . FC < ColorBarProps > = ( props ) => {
115
- const { className, data, showPercentage } = props ;
131
+ const { className, data, showPercentage, total } = props ;
116
132
117
- const total = useMemo (
133
+ const calculatedTotal = useMemo (
118
134
( ) =>
135
+ total ??
119
136
data . reduce ( ( total , data ) => total . plus ( data . value ) , new BigNumber ( 0 ) ) ,
120
- [ data ]
137
+ [ data , total ]
121
138
) ;
122
139
140
+ // Using grid to overlap the bars without using relative which breaks the tooltips
123
141
return (
124
- < div className = { cn ( "flex" , "h-full" , "overflow-hidden" , className ) } >
125
- { ! total . eq ( 0 ) ? (
126
- data . map ( ( data , index ) => (
142
+ < div
143
+ className = { cn (
144
+ "grid" ,
145
+ "grid-rows-1" ,
146
+ "grid-cols-1" ,
147
+ "h-full" ,
148
+ "overflow-hidden" ,
149
+ className
150
+ ) }
151
+ >
152
+ < div
153
+ className = { cn (
154
+ "row-start-1" ,
155
+ "col-start-1" ,
156
+ "block" ,
157
+ "h-full" ,
158
+ "w-full" ,
159
+ "bg-likecoin-grey"
160
+ ) }
161
+ />
162
+ < div className = { cn ( "row-start-1" , "col-start-1" , "flex" ) } >
163
+ { data . map ( ( data , index ) => (
127
164
< ColorBarSection
128
165
key = { index }
129
166
data = { data }
130
- total = { total }
167
+ total = { calculatedTotal }
131
168
showPercentage = { showPercentage }
132
169
/>
133
- ) )
134
- ) : (
135
- < div
136
- className = { cn ( "inline-block" , "h-full" , "w-full" , "bg-likecoin-grey" ) }
137
- />
138
- ) }
170
+ ) ) }
171
+ </ div >
139
172
</ div >
140
173
) ;
141
174
} ;
0 commit comments