Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions example/src/Examples/TextExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Subheading,
customText,
Title,
Text as RNPText,
} from 'react-native-paper';

import { useExampleTheme } from '..';
Expand Down Expand Up @@ -111,6 +112,11 @@ const TextExample = () => {
Custom Variant
</Text>
</PaperProvider>

<RNPText style={{ color: 'red' }}>
<RNPText>Nested </RNPText>
Text
</RNPText>
</>
)}
</View>
Expand Down
35 changes: 27 additions & 8 deletions src/components/Typography/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Text as NativeText,
TextStyle,
} from 'react-native';
import type { TextProps } from 'react-native';

import AnimatedText from './AnimatedText';
import type { VariantProp } from './types';
Expand All @@ -14,6 +15,8 @@ import { useInternalTheme } from '../../core/theming';
import type { ThemeProp } from '../../types';
import { forwardRef } from '../../utils/forwardRef';

const TextContext = React.createContext<TextProps | null>(null);

export type Props<T> = React.ComponentProps<typeof NativeText> & {
/**
* @supported Available in v5.x with theme version 3
Expand Down Expand Up @@ -81,13 +84,14 @@ export type TextRef = React.ForwardedRef<{
* @extends Text props https://reactnative.dev/docs/text#props
*/
const Text = (
{ style, variant, theme: initialTheme, ...rest }: Props<string>,
{ style, variant, theme: initialTheme, children, ...rest }: Props<string>,
ref: TextRef
) => {
const root = React.useRef<NativeText | null>(null);
// FIXME: destructure it in TS 4.6+
const theme = useInternalTheme(initialTheme);
const writingDirection = I18nManager.getConstants().isRTL ? 'rtl' : 'ltr';
const parentTextContext = React.useContext(TextContext);

React.useImperativeHandle(ref, () => ({
setNativeProps: (args: Object) => root.current?.setNativeProps(args),
Expand Down Expand Up @@ -117,7 +121,7 @@ const Text = (
// specified in a parent in favor of children's variant:
if (props.variant) {
font = theme.fonts[props.variant as VariantProp<typeof props.variant>];
textStyle = [style, font];
textStyle = [style, props.style, font];
}

// Case two: Nested `Text` has specified `styles` which intefere
Expand Down Expand Up @@ -149,20 +153,35 @@ const Text = (
textStyle,
]}
{...rest}
/>
>
{children}
</NativeText>
);
} else {
const font = theme.isV3 ? theme.fonts.default : theme.fonts?.regular;
const textStyle = {
...font,
color: theme.isV3 ? theme.colors?.onSurface : theme.colors.text,
};

if (parentTextContext) {
Object.assign(textStyle, StyleSheet.flatten(parentTextContext.style));
}

return (
<NativeText
{...rest}
ref={root}
style={[styles.text, textStyle, { writingDirection }, style]}
/>
<TextContext.Provider
value={{
style: [styles.text, textStyle, { writingDirection }, style],
}}
>
<NativeText
{...rest}
ref={root}
style={[styles.text, textStyle, { writingDirection }, style]}
>
{children}
</NativeText>
</TextContext.Provider>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Text Context Missing in V3 Theme Variants

The TextContext.Provider is only present in the else branch, not in the theme.isV3 variant branch. This inconsistency prevents nested Text components from inheriting parent styles when using V3 themes with variants.

Fix in Cursor Fix in Web

);
}
};
Expand Down