-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathTextAlignmentEditor.tsx
More file actions
82 lines (77 loc) · 2.09 KB
/
Copy pathTextAlignmentEditor.tsx
File metadata and controls
82 lines (77 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { EnrichedTextInput } from 'react-native-enriched-html';
import type {
EnrichedTextInputInstance,
OnChangeStateEvent,
} from 'react-native-enriched-html';
import { useRef, useState } from 'react';
import { View, StyleSheet, Pressable, Text } from 'react-native';
import { htmlStyle } from './htmlStyle';
const alignments = ['left', 'center', 'right', 'justify'] as const;
export default function App() {
const ref = useRef<EnrichedTextInputInstance>(null);
const [state, setState] = useState<OnChangeStateEvent | null>(null);
const renderButton = (alignment: (typeof alignments)[number]) => {
// A single string reports the alignment of the paragraph at the cursor.
const isActive = state?.alignment === alignment;
return (
<Pressable
key={alignment}
style={[styles.button, isActive && styles.buttonActive]}
onPress={() => {
const alignmentToSet = isActive ? 'auto' : alignment;
ref.current?.setTextAlignment(alignmentToSet);
}}>
<Text style={[styles.text, isActive && styles.textActive]}>
{alignment}
</Text>
</Pressable>
);
};
return (
<View style={styles.container}>
<EnrichedTextInput
ref={ref}
style={styles.input}
htmlStyle={htmlStyle}
placeholder="Type a paragraph, then align it below..."
onChangeState={e => setState(e.nativeEvent)}
/>
<View style={styles.row}>{alignments.map(renderButton)}</View>
</View>
);
}
const styles = StyleSheet.create({
container: { gap: 12 },
input: {
fontSize: 18,
color: '#232736',
padding: 12,
borderRadius: 12,
minHeight: 96,
backgroundColor: '#eef0ff',
},
row: {
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
gap: 8,
},
button: {
width: 90,
padding: 8,
borderRadius: 24,
borderWidth: 1,
borderColor: '#919fcf',
},
buttonActive: {
borderColor: '#57b495',
backgroundColor: '#57b495',
},
text: {
textAlign: 'center',
color: '#919fcf',
},
textActive: {
color: '#eef0ff',
},
});