import React from 'react'; import { Platform, View, ViewProps, StyleSheet } from 'react-native'; import Animated, { useAnimatedStyle, withSpring, AnimatedProps, } from 'react-native-reanimated'; import { LineChartCursor, LineChartCursorProps } from './Cursor'; import { useLineChart } from './useLineChart'; type LineChartCursorCrosshairProps = Omit< LineChartCursorProps, 'children' | 'type' > & { children?: React.ReactNode; color?: string; size?: number; outerSize?: number; crosshairWrapperProps?: AnimatedProps; crosshairProps?: ViewProps; crosshairOuterProps?: ViewProps; }; LineChartCursorCrosshair.displayName = 'LineChartCursorCrosshair'; export function LineChartCursorCrosshair({ children, color = 'black', size = 8, outerSize = 32, crosshairWrapperProps = {}, crosshairProps = {}, crosshairOuterProps = {}, ...props }: LineChartCursorCrosshairProps) { const { currentX, currentY, isActive } = useLineChart(); // It seems that enabling spring animation on initial render on Android causes a crash. const [enableSpringAnimation, setEnableSpringAnimation] = React.useState( Platform.OS === 'ios' ); React.useEffect(() => { setTimeout(() => { setEnableSpringAnimation(true); }, 100); }, []); const animatedCursorStyle = useAnimatedStyle( () => ({ transform: [ { translateX: currentX.value - outerSize / 2 }, { translateY: currentY.value - outerSize / 2 }, { scale: enableSpringAnimation ? withSpring(isActive.value ? 1 : 0, { damping: 10, }) : 0, }, ], }) ); return ( {children} ); } const styles = StyleSheet.create({ crosshairWrapper: { alignItems: 'center', justifyContent: 'center', }, crosshairOuter: { opacity: 0.1, position: 'absolute', }, });