For a client we're building horizontally dragging rows of media items. Dragging horizontally using Framer-Motion works great, but I can't animate the x position on the click of a button.
This is the general idea:
This is the component as I currently have it (I removed style, etc. for brevity):
const HorizontalScroll = ({ children }) => {
const x = useMotionValue(0);
function onLeftClick() {
const xPos = x.get();
if (Math.round(xPos) === 0) {
return;
}
const newXPosition = xPos + 600;
x.set(newXPosition > 0 ? 0 : newXPosition);
}
function onRightClick() {
const xPos = x.get();
const newXPosition = xPos - 600;
x.set(newXPosition < -2000 ? -2000 : newXPosition);
}
return (
<>
<button
type="button"
onClick={onLeftClick}
>
Left
</button>
<motion.div
drag="x"
dragConstraints={{ left: -2000, right: 0 }}
initial={false}
style={{ width: 2000, x }}
>
{children}
</motion.div>
<button
type="button"
onClick={onRightClick}
>
Right
</button>
</>
);
};
So I do x.set()
when clicking either left or right on the arrows. Doing x.set()
works, but it isn't animated.
Instead of const x = useMotionValue(0)
I could use useSpring
or useTransform
, but that breaks the dragging behaviour.
So in short, I want to animate the x.set()
, but I've no idea how to do that. Anybody got an idea?