import React from 'react'; import type { LayoutChangeEvent, StyleProp, TextStyle, ViewProps, } from 'react-native'; import { StyleSheet } from 'react-native'; import Animated, { useAnimatedStyle, useDerivedValue, useSharedValue, } from 'react-native-reanimated'; import type { SharedValue, AnimatedStyle } from 'react-native-reanimated'; import { CandlestickChartDimensionsContext } from './Chart'; import { useCandlestickChart } from './useCandlestickChart'; import { CandlestickChartPriceText, CandlestickChartPriceTextProps, } from './PriceText'; export type CandlestickChartCrosshairTooltipProps = ViewProps & { children?: React.ReactNode; xGutter?: number; yGutter?: number; tooltipTextProps?: CandlestickChartPriceTextProps; textStyle?: AnimatedStyle>; }; export type CandlestickChartCrosshairTooltipContext = { position: SharedValue<'left' | 'right'>; }; export const CandlestickChartCrosshairTooltipContext = React.createContext({ position: { value: 'left', } as CandlestickChartCrosshairTooltipContext['position'], }); export function CandlestickChartCrosshairTooltip({ children, xGutter = 8, yGutter = 8, tooltipTextProps, textStyle, ...props }: CandlestickChartCrosshairTooltipProps) { const { width, height } = React.useContext(CandlestickChartDimensionsContext); const { currentY } = useCandlestickChart(); const { position } = React.useContext( CandlestickChartCrosshairTooltipContext ); const elementHeight = useSharedValue(0); const elementWidth = useSharedValue(0); const handleLayout = React.useCallback( (event: LayoutChangeEvent) => { elementHeight.value = event.nativeEvent.layout.height; elementWidth.value = event.nativeEvent.layout.width; }, [elementHeight, elementWidth] ); const topOffset = useDerivedValue(() => { let offset = 0; if (currentY.value < elementHeight.value / 2 + yGutter) { offset = currentY.value - (elementHeight.value / 2 + yGutter); } else if (currentY.value + elementHeight.value / 2 > height - yGutter) { offset = currentY.value + elementHeight.value / 2 - height + yGutter; } return offset; }); const tooltip = useAnimatedStyle( () => ({ backgroundColor: 'white', position: 'absolute', display: 'flex', padding: 4, }) ); const leftTooltip = useAnimatedStyle( () => ({ left: xGutter, top: -(elementHeight.value / 2) - topOffset.value, opacity: position.value === 'left' ? 1 : 0, }) ); const rightTooltip = useAnimatedStyle( () => ({ left: width - elementWidth.value - xGutter, top: -(elementHeight.value / 2) - topOffset.value, opacity: position.value === 'right' ? 1 : 0, }) ); return ( <> {children || ( )} {children || ( )} ); } const styles = StyleSheet.create({ text: { fontSize: 14, }, });