Skip to content

Commit 15f7e25

Browse files
committed
chore: move gestures to separate file
1 parent 43fe090 commit 15f7e25

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

example/app/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
66
import { Banner } from "../components/Banner";
77

88
const generateRandomData = (): [number, number][] => {
9-
return Array.from({ length: 100 }, (_, i) => [i, Math.random() * 2000]);
9+
return Array.from({ length: 30 }, (_, i) => [i, Math.random() * 2000]);
1010
};
1111

1212
const formatter = new Intl.NumberFormat("en-US", {

src/components/LineChart/index.tsx

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Canvas, Path } from "@shopify/react-native-skia";
22
import { useCallback, useMemo, useState } from "react";
33
import { LayoutChangeEvent, StyleSheet, View, ViewProps } from "react-native";
4-
import { Gesture, GestureDetector } from "react-native-gesture-handler";
4+
import { GestureDetector } from "react-native-gesture-handler";
55
import { useDerivedValue, useSharedValue } from "react-native-reanimated";
66

77
import { 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

1819
export 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" />
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Gesture } from "react-native-gesture-handler";
2+
import { SharedValue } from "react-native-reanimated";
3+
4+
interface UseGestureProps {
5+
x: SharedValue<number>;
6+
cursorRadius: number;
7+
}
8+
9+
export const useGestures = ({ x, cursorRadius }: UseGestureProps) => {
10+
const panGesture = Gesture.Pan()
11+
.onBegin((evt) => (x.value = evt.x))
12+
.onChange((evt) => (x.value = evt.x))
13+
.onEnd(() => (x.value = -cursorRadius));
14+
15+
const hoverGesture = Gesture.Hover()
16+
.onBegin((evt) => (x.value = evt.x))
17+
.onChange((evt) => (x.value = evt.x))
18+
.onEnd(() => (x.value = -cursorRadius));
19+
20+
return Gesture.Race(hoverGesture, panGesture);
21+
};

0 commit comments

Comments
 (0)