Skip to content

Commit 3389d9b

Browse files
committed
fix: nested text styling not inheriting from parent
1 parent 813cdd4 commit 3389d9b

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

example/src/Examples/TextExample.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
Subheading,
1212
customText,
1313
Title,
14+
Text as RNPText,
1415
} from 'react-native-paper';
1516

1617
import { useExampleTheme } from '..';
@@ -111,6 +112,11 @@ const TextExample = () => {
111112
Custom Variant
112113
</Text>
113114
</PaperProvider>
115+
116+
<RNPText style={{ color: 'red' }}>
117+
<RNPText>Nested </RNPText>
118+
Text
119+
</RNPText>
114120
</>
115121
)}
116122
</View>

src/components/Typography/Text.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Text as NativeText,
77
TextStyle,
88
} from 'react-native';
9+
import type { TextProps } from 'react-native';
910

1011
import AnimatedText from './AnimatedText';
1112
import type { VariantProp } from './types';
@@ -14,6 +15,8 @@ import { useInternalTheme } from '../../core/theming';
1415
import type { ThemeProp } from '../../types';
1516
import { forwardRef } from '../../utils/forwardRef';
1617

18+
const TextContext = React.createContext<TextProps | null>(null);
19+
1720
export 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
*/
8386
const 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

Comments
 (0)