66 Text as NativeText ,
77 TextStyle ,
88} from 'react-native' ;
9+ import type { TextProps } from 'react-native' ;
910
1011import AnimatedText from './AnimatedText' ;
1112import type { VariantProp } from './types' ;
@@ -14,6 +15,8 @@ import { useInternalTheme } from '../../core/theming';
1415import type { ThemeProp } from '../../types' ;
1516import { forwardRef } from '../../utils/forwardRef' ;
1617
18+ const TextContext = React . createContext < TextProps | null > ( null ) ;
19+
1720export type Props < T > = React . ComponentProps < typeof NativeText > & {
1821 /**
1922 * @supported Available in v5.x with theme version 3
@@ -81,13 +84,14 @@ export type TextRef = React.ForwardedRef<{
8184 * @extends Text props https://reactnative.dev/docs/text#props
8285 */
8386const Text = (
84- { style, variant, theme : initialTheme , ...rest } : Props < string > ,
87+ { style, variant, theme : initialTheme , children , ...rest } : Props < string > ,
8588 ref : TextRef
8689) => {
8790 const root = React . useRef < NativeText | null > ( null ) ;
8891 // FIXME: destructure it in TS 4.6+
8992 const theme = useInternalTheme ( initialTheme ) ;
9093 const writingDirection = I18nManager . getConstants ( ) . isRTL ? 'rtl' : 'ltr' ;
94+ const parentTextContext = React . useContext ( TextContext ) ;
9195
9296 React . useImperativeHandle ( ref , ( ) => ( {
9397 setNativeProps : ( args : Object ) => root . current ?. setNativeProps ( args ) ,
@@ -117,7 +121,7 @@ const Text = (
117121 // specified in a parent in favor of children's variant:
118122 if ( props . variant ) {
119123 font = theme . fonts [ props . variant as VariantProp < typeof props . variant > ] ;
120- textStyle = [ style , font ] ;
124+ textStyle = [ style , props . style , font ] ;
121125 }
122126
123127 // Case two: Nested `Text` has specified `styles` which intefere
@@ -149,20 +153,35 @@ const Text = (
149153 textStyle ,
150154 ] }
151155 { ...rest }
152- />
156+ >
157+ { children }
158+ </ NativeText >
153159 ) ;
154160 } else {
155161 const font = theme . isV3 ? theme . fonts . default : theme . fonts ?. regular ;
156162 const textStyle = {
157163 ...font ,
158164 color : theme . isV3 ? theme . colors ?. onSurface : theme . colors . text ,
159165 } ;
166+
167+ if ( parentTextContext ) {
168+ Object . assign ( textStyle , StyleSheet . flatten ( parentTextContext . style ) ) ;
169+ }
170+
160171 return (
161- < NativeText
162- { ...rest }
163- ref = { root }
164- style = { [ styles . text , textStyle , { writingDirection } , style ] }
165- />
172+ < TextContext . Provider
173+ value = { {
174+ style : [ styles . text , textStyle , { writingDirection } , style ] ,
175+ } }
176+ >
177+ < NativeText
178+ { ...rest }
179+ ref = { root }
180+ style = { [ styles . text , textStyle , { writingDirection } , style ] }
181+ >
182+ { children }
183+ </ NativeText >
184+ </ TextContext . Provider >
166185 ) ;
167186 }
168187} ;
0 commit comments