-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathText.tsx
More file actions
206 lines (186 loc) · 6.29 KB
/
Text.tsx
File metadata and controls
206 lines (186 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import * as React from 'react';
import {
I18nManager,
StyleProp,
StyleSheet,
Text as NativeText,
TextStyle,
} from 'react-native';
import type { TextProps } from 'react-native';
import AnimatedText from './AnimatedText';
import type { VariantProp } from './types';
import StyledText from './v2/StyledText';
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
*
* Variant defines appropriate text styles for type role and its size.
* Available variants:
*
* Display: `displayLarge`, `displayMedium`, `displaySmall`
*
* Headline: `headlineLarge`, `headlineMedium`, `headlineSmall`
*
* Title: `titleLarge`, `titleMedium`, `titleSmall`
*
* Label: `labelLarge`, `labelMedium`, `labelSmall`
*
* Body: `bodyLarge`, `bodyMedium`, `bodySmall`
*/
variant?: VariantProp<T>;
children: React.ReactNode;
theme?: ThemeProp;
style?: StyleProp<TextStyle>;
};
export type TextRef = React.ForwardedRef<{
setNativeProps(args: Object): void;
}>;
// @component-group Typography
/**
* Typography component showing styles complied with passed `variant` prop and supported by the type system.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Text } from 'react-native-paper';
*
* const MyComponent = () => (
* <>
* <Text variant="displayLarge">Display Large</Text>
* <Text variant="displayMedium">Display Medium</Text>
* <Text variant="displaySmall">Display small</Text>
*
* <Text variant="headlineLarge">Headline Large</Text>
* <Text variant="headlineMedium">Headline Medium</Text>
* <Text variant="headlineSmall">Headline Small</Text>
*
* <Text variant="titleLarge">Title Large</Text>
* <Text variant="titleMedium">Title Medium</Text>
* <Text variant="titleSmall">Title Small</Text>
*
* <Text variant="bodyLarge">Body Large</Text>
* <Text variant="bodyMedium">Body Medium</Text>
* <Text variant="bodySmall">Body Small</Text>
*
* <Text variant="labelLarge">Label Large</Text>
* <Text variant="labelMedium">Label Medium</Text>
* <Text variant="labelSmall">Label Small</Text>
* </>
* );
*
* export default MyComponent;
* ```
*
* @extends Text props https://reactnative.dev/docs/text#props
*/
const Text = (
{ 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),
}));
if (theme.isV3 && variant) {
let font = theme.fonts[variant];
let textStyle = [font, style];
if (
React.isValidElement(children) &&
(children.type === Component ||
children.type === AnimatedText ||
children.type === StyledText)
) {
const { props } = rest.children as {
props: { variant?: string; style?: StyleProp<TextStyle> };
};
// Context: Some components have the built-in `Text` component with a predefined variant,
// that also accepts `children` as a `React.Node`. This can result in a situation,
// where another `Text` component is rendered within the built-in `Text` component.
// By doing that, we assume that user doesn't want to consume pre-defined font properties.
// Case one: Nested `Text` has different `variant` that specified in parent. For example:
// <Chip>
// <Text variant="displayMedium">Nested</Text>
// </Chip>
// Solution: To address the following scenario, the code below overrides the `variant`
// 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, props.style, font];
}
// Case two: Nested `Text` has specified `styles` which intefere
// with font properties, from the parent's `variant`. For example:
// <Chip>
// <Text style={{fontSize: 30}}>Nested</Text>
// </Chip>
// Solution: To address the following scenario, the code below overrides the
// parent's style with children's style:
if (!props.variant) {
textStyle = [style, props.style];
}
}
if (typeof font !== 'object') {
throw new Error(
`Variant ${variant} was not provided properly. Valid variants are ${Object.keys(
theme.fonts
).join(', ')}.`
);
}
return (
<NativeText
ref={root}
style={[
styles.text,
{ writingDirection, color: theme.colors.onSurface },
textStyle,
]}
{...
}
>
{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 (
<TextContext.Provider
value={{
style: [styles.text, textStyle, { writingDirection }, style],
}}
>
<NativeText
{...rest}
ref={root}
style={[styles.text, textStyle, { writingDirection }, style]}
>
{children}
</NativeText>
</TextContext.Provider>
);
}
};
const styles = StyleSheet.create({
text: {
textAlign: 'left',
},
});
type TextComponent<T> = (
props: Props<T> & { ref?: React.RefObject<TextRef> }
) => JSX.Element;
const Component = forwardRef(Text) as TextComponent<never>;
export const customText = <T,>() => Component as unknown as TextComponent<T>;
export default Component;