Skip to content

fix: sanitize velocity to prevent instant snap on Reanimated v4+#2657

Open
ahmadaccino wants to merge 1 commit into
gorhom:masterfrom
ahmadaccino:fix/snap-animation-velocity-2470
Open

fix: sanitize velocity to prevent instant snap on Reanimated v4+#2657
ahmadaccino wants to merge 1 commit into
gorhom:masterfrom
ahmadaccino:fix/snap-animation-velocity-2470

Conversation

@ahmadaccino
Copy link
Copy Markdown

Summary

Fixes #2470 — no smooth animation when the bottom sheet snaps back to its initial position on Reanimated v4 + Gesture Handler v2.

Root Cause

Reanimated v4 + Gesture Handler v2 can report an excessively high velocityY whose direction is opposite to the snap target. When this velocity is passed directly to withSpring, the spring overshoots and resolves in a single frame — producing an instant snap with no visible animation.

Fix

Two guards are applied to the velocity inside animateToPosition before it reaches withSpring:

  1. Clamp the absolute velocity to ±500 — prevents absurdly high values from producing jarring or invisible animations.
  2. Zero the velocity when it opposes the snap direction — if the sheet needs to snap up but velocity points down (or vice versa), velocity is set to 0 so the spring does not fight the target.

Why this is safe for all Reanimated versions

  • The guards live in animateToPosition, the single entry point for all snap animations.
  • Large opposing velocities are never desirable for a snap animation regardless of Reanimated version.
  • On Reanimated v2/v3 where velocity is correctly reported, it is typically already aligned with the snap direction (passes through unchanged) or small enough to be within the 500 cap.
  • Programmatic calls (no gesture) pass velocity = 0 by default and are completely unaffected.

Related

…hom#2470)

Reanimated v4 + Gesture Handler v2 can report an excessively high
velocityY in the opposite direction of the snap target. When passed
directly to withSpring, the spring overshoots and resolves in a single
frame — producing an instant snap with no visible animation.

Fix:
- Clamp velocity magnitude to ±500
- Zero velocity when it opposes the snap direction

These guards are safe for all Reanimated versions since large opposing
velocities are never desirable for snap animations.

Fixes gorhom#2470
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: No smooth animation when returning to the initial position

1 participant