Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve behavior for animated graph with gesture #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false
"useTabs": false,
"semi": false
},
"react-native-builder-bob": {
"source": "src",
Expand Down
46 changes: 33 additions & 13 deletions src/AnimatedLineGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export function AnimatedLineGraph({
TopAxisLabel,
BottomAxisLabel,
selectionDotShadowColor,
gestureHoldDuration = 300,
initialIndex,
resetPositionOnRelease,
...props
}: AnimatedLineGraphProps): React.ReactElement {
const [width, setWidth] = useState(0)
Expand All @@ -53,6 +56,7 @@ export function AnimatedLineGraph({
setWidth(Math.round(layout.width))
setHeight(Math.round(layout.height))
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)

Expand Down Expand Up @@ -158,7 +162,9 @@ export function AnimatedLineGraph({
[interpolateProgress]
)

const { gesture, isActive, x } = useHoldOrPanGesture({ holdDuration: 300 })
const { gesture, isActive, x } = useHoldOrPanGesture({
holdDuration: gestureHoldDuration,
})
const circleX = useValue(0)
const circleY = useValue(0)
const pathEnd = useValue(0)
Expand Down Expand Up @@ -193,17 +199,25 @@ export function AnimatedLineGraph({
damping: 50,
velocity: 0,
})
if (!active) pathEnd.current = 1
if (!active && resetPositionOnRelease) pathEnd.current = 1

if (active) onGestureStart?.()
else onGestureEnd?.()
},
[circleRadius, onGestureEnd, onGestureStart, pathEnd]
[
circleRadius,
onGestureEnd,
onGestureStart,
pathEnd,
resetPositionOnRelease,
]
)
useAnimatedReaction(
() => x.value,
(fingerX) => {
runOnJS(setFingerX)(fingerX)
() => [x.value, isActive.value],
([fingerX, isActiveValue]) => {
if (isActiveValue) {
runOnJS(setFingerX)(fingerX as number)
}
Comment on lines -204 to +223
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? Afaik it worked fine before

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes with the idea of the graph highlight & the initialIndex. On the initial render, the setFingerX function is executed, but as there is no gesture, the highlight path starts at 0.

},
[isActive, setFingerX, width, x]
)
Expand All @@ -215,16 +229,22 @@ export function AnimatedLineGraph({
[isActive, setIsActive]
)
const positions = useDerivedValue(
() => [
0,
Math.min(0.15, pathEnd.current),
pathEnd.current,
pathEnd.current,
1,
],
() => [0, pathEnd.current, pathEnd.current, pathEnd.current, 1],
[pathEnd]
)

useEffect(() => {
if (
typeof initialIndex === 'number' &&
initialIndex < points.length &&
width
) {
const xForIndex = Math.round((initialIndex * width) / points.length)
setFingerX(xForIndex)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [points, initialIndex, width])

return (
<View {...props}>
<GestureDetector gesture={enablePanGesture ? gesture : undefined}>
Expand Down
15 changes: 15 additions & 0 deletions src/LineGraphProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ export type AnimatedLineGraphProps = BaseLineGraphProps & {
* The element that gets rendered below the Graph (usually the "min" point/value of the Graph)
*/
BottomAxisLabel?: () => React.ReactElement | null

/**
* Hold duration for the graph gesture
*/
gestureHoldDuration?: number

/**
* Initial index shown on the graph
*/
initialIndex?: number

/**
* Wether to reset or not the circle position when releasing the gesture
*/
resetPositionOnRelease?: boolean
}

export type LineGraphProps =
Expand Down