[BUG] Stop scrolling from interfering with dragging
See original GitHub issueIs your feature request related to a problem? Please describe.
If you render a list of drag="x"
components and try to scroll down on a touch device, a small amount of horizontal drag happens on each element that your finger touches.
Describe the solution you’d like A generic solution could be to allow the variant styles to override the styles from the drag.
let variants = {
// this would override whatever x value the drag gesture is providing
static: { x: 0 },
swiping: { },
};
let Swipeable = () => {
let [isSwiping, setIsSwiping] = useState(0);
let onDrag = (event, info) => setIsSwiping(info.offset.x > 10);
return (
<motion.div drag="x" animate={isSwiping ? "swiping" : "static"} onDrag={onDrag} />
);
}
Describe alternatives you’ve considered
I also tried overriding by passing transform
styles directly to the motion.div
element.
A more specific version of this might be adding a dragThreshold
prop that would specify the offsets that the gesture must pass before the onDragStart
event is called.
Another option might be having some way to cancel the animation (but not the event) from the onDrag
handler.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:29 (1 by maintainers)
Top GitHub Comments
I have a motion component that I want to drag on
"x"
for animations, but it takes enough of the screen on mobile that I want users to be able to drag it on"y"
for scrolling. Do you have any advice or workaround ideas right now?I just build a nice slider using the
drag="x"
functionality, when I ran into this issue. The component takes up most of the viewport, so you should be able to scroll down the page without trigging the drag effect.Had to look into
blockViewportScroll()
, and it seems like a really aggressive method of preventing scrolling while dragging, since we have no way of controlling it.I know this is one of those difficult issues, and we can agree that you don’t want scrolling to occur while dragging.
I made “fix” that determines if we are scrolling or dragging based on the initial velocity in
onDragStart
- but it’s not perfect. Might be better if it could sample a few events before locking it.