1
1
import * as React from "react" ;
2
+ import { Platform } from "react-native" ;
2
3
3
4
import propTypes from "@styled-system/prop-types" ;
4
5
import PropTypes from "prop-types" ;
@@ -14,6 +15,8 @@ import {
14
15
system ,
15
16
} from "styled-system" ;
16
17
18
+ import { theme } from "../theme" ;
19
+
17
20
export const Text = styled . Text `
18
21
${ textStyle }
19
22
${ space }
@@ -29,6 +32,13 @@ export const Text = styled.Text`
29
32
} ,
30
33
textTransform : { property : "textTransform" , cssProperty : "textTransform" } ,
31
34
} ) }
35
+ ${ Platform . select ( {
36
+ android : `
37
+ include-font-padding: false;
38
+ text-align-vertical: center;
39
+ ` ,
40
+ ios : "" ,
41
+ } ) }
32
42
` ;
33
43
34
44
/**
@@ -74,9 +84,54 @@ export const Text = styled.Text`
74
84
* @extends StyledSystems props /styled-system
75
85
*/
76
86
77
- export const Typography = ( { children, ...rest } ) => (
78
- < Text { ...rest } > { children } </ Text >
79
- ) ;
87
+ export const Typography = ( {
88
+ children,
89
+ fontSize,
90
+ lineHeightMultiplier = 1.3 ,
91
+ style,
92
+ ...rest
93
+ } ) => {
94
+ const parseSize = size => {
95
+ if ( typeof size === "number" ) return size ;
96
+
97
+ if ( typeof size === "string" ) {
98
+ // Check if it's a theme token
99
+ const themeSize = theme . fontSizes [ size ] ;
100
+ if ( themeSize !== undefined ) {
101
+ return themeSize ;
102
+ }
103
+
104
+ const parsed = parseFloat ( size ) ;
105
+
106
+ return isNaN ( parsed ) ? theme . fontSizes . s : parsed ;
107
+ }
108
+
109
+ return theme . fontSizes . s ;
110
+ } ;
111
+
112
+ const calculatedLineHeight = React . useMemo ( ( ) => {
113
+ const size = parseSize ( fontSize || rest . fontSize || theme . fontSizes . s ) ;
114
+
115
+ return Math . floor ( size * lineHeightMultiplier ) ;
116
+ } , [ fontSize , rest . fontSize , lineHeightMultiplier ] ) ;
117
+
118
+ return (
119
+ < Text
120
+ android_hyphenationFrequency = "full"
121
+ fontSize = { fontSize }
122
+ textBreakStrategy = "simple"
123
+ { ...rest }
124
+ style = { [
125
+ Platform . OS === "android" && {
126
+ lineHeight : calculatedLineHeight ,
127
+ } ,
128
+ style ,
129
+ ] }
130
+ >
131
+ { children }
132
+ </ Text >
133
+ ) ;
134
+ } ;
80
135
81
136
Typography . propTypes = {
82
137
...propTypes . space ,
@@ -86,8 +141,12 @@ Typography.propTypes = {
86
141
...propTypes . color ,
87
142
...propTypes . textStyle ,
88
143
children : PropTypes . node ,
144
+ fontSize : PropTypes . oneOfType ( [ PropTypes . number , PropTypes . string ] ) ,
145
+ lineHeightMultiplier : PropTypes . number ,
146
+ style : PropTypes . object ,
89
147
} ;
90
148
91
149
Typography . defaultProps = {
92
150
textStyle : "defaultTextStyle" ,
151
+ lineHeightMultiplier : 1.3 ,
93
152
} ;
0 commit comments