import React, { useState } from 'react';
import {
View,
TextInput,
InputAccessoryView,
TouchableOpacity,
Text,
ScrollView,
StyleSheet,
} from 'react-native';
const App = () => {
const inputAccessoryViewID = 'formatToolbar';
const [text, setText] = useState('');
const insertFormatting = (prefix, suffix = '') => {
setText(text + prefix + suffix);
};
return (
<View style={styles.container}>
<ScrollView style={styles.content}>
<Text style={styles.preview}>{text}</Text>
</ScrollView>
<TextInput
style={styles.input}
inputAccessoryViewID={inputAccessoryViewID}
placeholder="Type with formatting..."
value={text}
onChangeText={setText}
multiline
/>
<InputAccessoryView
nativeID={inputAccessoryViewID}
backgroundColor="#ffffff"
style={styles.accessoryView}>
<ScrollView horizontal showsHorizontalScrollIndicator={false}>
<View style={styles.toolbar}>
<TouchableOpacity
style={styles.button}
onPress={() => insertFormatting('**', '**')}>
<Text style={styles.buttonText}>Bold</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => insertFormatting('_', '_')}>
<Text style={styles.buttonText}>Italic</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => insertFormatting('# ')}>
<Text style={styles.buttonText}>H1</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => insertFormatting('- ')}>
<Text style={styles.buttonText}>List</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => insertFormatting('`', '`')}>
<Text style={styles.buttonText}>Code</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => setText('')}>
<Text style={[styles.buttonText, styles.clearButton]}>Clear</Text>
</TouchableOpacity>
</View>
</ScrollView>
</InputAccessoryView>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
content: {
flex: 1,
padding: 20,
},
preview: {
fontSize: 16,
lineHeight: 24,
},
input: {
borderTopWidth: 1,
borderTopColor: '#e0e0e0',
padding: 15,
fontSize: 16,
minHeight: 60,
},
accessoryView: {
borderTopWidth: 1,
borderTopColor: '#e0e0e0',
},
toolbar: {
flexDirection: 'row',
padding: 8,
gap: 8,
},
button: {
paddingVertical: 8,
paddingHorizontal: 16,
backgroundColor: '#007AFF',
borderRadius: 8,
justifyContent: 'center',
alignItems: 'center',
},
buttonText: {
color: '#ffffff',
fontSize: 14,
fontWeight: '600',
},
clearButton: {
color: '#ff3b30',
},
});
export default App;