-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Description
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:
- When you click on "Inner text"
_handlePressis not called. - 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:
- Manually do hit testing on
Textto see if a pressable region was hit. To do this, we'd have to sign up for theRichTextBlock'spointer events usingAddHandlerso we can passtruefor thehandledEventsTooparameter. 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 thePointerMovedhandler, 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. - Every nested Text component with an onPress handler is rendered as a
Hyperlink. This solution has some issues due to limitations withHyperlink:- The major limitation with this approach is that I don't think you can customize the hover color of the text.
- You can only nest
Runsand non-HyperlinkSpansinside of theHyperlink(e.g. can't nest inline views). I suspect this won't be problematic in practice.
- 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.