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

[Android] Text onPress triggers parent Touchable onPress #3159

Closed
dragoncodes opened this issue Oct 15, 2024 · 5 comments
Closed

[Android] Text onPress triggers parent Touchable onPress #3159

dragoncodes opened this issue Oct 15, 2024 · 5 comments
Labels
Platform: Android This issue is specific to Android Repro provided A reproduction with a snack or repo is provided

Comments

@dragoncodes
Copy link

Description

When a Text with onPress is a child of any Touchable (Opacity, Highlight, WithoutFeedback), when tapping on the Text the onPress of the Touchable parent is also triggered.

*Only seen on Android

Note: On iOS only the parent Touchable#onPress is triggered, but if the Text is wrapped in NativeViewGestureHandler -> only the Text#onPress is called

Steps to reproduce

  1. Open https://snack.expo.dev/@dragoncodes/rngh-multi-onpress-repro?platform=android
  2. Click on the text in the middle of the screen

Current behavior:

Counter is incremented by 2

Expected behaviour:

Counter is incremented by 1

Preview:

export default function App() {
  const [counter, setCounter] = useState(0);

  return (
    <GestureHandlerRootView
      style={{
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
      }}>
      <Text>{`Counter: ${counter}`}</Text>

      <TouchableOpacity
        onPress={() => {
          setCounter(counter + 1);
        }}>
        <Text
          onPress={() => {
            setCounter(counter + 1);
          }}>
          {'Inner Text'}
        </Text>
      </TouchableOpacity>
    </GestureHandlerRootView>
  );
}

Note: Not sure what runtime snacks use, but also tested with [email protected], [email protected], Hermes on a real device

Snack or a link to a repository

https://snack.expo.dev/@dragoncodes/rngh-multi-onpress-repro?platform=android

Gesture Handler version

2.16.2

React Native version

0.74.1

Platforms

Android

JavaScript runtime

Hermes

Workflow

Expo bare workflow

Architecture

Fabric (New Architecture)

Build type

Release mode

Device

Real device

Device model

Samsung Galaxy Fold 4

Acknowledgements

Yes

@github-actions github-actions bot added Platform: Android This issue is specific to Android Repro provided A reproduction with a snack or repo is provided labels Oct 15, 2024
@m-bert
Copy link
Contributor

m-bert commented Nov 8, 2024

Hi @dragoncodes! Could you please check if using our Text component from this PR works? 😅

m-bert added a commit that referenced this issue Dec 12, 2024
## Description

This PR adds `Text` component to **Gesture Handler**. 

Upon investigating #3159 we decided that it will be better to add our own `Text` component, instead of forcing users to create their own version of `Text` with `NativeViewGestureHandler`.

## Test plan

<details>
<summary>New example:</summary>

```jsx
import { useState } from 'react';
import { StyleSheet } from 'react-native';
import {
  Text,
  GestureHandlerRootView,
  TouchableOpacity,
} from 'react-native-gesture-handler';

export default function NestedText() {
  const [counter, setCounter] = useState(0);

  return (
    <GestureHandlerRootView style={styles.container}>
      <Text style={{ fontSize: 30 }}>{`Counter: ${counter}`}</Text>

      <TouchableOpacity
        onPress={() => {
          console.log('Touchable');
          setCounter((prev) => prev + 1);
        }}>
        <Text
          style={[styles.textCommon, styles.outerText]}
          onPress={() => {
            console.log('Outer text');
            setCounter((prev) => prev + 1);
          }}>
          {'Outer Text '}
          <Text
            style={[styles.textCommon, styles.innerText]}
            onPress={() => {
              console.log('Nested text');
              setCounter((prev) => prev + 1);
            }}>
            {'Nested Text'}
          </Text>
        </Text>
      </TouchableOpacity>
    </GestureHandlerRootView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',

    gap: 20,
  },

  textCommon: {
    padding: 10,
    color: 'white',
  },

  outerText: {
    fontSize: 30,
    borderWidth: 2,
    backgroundColor: '#131313',
  },

  innerText: {
    fontSize: 25,
    backgroundColor: '#F06312',
  },
});


```

</details>
@mhoran
Copy link

mhoran commented Jan 12, 2025

I gave the changes in #3202 a try in my iOS app. While the sample code in the PR mostly works (on iOS the Touchable onPress never fires), in a real app I see inconsistent behavior with the new Text component. It sort of works, but most of the time causes a wrapping TouchableHighlight to never register (it doesn't highlight, nor does the onLongPress fire.) The Text onPress does seem to work reliably (as it does with the sample), just the TouchableHighlight failing intermittently. If I revert to React Native's text component, the TouchableHighlight onLongPress does fire as expected (and the line is highlighted), but of course the Text onPress does not work.

@mhoran
Copy link

mhoran commented Jan 13, 2025

To clarify the above: this works as expected with React Native components, but does not behave the same with RNGH. I have Text components which may or may not have onPress and onLongPress handlers, wrapped in a TouchableHighlight. With React Native components, the onPress and onLongPress handlers on the Text will always be called if present. However, the touch will fall through to the TouchableHighlight if they are not. I would like to use the RNGH TouchableHighlight instead, with the new Text component, it does not work consistently.

@m-bert
Copy link
Contributor

m-bert commented Jan 13, 2025

Thanks for your feedback @mhoran. I'll look into it.

I'm closing this issue as it seems to be resolved. In the meantime, could you open new one regarding problems that you've mentioned?

@m-bert m-bert closed this as completed Jan 13, 2025
@mhoran
Copy link

mhoran commented Jan 13, 2025

Thanks. I have opened a new issue here: #3331.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: Android This issue is specific to Android Repro provided A reproduction with a snack or repo is provided
Projects
None yet
Development

No branches or pull requests

3 participants