|
1 | | -import { Gesture } from "react-native-gesture-handler"; |
2 | | -import { SharedValue } from "react-native-reanimated"; |
| 1 | +import { SkPath } from "@shopify/react-native-skia"; |
| 2 | +import { |
| 3 | + Gesture, |
| 4 | + type PanGestureHandlerEventPayload, |
| 5 | + type PanGestureChangeEventPayload, |
| 6 | +} from "react-native-gesture-handler"; |
| 7 | +import { SharedValue, interpolate } from "react-native-reanimated"; |
3 | 8 |
|
4 | | -interface UseGestureProps { |
| 9 | +import { getYForX } from "./Math"; |
| 10 | + |
| 11 | +type PanGestureHandlerOnBeginEventPayload = { |
| 12 | + point: number; |
| 13 | + event: PanGestureHandlerEventPayload; |
| 14 | +}; |
| 15 | +type PanGestureHandlerOnChangeEventPayload = { |
| 16 | + point: number; |
| 17 | + event: PanGestureHandlerEventPayload & PanGestureChangeEventPayload; |
| 18 | +}; |
| 19 | + |
| 20 | +// Extract Hover Gesture onBegin args since it isn't exported by rngh |
| 21 | +type HoverGestureOnBegin = ReturnType<typeof Gesture.Hover>["onBegin"]; |
| 22 | +type HoverGestureOnBeginCallBack = Parameters<HoverGestureOnBegin>[0]; |
| 23 | +type HoverGestureHandlerOnBeginEventPayload = { |
| 24 | + point: number; |
| 25 | + event: Parameters<HoverGestureOnBeginCallBack>[0]; |
| 26 | +}; |
| 27 | + |
| 28 | +// Extract Hover Gesture onChange args since it isn't exported by rngh |
| 29 | +type HoverGestureOnChange = ReturnType<typeof Gesture.Hover>["onChange"]; |
| 30 | +type HoverGestureOnChangeCallBack = Parameters<HoverGestureOnChange>[0]; |
| 31 | +type HoverGestureHandlerOnChangeEventPayload = { |
| 32 | + point: number; |
| 33 | + event: Parameters<HoverGestureOnChangeCallBack>[0]; |
| 34 | +}; |
| 35 | + |
| 36 | +// Extract Hover Gesture onEnd args since it isn't exported by rngh |
| 37 | +type HoverGestureOnEnd = ReturnType<typeof Gesture.Hover>["onEnd"]; |
| 38 | +type HoverGestureOnEndCallBack = Parameters<HoverGestureOnEnd>[0]; |
| 39 | +type HoverGestureHandlerOnEndEventPayload = Parameters<HoverGestureOnEndCallBack>[0]; |
| 40 | + |
| 41 | +export interface UseGestureProps { |
5 | 42 | x: SharedValue<number>; |
| 43 | + y: SharedValue<number>; |
| 44 | + path: SkPath; |
| 45 | + height: number; |
| 46 | + minValue: number; |
| 47 | + maxValue: number; |
6 | 48 | cursorRadius: number; |
| 49 | + /** Callback when the pan gesture begins. This function must be a worklet function. */ |
| 50 | + onPanGestureBegin: ((payload: PanGestureHandlerOnBeginEventPayload) => void) | null; |
| 51 | + onPanGestureChange: ((payload: PanGestureHandlerOnChangeEventPayload) => void) | null; |
| 52 | + onPanGestureEnd: ((payload: PanGestureHandlerEventPayload) => void) | null; |
| 53 | + onHoverGestureBegin: ((payload: HoverGestureHandlerOnBeginEventPayload) => void) | null; |
| 54 | + onHoverGestureChange: ((payload: HoverGestureHandlerOnChangeEventPayload) => void) | null; |
| 55 | + onHoverGestureEnd: ((payload: HoverGestureHandlerOnEndEventPayload) => void) | null; |
7 | 56 | } |
8 | 57 |
|
9 | | -export const useGestures = ({ x, cursorRadius }: UseGestureProps) => { |
| 58 | +/** |
| 59 | + * Returns the gesture handlers for the LineChart component. |
| 60 | + * @param param0 - The props to allow the gesture handlers to interact with the LineChart component. |
| 61 | + * @returns The gesture handlers for the LineChart component. |
| 62 | + */ |
| 63 | +export const useGestures = ({ |
| 64 | + x, |
| 65 | + y, |
| 66 | + path, |
| 67 | + height, |
| 68 | + minValue, |
| 69 | + maxValue, |
| 70 | + cursorRadius, |
| 71 | + onPanGestureBegin, |
| 72 | + onPanGestureChange, |
| 73 | + onPanGestureEnd, |
| 74 | + onHoverGestureBegin, |
| 75 | + onHoverGestureChange, |
| 76 | + onHoverGestureEnd, |
| 77 | +}: UseGestureProps) => { |
10 | 78 | const panGesture = Gesture.Pan() |
11 | | - .onBegin((evt) => (x.value = evt.x)) |
12 | | - .onChange((evt) => (x.value = evt.x)) |
13 | | - .onEnd(() => (x.value = -cursorRadius)); |
| 79 | + .onBegin((event) => { |
| 80 | + x.value = event.x; |
| 81 | + y.value = getYForX({ path, x: event.x }); |
| 82 | + const point = interpolate( |
| 83 | + y.value, |
| 84 | + [cursorRadius, height - cursorRadius], |
| 85 | + [maxValue, minValue] |
| 86 | + ); |
| 87 | + if (onPanGestureBegin) onPanGestureBegin({ event, point }); |
| 88 | + }) |
| 89 | + .onChange((event) => { |
| 90 | + x.value = event.x; |
| 91 | + y.value = getYForX({ path, x: event.x }); |
| 92 | + const point = interpolate( |
| 93 | + y.value, |
| 94 | + [cursorRadius, height - cursorRadius], |
| 95 | + [maxValue, minValue] |
| 96 | + ); |
| 97 | + if (onPanGestureChange) onPanGestureChange({ event, point }); |
| 98 | + }) |
| 99 | + .onEnd((event) => { |
| 100 | + x.value = -cursorRadius; |
| 101 | + if (onPanGestureEnd) onPanGestureEnd(event); |
| 102 | + }); |
14 | 103 |
|
15 | 104 | const hoverGesture = Gesture.Hover() |
16 | | - .onBegin((evt) => (x.value = evt.x)) |
17 | | - .onChange((evt) => (x.value = evt.x)) |
18 | | - .onEnd(() => (x.value = -cursorRadius)); |
| 105 | + .onBegin((event) => { |
| 106 | + x.value = event.x; |
| 107 | + y.value = getYForX({ path, x: event.x }); |
| 108 | + const point = interpolate( |
| 109 | + y.value, |
| 110 | + [cursorRadius, height - cursorRadius], |
| 111 | + [maxValue, minValue] |
| 112 | + ); |
| 113 | + if (onHoverGestureBegin) onHoverGestureBegin({ event, point }); |
| 114 | + }) |
| 115 | + .onChange((event) => { |
| 116 | + x.value = event.x; |
| 117 | + y.value = getYForX({ path, x: event.x }); |
| 118 | + const point = interpolate( |
| 119 | + y.value, |
| 120 | + [cursorRadius, height - cursorRadius], |
| 121 | + [maxValue, minValue] |
| 122 | + ); |
| 123 | + if (onHoverGestureChange) onHoverGestureChange({ event, point }); |
| 124 | + }) |
| 125 | + .onEnd((event) => { |
| 126 | + x.value = -cursorRadius; |
| 127 | + if (onHoverGestureEnd) onHoverGestureEnd(event); |
| 128 | + }); |
19 | 129 |
|
| 130 | + // We return a composed gesture that listens to both pan and hover gestures. This is to |
| 131 | + // allow the chart component to work on both touch and mouse devices. |
20 | 132 | return Gesture.Race(hoverGesture, panGesture); |
21 | 133 | }; |
0 commit comments