@@ -248,7 +248,8 @@ const SwipeChartComponent = (params: Props) => {
248
248
249
249
React . useEffect ( ( ) => {
250
250
if ( chartData . length > 0 ) {
251
- rCachedWidths . current = { }
251
+ rPriceCursorWidth . current = 0
252
+ rXTooltipWidth . current = 0
252
253
sMinPriceString . value = `${ fiatSymbol } ${ formatFiatString ( { fiatAmount : minPrice . toString ( ) , autoPrecision : true } ) } `
253
254
sMaxPriceString . value = `${ fiatSymbol } ${ formatFiatString ( { fiatAmount : maxPrice . toString ( ) , autoPrecision : true } ) } `
254
255
}
@@ -263,8 +264,9 @@ const SwipeChartComponent = (params: Props) => {
263
264
264
265
const rIsShowCursor = React . useRef < boolean > ( false )
265
266
const rXTooltipView = React . useRef < View > ( null )
267
+ const rXTooltipWidth = React . useRef < number > ( 0 )
266
268
const rPriceCursorView = React . useRef < View > ( null )
267
- const rCachedWidths = React . useRef < { [ target : number ] : number } > ( { } )
269
+ const rPriceCursorWidth = React . useRef < number > ( 0 )
268
270
269
271
const rMinPriceView = React . useRef < Animated . View > ( null )
270
272
const rMaxPriceView = React . useRef < Animated . View > ( null )
@@ -399,24 +401,6 @@ const SwipeChartComponent = (params: Props) => {
399
401
}
400
402
} )
401
403
402
- /**
403
- * Natively center align a component across the Y axis origin
404
- */
405
- const nativeCenterAlignLayout = ( ref : React . RefObject < View | Animated . View | undefined > , offset ?: number ) => ( layoutChangeEvent : LayoutChangeEvent ) => {
406
- if ( layoutChangeEvent != null && layoutChangeEvent . nativeEvent != null ) {
407
- const target = layoutChangeEvent . target
408
-
409
- // Store measurements and avoid over-updating if the size of the component
410
- // doesn't change significantly
411
- const currentWidth = rCachedWidths . current [ target ]
412
- const newWidth = layoutChangeEvent . nativeEvent . layout . width
413
- if ( currentWidth == null || Math . abs ( currentWidth - newWidth ) > 1 ) {
414
- rCachedWidths . current [ target ] = newWidth
415
- if ( ref . current != null ) ref . current . setNativeProps ( { left : - newWidth / 2 + ( offset ?? 0 ) } )
416
- }
417
- }
418
- }
419
-
420
404
/**
421
405
* Set the X axis position of the min/max labels. Left or right justify the
422
406
* label according to its horizontal position on the chart
@@ -432,8 +416,8 @@ const SwipeChartComponent = (params: Props) => {
432
416
}
433
417
}
434
418
435
- const handleAlignCursorLayout = useHandler ( nativeCenterAlignLayout ( rPriceCursorView , PULSE_CURSOR_RADIUS * 2 ) )
436
- const handleAlignXTooltipLayout = useHandler ( nativeCenterAlignLayout ( rXTooltipView ) )
419
+ const handleAlignCursorLayout = useHandler ( nativeCenterAlignLayout ( rPriceCursorWidth , rPriceCursorView , PULSE_CURSOR_RADIUS * 2 ) )
420
+ const handleAlignXTooltipLayout = useHandler ( nativeCenterAlignLayout ( rXTooltipWidth , rXTooltipView ) )
437
421
438
422
const handleAlignMinPriceLabelLayout = useHandler ( setMinMaxLabelsX ( sMinPriceLabelX , minPriceDataPoint ) )
439
423
const handleAlignMaxPriceLabelLayout = useHandler ( setMinMaxLabelsX ( sMaxPriceLabelX , maxPriceDataPoint ) )
@@ -669,4 +653,21 @@ const getStyles = cacheStyles((theme: Theme) => {
669
653
}
670
654
} )
671
655
656
+ /**
657
+ * Natively center align a component across the Y axis origin
658
+ */
659
+ const nativeCenterAlignLayout =
660
+ ( widthRef : React . MutableRefObject < number > , ref : React . RefObject < View | Animated . View | undefined > , offset ?: number ) =>
661
+ ( layoutChangeEvent : LayoutChangeEvent ) => {
662
+ if ( layoutChangeEvent != null && layoutChangeEvent . nativeEvent != null ) {
663
+ // Store measurements and avoid over-updating if the size of the component
664
+ // doesn't change significantly
665
+ const newWidth = layoutChangeEvent . nativeEvent . layout . width
666
+ if ( Math . abs ( widthRef . current - newWidth ) > 1 ) {
667
+ widthRef . current = newWidth
668
+ if ( ref . current != null ) ref . current . setNativeProps ( { left : - newWidth / 2 + ( offset ?? 0 ) } )
669
+ }
670
+ }
671
+ }
672
+
672
673
export const SwipeChart = React . memo ( SwipeChartComponent )
0 commit comments