11import { Canvas , Path } from "@shopify/react-native-skia" ;
22import { useCallback , useMemo , useState } from "react" ;
33import { LayoutChangeEvent , StyleSheet , View , ViewProps } from "react-native" ;
4- import { Gesture , GestureDetector } from "react-native-gesture-handler" ;
4+ import { GestureDetector } from "react-native-gesture-handler" ;
55import { useDerivedValue , useSharedValue } from "react-native-reanimated" ;
66
77import { AxisLabelComponentProps , AxisLabelContainer } from "./AxisLabel" ;
@@ -14,6 +14,7 @@ import {
1414 DEFAULT_FORMATTER ,
1515 DEFAULT_STROKE_WIDTH ,
1616} from "./constants" ;
17+ import { useGestures } from "./useGestures" ;
1718
1819export type LineChartProps = ViewProps & {
1920 /** Array of [x, y] points for the chart */
@@ -44,20 +45,7 @@ export const LineChart: React.FC<LineChartProps> = ({
4445
4546 // Initially -cursorRadius so that the cursor is offscreen
4647 const x = useSharedValue ( - cursorRadius ) ;
47- const panGesture = Gesture . Pan ( )
48- // Follow the cursor on the x-axis
49- . onBegin ( ( evt ) => ( x . value = evt . x ) )
50- . onChange ( ( evt ) => ( x . value = evt . x ) )
51- // When the gesture ends, we reset the x value to -cursorRadius so that the cursor is offscreen
52- . onEnd ( ( ) => ( x . value = - cursorRadius ) ) ;
53- const hoverGesture = Gesture . Hover ( )
54- // Follow the cursor on the x-axis
55- . onBegin ( ( evt ) => ( x . value = evt . x ) )
56- . onChange ( ( evt ) => ( x . value = evt . x ) )
57- // When the gesture ends, we reset the x value to -cursorRadius so that the cursor is offscreen
58- . onEnd ( ( ) => ( x . value = - cursorRadius ) ) ;
59-
60- const gesture = Gesture . Race ( hoverGesture , panGesture ) ;
48+ const gestures = useGestures ( { x, cursorRadius } ) ;
6149
6250 // We separate the computation of the data from the rendering. This is so that these values are
6351 // not recomputed when the width or height of the chart changes, but only when the points change.
@@ -83,7 +71,7 @@ export const LineChart: React.FC<LineChartProps> = ({
8371 < TopAxisLabel value = { data . maxValue } />
8472 </ AxisLabelContainer >
8573 ) }
86- < GestureDetector gesture = { gesture } >
74+ < GestureDetector gesture = { gestures } >
8775 < View style = { styles . container } onLayout = { onLayout } >
8876 < Canvas style = { { height, width } } >
8977 < Path style = "stroke" path = { path } strokeWidth = { strokeWidth } color = "black" />
0 commit comments