-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathAnimatedText.tsx
More file actions
107 lines (99 loc) · 2.58 KB
/
AnimatedText.tsx
File metadata and controls
107 lines (99 loc) · 2.58 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
import * as React from 'react';
import {
Animated,
I18nManager,
StyleProp,
StyleSheet,
TextStyle,
Text,
} from 'react-native';
import type { VariantProp } from './types';
import { useInternalTheme } from '../../core/theming';
import type { ThemeProp } from '../../types';
import { forwardRef } from '../../utils/forwardRef';
type Props<T> = React.ComponentPropsWithRef<typeof Animated.Text> & {
/**
* 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>;
style?: StyleProp<TextStyle>;
/**
* @optional
*/
theme?: ThemeProp;
};
/**
* Animated text component which follows styles from the theme.
*
* @extends Text props https://reactnative.dev/docs/text#props
*/
const AnimatedText = forwardRef<Text & HTMLElement, Props<never>>(
function AnimatedText(
{ style, theme: themeOverrides, variant, ...rest },
ref
) {
const theme = useInternalTheme(themeOverrides);
const writingDirection = I18nManager.getConstants().isRTL ? 'rtl' : 'ltr';
if (theme.isV3 && variant) {
const font = theme.fonts[variant];
if (typeof font !== 'object') {
throw new Error(
`Variant ${variant} was not provided properly. Valid variants are ${Object.keys(
theme.fonts
).join(', ')}.`
);
}
return (
<Animated.Text
ref={ref}
{...rest}
style={[
font,
styles.text,
{ writingDirection, color: theme.colors.onSurface },
style,
]}
/>
);
} else {
const font = !theme.isV3 ? theme.fonts.regular : theme.fonts.bodyMedium;
const textStyle = {
...font,
color: theme.isV3 ? theme.colors.onSurface : theme.colors.text,
};
return (
<Animated.Text
ref={ref}
{...rest}
style={[
styles.text,
textStyle,
{
writingDirection,
},
style,
]}
/>
);
}
}
);
const styles = StyleSheet.create({
text: {
textAlign: 'left',
},
});
export const customAnimatedText = <T,>() =>
AnimatedText as (props: Props<T>) => React.ReactElement | null;
export default AnimatedText;