Skip to content

Fix(iOS): onPress fails for Text with nested View#49393

Closed
coado wants to merge 1 commit into
facebook:mainfrom
coado:@dmalecki/text-with-nested-view
Closed

Fix(iOS): onPress fails for Text with nested View#49393
coado wants to merge 1 commit into
facebook:mainfrom
coado:@dmalecki/text-with-nested-view

Conversation

@coado
Copy link
Copy Markdown
Contributor

@coado coado commented Feb 13, 2025

Summary:

On the new architecture the RCTParagraphTextView is used to draw text on iOS but React doesn't know about it. The hit test stops propagation on the first encountered UIView of this type. The suggested solution is to add a dummy hit test for RCTParagraphTextView that returns always null, allowing further search of the touch target.

Changelog:

[IOS][FIXED] - Fixed onPress for Text with nested View.

Test Plan:

Before After
before.mov
after.mov
code
import {SafeAreaView, StyleSheet, Text, View, findNodeHandle} from 'react-native';

function App() {
  return (
    <SafeAreaView style={styles.container}>
      <Text
        ref={e => console.log(`Outer Text: ${findNodeHandle(e)}`)}
      >
        With Nested View
        <View ref={e => console.log(`View: ${findNodeHandle(e)}`)}>
          <Text
            ref={e => console.log(`Inner Text: ${findNodeHandle(e)}`)}
            onPress={() => {
              console.log('1. text pressed');
            }}
            style={styles.pressableText}
          >
            Pressable Text
          </Text>
        </View>
      </Text>
      <Text>
        Without Nested View
        <Text
          onPress={() => {
            console.log('2. text pressed');
          }}
          style={styles.pressableText}
        >
          Pressable Text
        </Text>
      </Text>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    gap: 10,
  },
  pressableText: {
    fontSize: 18,
    color: 'blue',
    backgroundColor: 'transparent',
  },
  nestedView: {
    backgroundColor: 'red',
    height: 100,
  },
  pressable: {
    backgroundColor: 'blue',
    height: 100,
    width: 100,
  },
});

export default App;

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Software Mansion Partner: Software Mansion Partner p: Facebook Partner: Facebook labels Feb 13, 2025
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@coado has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Summary:
On the new architecture the `RCTParagraphTextView` is used to draw text on iOS but React doesn't know about it. The hit test stops propagation on the first encountered  UIView of this type. The suggested solution is to add a dummy hit test for `RCTParagraphTextView` that returns always `null`, allowing further search of the touch target.


## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[IOS][FIXED] - Fixed onPress for Text with nested View.


Test Plan:
| Before       | After        |
|--------------|--------------|
|<video src="https://github.com/user-attachments/assets/745f555a-d4b6-4285-91a7-0e9ea9f43d84"> | <video src="https://github.com/user-attachments/assets/9896848f-13ca-4b57-bcc9-bead478ab078">  |



<details>
 <summary>code</summary>

```ts
import {SafeAreaView, StyleSheet, Text, View, findNodeHandle} from 'react-native';

function App() {
  return (
    <SafeAreaView style={styles.container}>
      <Text
        ref={e => console.log(`Outer Text: ${findNodeHandle(e)}`)}
      >
        With Nested View
        <View ref={e => console.log(`View: ${findNodeHandle(e)}`)}>
          <Text
            ref={e => console.log(`Inner Text: ${findNodeHandle(e)}`)}
            onPress={() => {
              console.log('1. text pressed');
            }}
            style={styles.pressableText}
          >
            Pressable Text
          </Text>
        </View>
      </Text>
      <Text>
        Without Nested View
        <Text
          onPress={() => {
            console.log('2. text pressed');
          }}
          style={styles.pressableText}
        >
          Pressable Text
        </Text>
      </Text>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    gap: 10,
  },
  pressableText: {
    fontSize: 18,
    color: 'blue',
    backgroundColor: 'transparent',
  },
  nestedView: {
    backgroundColor: 'red',
    height: 100,
  },
  pressable: {
    backgroundColor: 'blue',
    height: 100,
    width: 100,
  },
});

export default App;

```

</details>

Differential Revision: D69591520

Pulled By: coado
@coado coado force-pushed the @dmalecki/text-with-nested-view branch from 149e7d5 to 06e034c Compare February 13, 2025 13:45
@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D69591520

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@coado merged this pull request in 6b2c40c.

@react-native-bot
Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @coado in 6b2c40c

When will my fix make it into a release? | How to file a pick request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged This PR has been merged. p: Facebook Partner: Facebook p: Software Mansion Partner: Software Mansion Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants