-
Notifications
You must be signed in to change notification settings - Fork 31
/
Text.js
84 lines (79 loc) · 1.89 KB
/
Text.js
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
import React from 'react';
import {Text, StyleSheet} from 'react-native';
import PropTypes from 'prop-types';
import {useThemeContext} from '../util/ThemeProvider';
import {fontBases, fontSizes, fontVariants} from '../util/prop-types';
const getTextStyle = ({
theme,
color,
size,
fontWeight,
fontFamily,
textAlign,
lineHeight,
letterSpacing,
fontBase,
fontVariant,
}) => {
const style = [
{
color: theme.colors[color],
fontSize: theme.fontSize[size],
lineHeight: theme.lineHeight[lineHeight || size],
textAlignVertical: 'center',
fontWeight,
textAlign,
},
];
if (fontFamily) {
style.push({fontFamily: theme.font[fontFamily]});
}
if (letterSpacing) {
style.push({letterSpacing: theme.letterSpacing[letterSpacing]});
}
if (fontBase && fontVariant) {
style.push({
fontFamily: theme.font[fontBase][fontVariant],
});
}
return style;
};
const TextElement = React.forwardRef(({style, ...props}, ref) => {
const theme = useThemeContext();
return (
<Text
{...props}
ref={ref}
includeFontPadding={false}
style={StyleSheet.flatten([getTextStyle({...props, theme}), style])}>
{props.children}
</Text>
);
});
TextElement.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
children: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.array,
PropTypes.element,
]).isRequired,
size: fontSizes,
lineHeight: fontSizes,
fontFamily: PropTypes.string,
color: PropTypes.string,
fontWeight: PropTypes.string,
letterSpacing: fontSizes,
textAlign: PropTypes.oneOf(['left', 'center', 'right']),
fontBase: fontBases,
fontVariant: fontVariants,
};
TextElement.defaultProps = {
color: 'para',
size: 'md',
fontWeight: '500',
textAlign: 'left',
fontBases: 'body',
fontVariant: 'medium',
};
export default TextElement;