MCPcopy Create free account
hub / github.com/callstack/react-native-paper / Text

Function Text

src/components/Typography/Text.tsx:84–171  ·  view source on GitHub ↗
(
  { style, variant, theme: initialTheme, ...rest }: Props<string>,
  ref: TextRef
)

Source from the content-addressed store, hash-verified

82 * @extends Text props https://reactnative.dev/docs/text#props
83 */
84const Text = (
85 { style, variant, theme: initialTheme, ...rest }: Props<string>,
86 ref: TextRef
87) => {
88 const root = React.useRef<NativeText | null>(null);
89 // FIXME: destructure it in TS 4.6+
90 const theme = useInternalTheme(initialTheme);
91 const writingDirection = I18nManager.getConstants().isRTL ? 'rtl' : 'ltr';
92
93 React.useImperativeHandle(ref, () => ({
94 setNativeProps: (args: Object) => root.current?.setNativeProps(args),
95 }));
96
97 if (theme.isV3 && variant) {
98 let font = theme.fonts[variant];
99 let textStyle = [font, style];
100
101 if (
102 React.isValidElement(rest.children) &&
103 (rest.children.type === Component ||
104 rest.children.type === AnimatedText ||
105 rest.children.type === StyledText)
106 ) {
107 const { props } = rest.children as {
108 props: { variant?: string; style?: StyleProp<TextStyle> };
109 };
110
111 // Context: Some components have the built-in `Text` component with a predefined variant,
112 // that also accepts `children` as a `React.Node`. This can result in a situation,
113 // where another `Text` component is rendered within the built-in `Text` component.
114 // By doing that, we assume that user doesn't want to consume pre-defined font properties.
115 // Case one: Nested `Text` has different `variant` that specified in parent. For example:
116 // <Chip>
117 // <Text variant="displayMedium">Nested</Text>
118 // </Chip>
119 // Solution: To address the following scenario, the code below overrides the `variant`
120 // specified in a parent in favor of children's variant:
121 if (props.variant) {
122 font = theme.fonts[props.variant as VariantProp<typeof props.variant>];
123 textStyle = [style, font];
124 }
125
126 // Case two: Nested `Text` has specified `styles` which intefere
127 // with font properties, from the parent's `variant`. For example:
128 // <Chip>
129 // <Text style={{fontSize: 30}}>Nested</Text>
130 // </Chip>
131 // Solution: To address the following scenario, the code below overrides the
132 // parent's style with children's style:
133 if (!props.variant) {
134 textStyle = [style, props.style];
135 }
136 }
137
138 if (typeof font !== 'object') {
139 throw new Error(
140 `Variant ${variant} was not provided properly. Valid variants are ${Object.keys(
141 theme.fonts

Callers

nothing calls this directly

Calls 1

useInternalThemeFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…