Repository navigation
fix(text): inherit parent styles in nested Text - #5040
Conversation
A nested Text or AnimatedText without a variant applied its fallback font unconditionally, overriding the font, weight, letter spacing and color it should have inherited from the enclosing text. React Native inherits these, so a nested one now declares only its own style. The shared context lives in its own module to keep the Text and AnimatedText imports one-directional. Fixes #4351.
|
A quick note on the Typography work: because a nested |
MikitasK
left a comment
There was a problem hiding this comment.
overall, these changes looks good to me 👌 the approach looks solid & aligns with RN's text inheritance behavior 👍
just a few point to address before merge:
Co-authored-by: Mikita Kliushun <nikita.kliushun@gmail.com>
Co-authored-by: Mikita Kliushun <nikita.kliushun@gmail.com>
|
@MikitasK thanks for the review, your proposed changes sounded good! |
|
@satya164 @artus9033 all yours |
satya164
left a comment
There was a problem hiding this comment.
please remove all LLM generated comments, they are either unnecessary or could be replaced with 1-2 lines describing the purpose.
only add comments where you think it's needed for understanding something and write it by hand.
for tests: add tests where
Textisn't a direct child and can be nested inside another component that's used insideText- child text also sets its variant and theme
this implementation also isn't accurate to what react native does. a View nested inside Text will reset the inheritance, though it might be a reasonable limitation.
|
Review by Claude Screenshots of this code added to the Typography screen, dark theme: <Text variant="headlineSmall" style={{ color: 'tomato' }}>
A tomato <Text>plain child</Text>
</Text>
<Text variant="headlineSmall" style={{ color: 'tomato' }}>
B tomato <Text variant="bodyLarge">variant child</Text>
</Text>
<Text style={{ color: 'tomato', fontWeight: 'bold' }}>
C tomato bold <Text variant="titleLarge">variant child</Text>
</Text>
<Text variant="headlineSmall">
D parent <Text theme={{ colors: { onSurface: 'green' } }}>green theme child</Text>
</Text>
<Chip selected onPress={() => {}}><Text variant="labelLarge">Var</Text></Chip>
<Button mode="contained"><Text variant="labelLarge">Var</Text></Button>iOS
Android
Web
|
satya164
left a comment
There was a problem hiding this comment.
Review by Claude
See the inline comments. (via Claude, verify if it makes sense)
Keep a comment only where it helps, in one line, and drop the context sentence that the nested font change made wrong. Check every child for an element instead of copying React Native's three-child cutoff. The provider is still skipped when the children are only strings or numbers.
Assert that a nested Text sets no color of its own and inherits the parent's, and cover a Text nested through another component. Fold the test that did not check what its title said into the one above it, drop the duplicate of the default font test, and replace the three AnimatedText tests with one.
A nested Text or AnimatedText with its own theme renders like a top-level one again, with the theme's color, as it does on main.
A nested Text or AnimatedText with a variant now applies only the variant font, so it no longer resets the color to onSurface. A Text with a variant inside a contained Button keeps the button's label color.
The nested section was inserted above the Custom Variant sample, which made that sample read as one of the nested cases.
|
Comments are trimmed to one line where they help (890995c). New tests cover a |



Motivation
A nested
Textreset the font and color it should inherit: aTextwith a variant in a containedButtonrendered inonSurface, barely visible in dark theme, and aChiparound a plainTextlost its font.Now a nested
TextorAnimatedTextsets only itsstyleand variant font and inherits the rest. With its ownthemeit renders like a top-level one.Related issue
Fixes #4351.
Screenshots / Videos
Before
After
Test plan
iOS, Android and web, light and dark: nest
Textwith and without a variant ortheme, and aTextwith a variant in aChipand a containedButton. Children keep the parent color unless they have atheme. Known limit (#5162): aTextin aViewin aTextgets the native default color.