@@ -34,12 +34,14 @@ export default function App() {
3434}
3535
3636const styles = StyleSheet .create ({
37- container: { flex: 1 , justifyContent: ' center ' , padding: 16 },
37+ container: { gap: 12 },
3838 input: {
3939 fontSize: 18 ,
40+ color: ' #232736' ,
4041 padding: 12 ,
41- maxHeight: 200 ,
42- backgroundColor: ' gainsboro' ,
42+ borderRadius: 12 ,
43+ minHeight: 96 ,
44+ backgroundColor: ' #eef0ff' ,
4345 },
4446});
4547```
@@ -53,11 +55,11 @@ Add a ref and a button that calls `toggleBold`. Each call flips bold on the
5355current selection, or arms it for the next characters you type if nothing is
5456selected.
5557
56- ``` tsx {2-4,9 ,12-15 }
58+ ``` tsx {2-4,7 ,12,16-18,33-44 }
5759import { EnrichedTextInput } from ' react-native-enriched-html' ;
5860import type { EnrichedTextInputInstance } from ' react-native-enriched-html' ;
5961import { useRef } from ' react' ;
60- import { View , Button , StyleSheet } from ' react-native' ;
62+ import { View , StyleSheet , Pressable , Text } from ' react-native' ;
6163
6264export default function App() {
6365 const ref = useRef <EnrichedTextInputInstance >(null );
@@ -69,32 +71,57 @@ export default function App() {
6971 style = { styles .input }
7072 placeholder = " Type something here..."
7173 />
72- <Button title = " Bold" onPress = { () => ref .current ?.toggleBold ()} />
74+ <Pressable style = { styles .button } onPress = { () => ref .current ?.toggleBold ()} >
75+ <Text style = { styles .text } >Bold</Text >
76+ </Pressable >
7377 </View >
7478 );
7579}
80+
81+ const styles = StyleSheet .create ({
82+ container: { gap: 12 },
83+ input: {
84+ fontSize: 18 ,
85+ color: ' #232736' ,
86+ padding: 12 ,
87+ borderRadius: 12 ,
88+ minHeight: 96 ,
89+ backgroundColor: ' #eef0ff' ,
90+ },
91+ button: {
92+ alignSelf: ' center' ,
93+ width: 100 ,
94+ padding: 8 ,
95+ borderRadius: 24 ,
96+ borderWidth: 1 ,
97+ borderColor: ' #919fcf' ,
98+ },
99+ text: {
100+ textAlign: ' center' ,
101+ color: ' #919fcf' ,
102+ },
103+ });
76104```
77105
78- Every other style works the same way — there's a ` toggleItalic ` ,
106+ Every other style is toggled similarly via ` ref ` — there's a ` toggleItalic ` ,
79107` toggleUnderline ` , ` toggleH1 ` , ` toggleOrderedList ` , and so on.
80108
81109## Reading the state back
82110
83111A toolbar button should also show whether bold is _ currently_ active, so the
84112user knows what they're about to change. The input reports this through the
85- ` onChangeState ` event, which fires whenever the active styles change — including
86- when the user just moves the cursor.
113+ ` onChangeState ` event, which fires whenever the active styles change.
87114
88115The payload holds an entry per style. Use ` bold.isActive ` to drive the button:
89116
90- ``` tsx {8,15- 19,22-26 }
117+ ``` tsx {4,6,11, 19,22,24,50-53,58-60 }
91118import { EnrichedTextInput } from ' react-native-enriched-html' ;
92119import type {
93120 EnrichedTextInputInstance ,
94121 OnChangeStateEvent ,
95122} from ' react-native-enriched-html' ;
96123import { useRef , useState } from ' react' ;
97- import { View , Button , StyleSheet } from ' react-native' ;
124+ import { View , StyleSheet , Pressable , Text } from ' react-native' ;
98125
99126export default function App() {
100127 const ref = useRef <EnrichedTextInputInstance >(null );
@@ -106,16 +133,49 @@ export default function App() {
106133 ref = { ref }
107134 style = { styles .input }
108135 placeholder = " Type something here..."
109- onChangeState = { (e ) => setState (e .nativeEvent )}
110- />
111- <Button
112- title = { state ?.bold .isActive ? ' Unbold' : ' Bold' }
113- color = { state ?.bold .isActive ? ' green' : ' gray' }
114- onPress = { () => ref .current ?.toggleBold ()}
136+ onChangeState = { e => setState (e .nativeEvent )}
115137 />
138+ <Pressable
139+ style = { [styles .button , state ?.bold .isActive && styles .buttonActive ]}
140+ onPress = { () => ref .current ?.toggleBold ()} >
141+ <Text style = { [styles .text , state ?.bold .isActive && styles .textActive ]} >
142+ Bold
143+ </Text >
144+ </Pressable >
116145 </View >
117146 );
118147}
148+
149+ const styles = StyleSheet .create ({
150+ container: { gap: 12 },
151+ input: {
152+ fontSize: 18 ,
153+ color: ' #232736' ,
154+ padding: 12 ,
155+ borderRadius: 12 ,
156+ minHeight: 96 ,
157+ backgroundColor: ' #eef0ff' ,
158+ },
159+ button: {
160+ alignSelf: ' center' ,
161+ width: 100 ,
162+ padding: 8 ,
163+ borderRadius: 24 ,
164+ borderWidth: 1 ,
165+ borderColor: ' #919fcf' ,
166+ },
167+ buttonActive: {
168+ borderColor: ' #57b495' ,
169+ backgroundColor: ' #57b495' ,
170+ },
171+ text: {
172+ textAlign: ' center' ,
173+ color: ' #919fcf' ,
174+ },
175+ textActive: {
176+ color: ' #eef0ff' ,
177+ },
178+ });
119179```
120180
121181That's the full loop: the button ** acts** on the input with ` toggleBold ` , and
@@ -139,4 +199,3 @@ notice the button turns green whenever the cursor sits on bold text. Switch to
139199the ** Code** tab to see the exact source.
140200
141201<InteractiveExample src = { FirstEditorSrc } component = { FirstEditor } />
142-
0 commit comments