Last active
March 12, 2025 13:02
-
-
Save scf4/012e9f615f6b43a1712a083b162afd94 to your computer and use it in GitHub Desktop.
react native selectively highlight input text (mentions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { View, Text, TextInput, StyleSheet } from 'react-native'; | |
const styles = StyleSheet.create({ | |
wrapper: { | |
width: '90%', | |
height: 24, | |
position: 'relative', | |
alignSelf: 'center', | |
}, | |
inputWrapper: { | |
position: 'absolute', | |
top: 0, | |
height: 24, | |
width: '100%', | |
borderWidth: 1, | |
borderColor: 'gray', | |
}, | |
input: { | |
height: 24, | |
fontSize: 18, | |
width: '100%', | |
}, | |
text: { | |
height: 24, | |
fontSize: 18, | |
position: 'absolute', | |
top: 0, | |
color: 'transparent', | |
}, | |
mention: { | |
backgroundColor: 'rgba(0, 150, 255, .5)', | |
} | |
}); | |
export default class App extends React.Component { | |
state = { | |
inputText: '', | |
formattedText: '', | |
} | |
handleChangeText = (inputText) => { | |
const words = inputText.split(' '); | |
const formattedText = []; | |
words.forEach(word => { | |
if (!word.startsWith('@')) return formattedText.push(word, ' '); | |
const mention = ( | |
<Text key={word} style={styles.mention}> | |
{word} | |
</Text> | |
); | |
formattedText.push(mention, ' '); | |
}); | |
this.setState({ inputText, formattedText }); | |
} | |
render() { | |
return ( | |
<View style={{ marginTop: 48 }}> | |
<View style={styles.wrapper}> | |
<Text style={styles.text}> | |
{this.state.formattedText} | |
</Text> | |
<View style={styles.inputWrapper}> | |
<TextInput | |
style={styles.input} | |
value={this.state.inputText} | |
onChangeText={this.handleChangeText} | |
/> | |
</View> | |
</View> | |
</View> | |
); | |
} | |
} |
<Text key={index} style={{ color: 'red' }}>
{word}
Thank you soooooooo much for this it worked perfectly
@scf4 Not really, I think most build their own stuff with react native. I saw this(https://github.com/harshq/react-native-mentions), but it's not so modular.
We can definitely make one!
Your snippet was very helpful for syntax highlighting.
@comphonia hey man, do you have some source code for what you showed in the gif?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works great but I'm seeing some unexpected behavior on my end.
I have "mention" color set to red, and all other text set to green. You'll notice in the GIF that after I move the cursor back to the highlighted portion of the code, then press space and start typing again, each letter from then on is very briefly colored red then flips to green. It's very subtle, and only appears to happen if I move the cursor back to a highlighted word and start typing from there.
I'm digging into it to see if I can figure out what's going on, but figured I'd comment here in case anyone has any ideas.
This is a fresh react native project created via
expo init
. The entirety of the project is pasted below:package.json
App.js