Skip to content

Commit f7245c8

Browse files
committed
Remove width hack from the price chart
1 parent fde5591 commit f7245c8

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/components/charts/SwipeChart.tsx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ const SwipeChartComponent = (params: Props) => {
248248

249249
React.useEffect(() => {
250250
if (chartData.length > 0) {
251-
rCachedWidths.current = {}
251+
rPriceCursorWidth.current = 0
252+
rXTooltipWidth.current = 0
252253
sMinPriceString.value = `${fiatSymbol}${formatFiatString({ fiatAmount: minPrice.toString(), autoPrecision: true })}`
253254
sMaxPriceString.value = `${fiatSymbol}${formatFiatString({ fiatAmount: maxPrice.toString(), autoPrecision: true })}`
254255
}
@@ -263,8 +264,9 @@ const SwipeChartComponent = (params: Props) => {
263264

264265
const rIsShowCursor = React.useRef<boolean>(false)
265266
const rXTooltipView = React.useRef<View>(null)
267+
const rXTooltipWidth = React.useRef<number>(0)
266268
const rPriceCursorView = React.useRef<View>(null)
267-
const rCachedWidths = React.useRef<{ [target: number]: number }>({})
269+
const rPriceCursorWidth = React.useRef<number>(0)
268270

269271
const rMinPriceView = React.useRef<Animated.View>(null)
270272
const rMaxPriceView = React.useRef<Animated.View>(null)
@@ -399,24 +401,6 @@ const SwipeChartComponent = (params: Props) => {
399401
}
400402
})
401403

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-
420404
/**
421405
* Set the X axis position of the min/max labels. Left or right justify the
422406
* label according to its horizontal position on the chart
@@ -432,8 +416,8 @@ const SwipeChartComponent = (params: Props) => {
432416
}
433417
}
434418

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))
437421

438422
const handleAlignMinPriceLabelLayout = useHandler(setMinMaxLabelsX(sMinPriceLabelX, minPriceDataPoint))
439423
const handleAlignMaxPriceLabelLayout = useHandler(setMinMaxLabelsX(sMaxPriceLabelX, maxPriceDataPoint))
@@ -669,4 +653,21 @@ const getStyles = cacheStyles((theme: Theme) => {
669653
}
670654
})
671655

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+
672673
export const SwipeChart = React.memo(SwipeChartComponent)

0 commit comments

Comments
 (0)