Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lock axis with gesture in the examples? #42

Closed
kossel opened this issue Mar 15, 2019 · 7 comments
Closed

lock axis with gesture in the examples? #42

kossel opened this issue Mar 15, 2019 · 7 comments

Comments

@kossel
Copy link

kossel commented Mar 15, 2019

If you go to the "Draggable List" example in mobile view, dragging items vertically causes the browser to scroll, is there a way to prevent that like preventDefault? I think would be nice to show in the example

https://www.react-spring.io/docs/hooks/examples

@dbismut
Copy link
Collaborator

dbismut commented Mar 15, 2019

Here is an updated sandbox without the browser scrolling, with just a minor css fix:
https://codesandbox.io/s/wwz6nxp8xw

@kossel
Copy link
Author

kossel commented Mar 15, 2019

@dbismut I think you missunderstood my point. we can not always just lock the body with overflow: hidden, would be nice if there a way to lock axis like React pose from popmotion (https://popmotion.io/pose/) see interactivity example.

you can also see this example, when you scroll horizontal the browser vertical scroll is ignored
https://codepen.io/kossel-the-styleful/pen/EMERBe

@dbismut
Copy link
Collaborator

dbismut commented Mar 15, 2019

Sorry I thought your question was related to that specific example.
I think your question is two-folded:

  1. can we lock axis
  2. is there a way to preventDefault

Question 1

Contrary to pose or react-pose, react-with-gesture needs you to be more explicit with your code. So if you only want an element to react to horizontal gestures you could have:

const [{ x }, set] = useSpring(() => ({ x: 0 }));
const bind = useGesture(({ delta, down }) =>
  set({ x: down ? delta[0] : 0, immediate: down })
); // delta[0] contains the x displacement
return (
    <animated.div
      {...bind()}
      style={{ transform: x.interpolate(v => `translateX(${v}px)`) }}
      className="square"
    />
);

See codesandbox here: https://codesandbox.io/s/6xvp63k19r (note that it doesn't fix the body scroll on mobile)

Question 2

Did you have a look at #1? You should be able to access the event element.

@drcmda
Copy link
Member

drcmda commented Mar 15, 2019

The event is passed through, it's part of the event data. but if you're in passive mode you can't call preventDefault, it's forbidden by the browser. You're free to switch passive events off of course. Another possibility would be to simply set the overscroll container to overflow: hidden. that's what i would probably go for.

@kossel
Copy link
Author

kossel commented Mar 15, 2019

Ok, I made it work https://codesandbox.io/embed/y28q726919?codemirror=1 for the scroll

but I still think would be good to include the event.preventDefault() into the examples so when user actually testing/playing it with phone, they don't see the page scrolls

@drcmda
Copy link
Member

drcmda commented Mar 15, 2019

The event is listed on the front-page, look where it says "Event data", that's the generic browser event with all the methods you can call on it. The passive handler stuff is mentioned under config. Passive events are faster and don't cause lag, but it doesn't allow you to stop propagation or prevent defaults.

@kossel
Copy link
Author

kossel commented Mar 16, 2019

thanks for the explanation!

@kossel kossel closed this as completed Mar 16, 2019
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

No branches or pull requests

3 participants