I am trying to wrap a nested text component in a TouchableOpacity or similar.
The use case for this is to parse links and other special pieces of text out of a longer message and make them interactable (links, hashtags, etc...)
ex. "Hello Testing [google.com] for [#energy] and [#candy] in the morning"
where [] represent interactable text
There are many questions like this on Stack Overflow, but in other cases, the OP was able to use a workaround that doesn't work in my case.
Here is an example:
<View style={someStyle}>
<Text style={sharedTextStyles}>
{textChunks.map((chunk:string)=>{
const text = (
<Text style={innerTextStyle}>
{chunk}
</Text>
)
return matchesSomeRegex() ?
<TouchableOpacity
onPressIn={someHandler0}
onPress={someHandler1}
onLongPress={someHandler2}
>
{text}
</TouchableOpacity> :
text
})}
</Text>
</View>
The above code works, but the pressable chunks are seemingly impossible to position within the text lines (too high, and impossible to reposition with styling)
I have tried splitting the string on " " and using flex positioning, which fixes the alignment, but creates new issues with text wrapping, spaces, and especially new lines that I can't think of a way to fix without writing a super complicated onLayout handling.
I am aware that the Text component has press handling, but between the horrible delayed visual feedback and the fact that it doesn't support onLongPress or onPressIn, this route is unworkable in my case.
Does anyone know how to fix this issue?