question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LongPress not working on web

See original GitHub issue

Description

Not all LongPress callbacks are being called on web.

Platforms

  • iOS
  • Android
  • Web

Screenshots

long-press-screen-recording

Steps To Reproduce

Run the code below and press the red circle.

Expected behavior

For all these callbacks to be called:

  • onTouchesDown
  • onBegin
  • onStrart
  • onTouchesUp
  • onEnd
  • onFinalize

Actual behavior

Only these callbacks are called:

  • onEnd
  • onFinalize

Snack or minimal code example

import { Platform, StyleSheet, Text } from 'react-native'
import {
  Gesture,
  GestureDetector,
  GestureHandlerRootView,
} from 'react-native-gesture-handler'
import Animated, {
  useAnimatedStyle,
  useSharedValue,
} from 'react-native-reanimated'

export default function App() {
  const isPressed = useSharedValue(false)
  const onTouchesDownWasCalled = useSharedValue(false)
  const onBeginWasCalled = useSharedValue(false)
  const onStartWasCalled = useSharedValue(false)
  const onTouchesUpWasCalled = useSharedValue(false)
  const onEndWasCalled = useSharedValue(false)
  const onFinalizeWasCalled = useSharedValue(false)

  const longPressGesture = Gesture.LongPress()
    .onTouchesDown(() => {
      isPressed.value = true
      onTouchesDownWasCalled.value = true
      onBeginWasCalled.value = false
      onStartWasCalled.value = false
      onTouchesUpWasCalled.value = false
      onEndWasCalled.value = false
      onFinalizeWasCalled.value = false
    })
    .onBegin(() => {
      onBeginWasCalled.value = true
    })
    .onStart(() => {
      onStartWasCalled.value = true
    })
    .onTouchesUp(() => {
      onTouchesUpWasCalled.value = true
    })
    .onEnd(() => {
      onEndWasCalled.value = true
    })
    .onFinalize(() => {
      isPressed.value = false
      onFinalizeWasCalled.value = true
    })

  const onTouchesDownStyles = useAnimatedStyle(() => ({
    opacity: onTouchesDownWasCalled.value ? 1 : 0,
  }))

  const onBeginStyles = useAnimatedStyle(() => ({
    opacity: onBeginWasCalled.value ? 1 : 0,
  }))

  const onStartStyles = useAnimatedStyle(() => ({
    opacity: onStartWasCalled.value ? 1 : 0,
  }))

  const onTouchesUpStyles = useAnimatedStyle(() => ({
    opacity: onTouchesUpWasCalled.value ? 1 : 0,
  }))

  const onEndStyles = useAnimatedStyle(() => ({
    opacity: onEndWasCalled.value ? 1 : 0,
  }))

  const onFinalizeStyles = useAnimatedStyle(() => ({
    opacity: onFinalizeWasCalled.value ? 1 : 0,
  }))

  const ballAnimatedStyles = useAnimatedStyle(() => ({
    backgroundColor: isPressed.value ? 'yellow' : 'red',
  }))

  return (
    <GestureHandlerRootView style={styles.container}>
      <Text style={styles.text}>
        {Platform.OS === 'web' ? 'Web Version' : 'Native Version'}
      </Text>
      <Animated.View style={onTouchesDownStyles}>
        <Text style={styles.text}>`onTouchesDown()` was called.</Text>
      </Animated.View>
      <Animated.View style={onBeginStyles}>
        <Text style={styles.text}>`onBegin()` was called.</Text>
      </Animated.View>
      <Animated.View style={onStartStyles}>
        <Text style={styles.text}>`onStart()` was called.</Text>
      </Animated.View>
      <Animated.View style={onTouchesUpStyles}>
        <Text style={styles.text}>`onTouchesUp()` was called.</Text>
      </Animated.View>
      <Animated.View style={onEndStyles}>
        <Text style={styles.text}>`onEnd()` was called.</Text>
      </Animated.View>
      <Animated.View style={onFinalizeStyles}>
        <Text style={styles.text}>`onFinalize()` was called.</Text>
      </Animated.View>
      <GestureDetector gesture={longPressGesture}>
        <Animated.View style={[styles.ball, ballAnimatedStyles]} />
      </GestureDetector>
    </GestureHandlerRootView>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  text: {
    paddingBottom: 16,
    fontSize: 16,
  },
  ball: {
    width: 128,
    height: 128,
    borderRadius: 64,
  },
})

Package versions

  • React: 17.0.1
  • React Native: 0.64.3
  • React Native Gesture Handler: 2.2.1
  • React Native Reanimated: 2.3.3

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
j-piaseckicommented, May 19, 2022

Hi! The linked PR should fix most of the issues. What’s still not fixed are touch events (onTouches...), as that will require more research to see if implementing it using hammerjs is feasible.

0reactions
elliotwaitecommented, Oct 7, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Long Press in JavaScript? - jquery - Stack Overflow
Any long press event must address multiple issues which this answer ignores. 1) Distinguish long press from drag from gesture from multi ...
Read more >
How to Detect Long Press Gestures in JavaScript Events in ...
So let's work through this step by step: Cancel our long press timeout if we know it's not a long press; Prevent our...
Read more >
Long press on new tab not working : r/chrome - Reddit
So I used to scroll through the cards on the new tab page when I was bored, opening anything I found interesting by...
Read more >
Long press to open in new tab has stopped working
I used to be able to open a link into a new tab on my phone by long pressing on it to get...
Read more >
How to enable the long press back button to show the history ...
You'll find that if you go to the play store and uninstall Chrome, it'll remove all recent updates, which seemed to be the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found