-
Notifications
You must be signed in to change notification settings - Fork 124
Improve behavior for animated graph with gesture #14
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,9 @@ export function AnimatedLineGraph({ | |
TopAxisLabel, | ||
BottomAxisLabel, | ||
selectionDotShadowColor, | ||
gestureHoldDuration = 300, | ||
initialIndex, | ||
resetPositionOnRelease, | ||
...props | ||
}: AnimatedLineGraphProps): React.ReactElement { | ||
const [width, setWidth] = useState(0) | ||
|
@@ -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 | ||
[] | ||
) | ||
|
||
|
@@ -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) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? Afaik it worked fine before There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
) | ||
|
@@ -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], | ||
foyarash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[pathEnd] | ||
) | ||
|
||
useEffect(() => { | ||
if ( | ||
typeof initialIndex === 'number' && | ||
foyarash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
initialIndex < points.length && | ||
width | ||
foyarash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
const xForIndex = Math.round((initialIndex * width) / points.length) | ||
foyarash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setFingerX(xForIndex) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
foyarash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, [points, initialIndex, width]) | ||
|
||
return ( | ||
<View {...props}> | ||
<GestureDetector gesture={enablePanGesture ? gesture : undefined}> | ||
|
Uh oh!
There was an error while loading. Please reload this page.