usehooks icon indicating copy to clipboard operation
usehooks copied to clipboard

onLongPress : movement during press

Open mtournay opened this issue 1 year ago • 3 comments

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.

mtournay avatar Jan 03 '24 09:01 mtournay

+1

danvoyce avatar May 27 '24 10:05 danvoyce

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,
    }
  )

mikefogg avatar Aug 14 '24 12:08 mikefogg