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"
_handlePress is 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
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.
- Every nested Text component with an onPress handler is rendered as a
Hyperlink. This solution has some issues due to limitations with Hyperlink:
- 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.
Problem
You can create a pressable Text substring by nesting a
Textcomponent with anonPresshandler inside of anotherTextcomponent. If the outerTextcomponent marks the text asselectable, then the press functionality of the innerTextcomponent is broken. Here's an example:There are a couple of issues with this:
_handlePressis not called.Potential Solutions
I have a few ideas for how to solve this but none are ideal so far:
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.Hyperlink. This solution has some issues due to limitations withHyperlink:Runsand non-HyperlinkSpansinside of theHyperlink(e.g. can't nest inline views). I suspect this won't be problematic in practice.The best option might be (2). We translate a
<Text>with anonPresshandler to a XAMLHyperlink. There will be a lot of limitations in styling but a hyperlink scenario is probably the most common use ofonPressonTextnodes anyway. Also, I can't think of a more general way to solve this nicely given the APIs XAML exposes.