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

onLongPress : movement during press #270

Open
mtournay opened this issue Jan 3, 2024 · 3 comments
Open

onLongPress : movement during press #270

mtournay opened this issue Jan 3, 2024 · 3 comments

Comments

@mtournay
Copy link

mtournay commented Jan 3, 2024

Hi

I'm using useLongpress hook, and it's working fine, but when users scrolls, i do not want the event to be fired, or i would have the ability to detect move in the event parameter.

Do you think it's possible ? Am I missing something already existing ?

Regards.

@danvoyce
Copy link

+1

@mikefogg
Copy link

For anyone looking for this still, a solution that seems to be working great for me is to simply track the current scroll position at start use it to compare with the current scroll position when the callback is fired.

Something like:

const startScrollLocation = useRef()
const attrs = useLongPress(
    () => {
      const movement = Math.abs(startScrollLocation.current - scrollContainerRef.current?.scrollTop)
      if (movement > 20) return
      
      fireTheCallback()
    },
    {
      onStart: () => (startScrollLocation.current = scrollContainerRef.current?.scrollTop),
      onFinish: () => (startScrollLocation.current = null),
      threshold: 350,
    }
  )

@tilenmiklavic
Copy link

This pull request might help.
#321

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

4 participants