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.

Gesture.Tap().maxDuration(...) not working on web

See original GitHub issue

Description

Using maxDuration() on a Tap gesture doesn’t seem to work on web.

Platforms

  • iOS
  • Android
  • Web

Screenshots

screen-recording

Steps To Reproduce

Run the code example below on the web then tap and hold on the red circle.

Expected behavior

For it to behave the same as on native where if the tap is held longer than the maxDuration value, the onTouchesCancelled callback is called followed by the onFinalize callback being called.

Actual behavior

On web, the tap can be held forever without ever being canceled.

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 wasCancelled = useSharedValue(false)
  const wasFinalized = useSharedValue(false)

  const tapGesture = Gesture.Tap()
    .maxDuration(500)
    .onBegin(() => {
      isPressed.value = true
      wasCancelled.value = false
      wasFinalized.value = false
    })
    .onTouchesCancelled(() => {
      wasCancelled.value = true
    })
    .onFinalize(() => {
      isPressed.value = false
      wasFinalized.value = true
    })

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

  const wasFinalizedAnimatedStyles = useAnimatedStyle(() => ({
    opacity: wasFinalized.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={wasCancelledAnimatedStyles}>
        <Text style={styles.text}>`onTouchesCancelled()` was called.</Text>
      </Animated.View>
      <Animated.View style={wasFinalizedAnimatedStyles}>
        <Text style={styles.text}>`onFinalize()` was called.</Text>
      </Animated.View>
      <GestureDetector gesture={tapGesture}>
        <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:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
j-piaseckicommented, May 19, 2022

It’s similar situation to https://github.com/software-mansion/react-native-gesture-handler/issues/2063#issuecomment-1131475881. If you want to know whether the gesture ended successfully or was cancelled you can use the second argument of onEnd and onFinalize callbacks, for example:

const tap = Gesture.Tap()
  .onBegin(() => console.log('begin'))
  .onStart(() => console.log('start'))
  .onEnd((_e, success) => console.log('end, successful:', success))
  .onFinalize((_e, success) => console.log('finalize, successful:', success));
0reactions
elliotwaitecommented, Oct 7, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

react native - How to stop propagation of Gesture.Tap()?
Here is a minimal example of how I tried to set up two nested GestureDetector with Gesture.Tap() gestures. import React from 'react'; import ......
Read more >
Camera - Expo Documentation
Run the example on your device to see all these features working together! ... one of maxDuration and maxFileSize is reached or camera...
Read more >
Tap gesture | React Native Gesture Handler - Software Mansion
A discrete gesture that recognizes one or many taps. Tap gestures detect one or more fingers briefly touching the screen. The fingers involved...
Read more >
A guide to delaying gestures in ScrollView - Hacking with Swift
Motivation. A gesture inside a scrolling view will hijack scrolling if the gesture is configured to be recognised first:
Read more >
Recognize a tap gesture - .NET MAUI - Microsoft Learn
NET Multi-platform App UI (.NET MAUI) tap gesture recognizer is used for tap detection and is implemented with the TapGestureRecognizer class.
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