Skip to content

Pressable Text substrings do not work properly when they're selectable #1679

@rigdern

Description

@rigdern

Problem

You can create a pressable Text substring by nesting a Text component with an onPress handler inside of another Text component. If the outer Text component marks the text as selectable, then the press functionality of the inner Text component is broken. Here's an example:

<Text selectable={true}>
  Outer text
  <Text onPress={this._handlePress} style={{color: 'red'}}>
    Inner text
  </Text>
  More outer text
</Text>

There are a couple of issues with this:

  1. When you click on "Inner text" _handlePress is not called.
  2. When you hover over "Inner text" the cursor is the I-beam selection cursor. Instead, the cursor should be a hand indicating that you are hovering over a clickable element.

Potential Solutions

I have a few ideas for how to solve this but none are ideal so far:

  1. Manually do hit testing on Text to see if a pressable region was hit. To do this, we'd have to sign up for the RichTextBlock's pointer events using AddHandler so we can pass true for the handledEventsToo parameter. A major limitation is apparently we are stuck with the I-beam selection cursor when hovering over text. When trying to set it to something else like an arrow in the PointerMoved handler, the cursor rapidly flickers between I-beam and arrow. To fix this, we'd likely need a feature from the XAML team. Another concern is around the performance of the code that would do the hit testing.
  2. Every nested Text component with an onPress handler is rendered as a Hyperlink. This solution has some issues due to limitations with Hyperlink:
  3. Every nested Text component with an onPress handler is rendered as an inline view. The issue with this approach is that when your selection includes the inline view, the copy command doesn't work.

The best option might be (2). We translate a <Text> with an onPress handler to a XAML Hyperlink. There will be a lot of limitations in styling but a hyperlink scenario is probably the most common use of onPress on Text nodes anyway. Also, I can't think of a more general way to solve this nicely given the APIs XAML exposes.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions