Inertia/momentum incorrect behavior, and unexpected value jumping
🐛 Bug Report
This is a bug for tracking the overall not-quite-right behavior of 1:1 animation with momentum at the end. I started thinking about this after my issue on react-use-gesture last weekend. Here are some earlier reports and discussions from react-spring and react-use-gesture:
https://github.com/react-spring/react-spring/issues/887 https://github.com/react-spring/react-spring/issues/1010 https://github.com/react-spring/react-use-gesture/issues/132
This is a problem that comes up most commonly with gestures, but I believe the solution lies in the animation primitives. Mapping gestures 1:1 with the animation requires using the immediate parameter, but doing so ends up breaking the expected velocity/momentum for the end of the animation/gesture. You can get kind of close to the desired behavior by using a stiff spring config without the immediate parameter, but this isn't ideal. HCI research has shown that the desired behavior for these types of gestures is a 1:1 correspondence.
The previous suggested solutions for building this behavior have been custom hooks outside the library, but hopefully we can all agree that this is something that should be covered by the library. Previous solutions added a concept of decay, but I think the library already has all of the concepts/APIs we need, it's just a matter of fixing how velocity is calculated when using immediate.
Expected behavior with reproduction
This is a demo of some horizontal scroll areas nested in a vertical scrolling area. Both of them have "touch" scrolling enabled; the scrollLeft and scrollTop are being animated, in order to co-exist with native browser scrolling.
https://codesandbox.io/s/react-spring-inertial-scroll-challenge-qh4wr
The desired behavior is exemplified by the native scrolling behavior on macOS with a trackpad -- 1:1 mapping of scroll gesture to position, with momentum proportional to the spring config when ending the gesture with velocity. The animated value should not overshoot its target.
The touch scrolling demo implementation does have some momentum, but only a tiny bit; not at all like you'd expect. Here's a gif showing how much momentum I could get when trying my hardest to "sling" it

Environment
react-springv9.0.0-rc.3reactv16.8.12
I've the same issue (just to reporting that I saw the same bug)
Hi @lourd
So what you're saying is, you should be able to click and drag a row and "sling it" so it animates with inertia to a stop?
And currently, as I can see it's pretty static af click and sling as opposed to doing a scroll on a mac trackpad – https://codesandbox.io/s/react-spring-touch-scrolling-example-forked-vc5q0?file=/src/App.js (updated to v9 deps to be sure).
Is that a fair summary?
Hey @joshuaellis, thanks for digging into this!
Yes, that's a good summary. Looking for end behavior that's super similar to how Greensock's inertia plugins feel.
Heads up that the sandbox updated to v9 has a bug:

RE. the bug, that looks like it might have something to do with react-use-gesture?
So does it actually make sense to have inertia when immedate = true? As immediate is defined in the docs as:
Prevents animation if true. Also valid as a function for individual keys: key => immediate
So having inertia would break that concept no?
Just like momentum scrolling on mobile phones, you want to be able to have a 1:1/"immediate" animation as your dragging something, and then when you let go the item should keep animating/moving in accordance with the velocity. Setting immediate should not set the velocity of the spring to 0.
Oh I see, thanks @lourd this makes a bit more sense to the issue we have!
Upon great inspection i don't think we need to use immediate we can use set & start.
onDrag({ down, movement: [x], first, last, vxvy: [vx] }) {
console.log("onDrag with movement", { x, first, last, vx, down });
if (first) isDragging.current = true;
if (last) setTimeout(() => (isDragging.current = false), 0);
if (down) {
api.set({ x: -x });
} else {
api.start({ x: -x });
}
}
where set sets the spring to the values and start should animate. It doesn't solve the issue, but my understanding of spring means the start value should be given the end goal.... 🤔
This example demonstrates this method and you can see the velocity is still 0 – https://codesandbox.io/s/react-spring-touch-scrolling-example-forked-vc5q0?file=/src/App.js
So maybe there's a problem with velocity updating at all....?
This has been released on 9.2.0-beta.0 if you could try it that would be great ⭐
Hey @joshuaellis, I just tried this out and am not seeing any change to the behavior. Here's an update of the original sandbox using 9.2.0-beta.1.
Here's the relevant code:
function HorizontalScrollView(props) {
const ref = React.useRef();
const [{ x }, spring] = useSpring(() => ({
x: 0,
config: { decay: 0.8, frequency: 5.5 }
}));
const bind = useGesture(
{
onDrag({ down, movement: [x], first, last, vxvy: [vx] }) {
console.log("X onDrag with: ", { x, vx, first, last, down });
spring.start({ x: -x, immediate: down });
},
onWheelStart() {
// Stop any user-land scroll animation from confcliting with the browser
spring.stop();
}
},
{
drag: {
axis: "x",
filterTaps: true,
initial() {
console.log("Initial x value", -ref.current.scrollLeft);
return [-ref.current.scrollLeft, 0];
}
}
}
);
return (
<animated.div
ref={ref}
scrollLeft={x}
className={props.className}
{...bind()}
>
{props.children}
</animated.div>
);
}
Am I using the API incorrectly? I'm using start instead of set, with immediate set to the down value. I'm passing in a value for decay in the initial useSpring config. I also tried setting decay to true in the call to start and got the same behavior.
I am seeing correct-looking values for the x velocity being logged, but it doesn't seem like they're affecting the animation.
I also tried using set instead of start. That causes a TypeError with Cannot read property 'set' of undefined
I've made some tweaks to the sandbox, you need to pass a velocity:
https://codesandbox.io/s/react-spring-920-beta-touch-scrolling-example-forked-2cdov?file=/src/App.js
I think this solves the issue? If so, please feel free to close this one 🙏🏼
That works brilliantly, thanks @joshuaellis!!!
I played around with your tweaks a little more, and it looks like the vxvy Vector2 parameter to onDrag is already scaled with the direction, so that can be used instead of having to scale the velocity Vector2. I updated my 9.2.0 beta sandbox with that change. I also converted it to TypeScript and added a Leva range control for the decay config to play around with.
Thats cool, if you were interested when it's done we could add it to react-spring.io as a guide or somewhere else!
For sure! If I could get a pointer towards where to put it I'd be happy to. Do you think I should write it up as a guide or add it as a demo?
Hmm, tried to reuse 99% exact same horizontal scroll view but every time velocity is supposed to kick in, scroll left jumps to 0 for a split second and back to where we left off, no momentum.
https://codesandbox.io/s/testing-123-58u9s?file=/src/App.js
Initial value and drag handler values are correct, here's what last config looks like after mouse release:
{
immediate: false,
x: 116.89642333984375,
config: {
velocity: 1.6103612199126909
}
}
Edit: Didn't read that fix is in beta versions, not in latest solid. My bad.
you have to define as a function ev.stopProgation();
If I'm not wrong, offset is also jumping, i.e it doesn't seem to take decay into account.
is there any way to know where spring will rest, the final value, with decay and velocity?