-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pan and hover gesture handlers, with point resolution
- Loading branch information
Showing
6 changed files
with
185 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { BannerComponentProps } from "@codeherence/react-native-graph"; | ||
import { Text, useFont } from "@shopify/react-native-skia"; | ||
// import { BannerComponentProps } from "@codeherence/react-native-graph"; | ||
// import { Text, useFont } from "@shopify/react-native-skia"; | ||
|
||
const robotoMedium = require("../public/fonts/Roboto/Roboto-Medium.ttf"); | ||
// const robotoMedium = require("../public/fonts/Roboto/Roboto-Medium.ttf"); | ||
|
||
export const Banner: React.FC<BannerComponentProps> = ({ text }) => { | ||
const font = useFont(robotoMedium, 24); | ||
return <Text x={0} y={0} font={font} text={text} />; | ||
}; | ||
// export const Banner: React.FC<BannerComponentProps> = ({ text }) => { | ||
// const font = useFont(robotoMedium, 24); | ||
// return <Text x={0} y={0} font={font} text={text} />; | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,133 @@ | ||
import { Gesture } from "react-native-gesture-handler"; | ||
import { SharedValue } from "react-native-reanimated"; | ||
import { SkPath } from "@shopify/react-native-skia"; | ||
import { | ||
Gesture, | ||
type PanGestureHandlerEventPayload, | ||
type PanGestureChangeEventPayload, | ||
} from "react-native-gesture-handler"; | ||
import { SharedValue, interpolate } from "react-native-reanimated"; | ||
|
||
interface UseGestureProps { | ||
import { getYForX } from "./Math"; | ||
|
||
type PanGestureHandlerOnBeginEventPayload = { | ||
point: number; | ||
event: PanGestureHandlerEventPayload; | ||
}; | ||
type PanGestureHandlerOnChangeEventPayload = { | ||
point: number; | ||
event: PanGestureHandlerEventPayload & PanGestureChangeEventPayload; | ||
}; | ||
|
||
// Extract Hover Gesture onBegin args since it isn't exported by rngh | ||
type HoverGestureOnBegin = ReturnType<typeof Gesture.Hover>["onBegin"]; | ||
type HoverGestureOnBeginCallBack = Parameters<HoverGestureOnBegin>[0]; | ||
type HoverGestureHandlerOnBeginEventPayload = { | ||
point: number; | ||
event: Parameters<HoverGestureOnBeginCallBack>[0]; | ||
}; | ||
|
||
// Extract Hover Gesture onChange args since it isn't exported by rngh | ||
type HoverGestureOnChange = ReturnType<typeof Gesture.Hover>["onChange"]; | ||
type HoverGestureOnChangeCallBack = Parameters<HoverGestureOnChange>[0]; | ||
type HoverGestureHandlerOnChangeEventPayload = { | ||
point: number; | ||
event: Parameters<HoverGestureOnChangeCallBack>[0]; | ||
}; | ||
|
||
// Extract Hover Gesture onEnd args since it isn't exported by rngh | ||
type HoverGestureOnEnd = ReturnType<typeof Gesture.Hover>["onEnd"]; | ||
type HoverGestureOnEndCallBack = Parameters<HoverGestureOnEnd>[0]; | ||
type HoverGestureHandlerOnEndEventPayload = Parameters<HoverGestureOnEndCallBack>[0]; | ||
|
||
export interface UseGestureProps { | ||
x: SharedValue<number>; | ||
y: SharedValue<number>; | ||
path: SkPath; | ||
height: number; | ||
minValue: number; | ||
maxValue: number; | ||
cursorRadius: number; | ||
/** Callback when the pan gesture begins. This function must be a worklet function. */ | ||
onPanGestureBegin: ((payload: PanGestureHandlerOnBeginEventPayload) => void) | null; | ||
onPanGestureChange: ((payload: PanGestureHandlerOnChangeEventPayload) => void) | null; | ||
onPanGestureEnd: ((payload: PanGestureHandlerEventPayload) => void) | null; | ||
onHoverGestureBegin: ((payload: HoverGestureHandlerOnBeginEventPayload) => void) | null; | ||
onHoverGestureChange: ((payload: HoverGestureHandlerOnChangeEventPayload) => void) | null; | ||
onHoverGestureEnd: ((payload: HoverGestureHandlerOnEndEventPayload) => void) | null; | ||
} | ||
|
||
export const useGestures = ({ x, cursorRadius }: UseGestureProps) => { | ||
/** | ||
* Returns the gesture handlers for the LineChart component. | ||
* @param param0 - The props to allow the gesture handlers to interact with the LineChart component. | ||
* @returns The gesture handlers for the LineChart component. | ||
*/ | ||
export const useGestures = ({ | ||
x, | ||
y, | ||
path, | ||
height, | ||
minValue, | ||
maxValue, | ||
cursorRadius, | ||
onPanGestureBegin, | ||
onPanGestureChange, | ||
onPanGestureEnd, | ||
onHoverGestureBegin, | ||
onHoverGestureChange, | ||
onHoverGestureEnd, | ||
}: UseGestureProps) => { | ||
const panGesture = Gesture.Pan() | ||
.onBegin((evt) => (x.value = evt.x)) | ||
.onChange((evt) => (x.value = evt.x)) | ||
.onEnd(() => (x.value = -cursorRadius)); | ||
.onBegin((event) => { | ||
x.value = event.x; | ||
y.value = getYForX({ path, x: event.x }); | ||
const point = interpolate( | ||
y.value, | ||
[cursorRadius, height - cursorRadius], | ||
[maxValue, minValue] | ||
); | ||
if (onPanGestureBegin) onPanGestureBegin({ event, point }); | ||
}) | ||
.onChange((event) => { | ||
x.value = event.x; | ||
y.value = getYForX({ path, x: event.x }); | ||
const point = interpolate( | ||
y.value, | ||
[cursorRadius, height - cursorRadius], | ||
[maxValue, minValue] | ||
); | ||
if (onPanGestureChange) onPanGestureChange({ event, point }); | ||
}) | ||
.onEnd((event) => { | ||
x.value = -cursorRadius; | ||
if (onPanGestureEnd) onPanGestureEnd(event); | ||
}); | ||
|
||
const hoverGesture = Gesture.Hover() | ||
.onBegin((evt) => (x.value = evt.x)) | ||
.onChange((evt) => (x.value = evt.x)) | ||
.onEnd(() => (x.value = -cursorRadius)); | ||
.onBegin((event) => { | ||
x.value = event.x; | ||
y.value = getYForX({ path, x: event.x }); | ||
const point = interpolate( | ||
y.value, | ||
[cursorRadius, height - cursorRadius], | ||
[maxValue, minValue] | ||
); | ||
if (onHoverGestureBegin) onHoverGestureBegin({ event, point }); | ||
}) | ||
.onChange((event) => { | ||
x.value = event.x; | ||
y.value = getYForX({ path, x: event.x }); | ||
const point = interpolate( | ||
y.value, | ||
[cursorRadius, height - cursorRadius], | ||
[maxValue, minValue] | ||
); | ||
if (onHoverGestureChange) onHoverGestureChange({ event, point }); | ||
}) | ||
.onEnd((event) => { | ||
x.value = -cursorRadius; | ||
if (onHoverGestureEnd) onHoverGestureEnd(event); | ||
}); | ||
|
||
// We return a composed gesture that listens to both pan and hover gestures. This is to | ||
// allow the chart component to work on both touch and mouse devices. | ||
return Gesture.Race(hoverGesture, panGesture); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export { LineChart } from "./components/SkiaComponents"; | ||
export { type BannerComponentProps } from "./components/LineChart/Banner"; | ||
export { type AxisLabelComponentProps } from "./components/LineChart/AxisLabel"; |