$ git show drag-snap

Drag and snap

Advanced/anim drag-snap

Drag a view anywhere, then spring back to its origin on release.

Main · New Architecture only · worklets live in react-native-worklets

This snippet uses APIs that didn’t change between versions, so the same code runs on Reanimated 4 and 3.

Gesture.PanuseSharedValuewithSpring
drag-snap.tsx · reanimated 4
const x = useSharedValue(0), y = useSharedValue(0);
const sx = useSharedValue(0), sy = useSharedValue(0);
const pan = Gesture.Pan()
  .onStart(() => { sx.value = x.value; sy.value = y.value; })
  .onUpdate(e => { x.value = sx.value + e.translationX; y.value = sy.value + e.translationY; })
  .onEnd(() => { x.value = withSpring(0); y.value = withSpring(0); });
const style = useAnimatedStyle(() => ({
  transform: [{ translateX: x.value }, { translateY: y.value }],
}));
return <GestureDetector gesture={pan}><Animated.View style={[styles.box, style]} /></GestureDetector>;