11import React , { useCallback } from 'react'
22import {
3- Platform ,
4- Text ,
5- type TextLayoutEvent ,
6- type TextProps ,
7- unstable_TextAncestorContext ,
3+ Platform ,
4+ Text ,
5+ type TextLayoutEvent ,
6+ type TextProps ,
7+ unstable_TextAncestorContext ,
88} from 'react-native'
99
1010import {
11- callback ,
12- getHostComponent ,
13- type HybridRef ,
11+ callback ,
12+ getHostComponent ,
13+ type HybridRef ,
1414} from 'react-native-nitro-modules'
1515import NitroTextConfig from '../nitrogen/generated/shared/json/NitroTextConfig.json'
1616import type { NitroTextMethods , NitroTextProps } from './specs/nitro-text.nitro'
@@ -19,123 +19,123 @@ import { flattenChildrenToFragments, styleToFragment } from './utils'
1919export type NitroTextRef = HybridRef < NitroTextProps , NitroTextMethods >
2020
2121const NitroTextView = getHostComponent < NitroTextProps , NitroTextMethods > (
22- 'NitroText' ,
23- ( ) => NitroTextConfig
22+ 'NitroText' ,
23+ ( ) => NitroTextConfig
2424)
2525
2626type NitroTextPropsWithEvents = Pick <
27- NitroTextProps ,
28- 'onTextLayout' | 'onPress' | 'onPressIn' | 'onPressOut' | 'menus'
27+ NitroTextProps ,
28+ 'onTextLayout' | 'onPress' | 'onPressIn' | 'onPressOut' | 'menus'
2929> &
30- Omit < TextProps , 'onTextLayout' >
30+ Omit < TextProps , 'onTextLayout' >
3131
3232let TextAncestorContext = unstable_TextAncestorContext
3333if (
34- Platform . constants . reactNativeVersion . major > 0 ||
35- ( Platform . constants . reactNativeVersion . major === 0 &&
36- Platform . constants . reactNativeVersion . minor < 81 )
34+ Platform . constants . reactNativeVersion . major > 0 ||
35+ ( Platform . constants . reactNativeVersion . major === 0 &&
36+ Platform . constants . reactNativeVersion . minor < 81 )
3737) {
38- TextAncestorContext = require ( 'react-native/Libraries/Text/TextAncestor' )
38+ TextAncestorContext = require ( 'react-native/Libraries/Text/TextAncestor' )
3939}
4040export const NitroText = ( props : NitroTextPropsWithEvents ) => {
41- const isInsideRNText = React . useContext ( TextAncestorContext )
42- const {
43- children,
44- style,
45- selectable,
46- selectionColor,
47- onTextLayout,
48- onPress,
49- onPressIn,
50- onPressOut,
51- onLongPress,
52- ...rest
53- } = props
41+ const isInsideRNText = React . useContext ( TextAncestorContext )
42+ const {
43+ children,
44+ style,
45+ selectable,
46+ selectionColor,
47+ onTextLayout,
48+ onPress,
49+ onPressIn,
50+ onPressOut,
51+ onLongPress,
52+ ...rest
53+ } = props
5454
55- const isSimpleText =
56- typeof children === 'string' || typeof children === 'number'
55+ const isSimpleText =
56+ typeof children === 'string' || typeof children === 'number'
5757
58- const fragments = React . useMemo ( ( ) => {
59- if ( isSimpleText ) return [ ]
60- return flattenChildrenToFragments ( children , style )
61- } , [ children , style , isSimpleText ] )
58+ const fragments = React . useMemo ( ( ) => {
59+ if ( isSimpleText ) return [ ]
60+ return flattenChildrenToFragments ( children , style )
61+ } , [ children , style , isSimpleText ] )
6262
63- const onRNTextLayout = useCallback (
64- ( e : TextLayoutEvent ) => {
65- onTextLayout ?.( e . nativeEvent )
66- } ,
67- [ onTextLayout ]
68- )
63+ const onRNTextLayout = useCallback (
64+ ( e : TextLayoutEvent ) => {
65+ onTextLayout ?.( e . nativeEvent )
66+ } ,
67+ [ onTextLayout ]
68+ )
6969
70- if ( isInsideRNText || Platform . OS === 'android' ) {
71- return (
72- < Text
73- { ...rest }
74- selectionColor = { selectionColor as any }
75- onPress = { onPress }
76- onPressIn = { onPressIn }
77- onPressOut = { onPressOut }
78- onLongPress = { onLongPress }
79- selectable = { selectable }
80- style = { style }
81- onTextLayout = { onRNTextLayout }
82- >
83- { children }
84- </ Text >
85- )
86- }
87-
88- const topStyles = styleToFragment ( style || undefined )
70+ if ( isInsideRNText || Platform . OS === 'android' ) {
71+ return (
72+ < Text
73+ { ...rest }
74+ selectionColor = { selectionColor as any }
75+ onPress = { onPress }
76+ onPressIn = { onPressIn }
77+ onPressOut = { onPressOut }
78+ onLongPress = { onLongPress }
79+ selectable = { selectable }
80+ style = { style }
81+ onTextLayout = { onRNTextLayout }
82+ >
83+ { children }
84+ </ Text >
85+ )
86+ }
8987
90- if ( isSimpleText ) {
91- return (
92- < NitroTextView
93- { ...rest }
94- selectable = { selectable }
95- fontFamily = { topStyles . fontFamily }
96- selectionColor = { selectionColor as string }
97- text = { String ( children ) }
98- style = { style }
99- fontColor = { topStyles . fontColor }
100- textAlign = { topStyles . textAlign }
101- textTransform = { topStyles . textTransform }
102- fontSize = { topStyles . fontSize }
103- fontWeight = { topStyles . fontWeight }
104- fontStyle = { topStyles . fontStyle }
105- lineHeight = { topStyles . lineHeight }
106- letterSpacing = { topStyles . letterSpacing }
107- textDecorationLine = { topStyles . textDecorationLine }
108- textDecorationColor = { topStyles . textDecorationColor }
109- textDecorationStyle = { topStyles . textDecorationStyle }
110- onTextLayout = { callback ( onTextLayout ) }
111- onPress = { callback ( onPress ) }
112- onPressIn = { callback ( onPressIn ) }
113- onPressOut = { callback ( onPressOut ) }
114- />
115- )
116- }
88+ const topStyles = styleToFragment ( style || undefined )
11789
118- return (
119- < NitroTextView
90+ if ( isSimpleText ) {
91+ return (
92+ < NitroTextView
12093 { ...rest }
12194 selectable = { selectable }
122- fragments = { fragments }
12395 fontFamily = { topStyles . fontFamily }
12496 selectionColor = { selectionColor as string }
97+ text = { String ( children ) }
12598 style = { style }
12699 fontColor = { topStyles . fontColor }
100+ textAlign = { topStyles . textAlign }
101+ textTransform = { topStyles . textTransform }
102+ fontSize = { topStyles . fontSize }
127103 fontWeight = { topStyles . fontWeight }
128104 fontStyle = { topStyles . fontStyle }
105+ lineHeight = { topStyles . lineHeight }
129106 letterSpacing = { topStyles . letterSpacing }
130- textAlign = { topStyles . textAlign }
131- textTransform = { topStyles . textTransform }
132107 textDecorationLine = { topStyles . textDecorationLine }
133108 textDecorationColor = { topStyles . textDecorationColor }
134109 textDecorationStyle = { topStyles . textDecorationStyle }
135110 onTextLayout = { callback ( onTextLayout ) }
136111 onPress = { callback ( onPress ) }
137112 onPressIn = { callback ( onPressIn ) }
138113 onPressOut = { callback ( onPressOut ) }
139- />
140- )
114+ />
115+ )
116+ }
117+
118+ return (
119+ < NitroTextView
120+ { ...rest }
121+ selectable = { selectable }
122+ fragments = { fragments }
123+ fontFamily = { topStyles . fontFamily }
124+ selectionColor = { selectionColor as string }
125+ style = { style }
126+ fontColor = { topStyles . fontColor }
127+ fontWeight = { topStyles . fontWeight }
128+ fontStyle = { topStyles . fontStyle }
129+ letterSpacing = { topStyles . letterSpacing }
130+ textAlign = { topStyles . textAlign }
131+ textTransform = { topStyles . textTransform }
132+ textDecorationLine = { topStyles . textDecorationLine }
133+ textDecorationColor = { topStyles . textDecorationColor }
134+ textDecorationStyle = { topStyles . textDecorationStyle }
135+ onTextLayout = { callback ( onTextLayout ) }
136+ onPress = { callback ( onPress ) }
137+ onPressIn = { callback ( onPressIn ) }
138+ onPressOut = { callback ( onPressOut ) }
139+ />
140+ )
141141}
0 commit comments