16
16
* limitations under the License.
17
17
*/
18
18
19
- import {
20
- Text ,
21
- Platform ,
22
- StyleSheet ,
23
- } from 'react-native' ;
24
19
import React from 'react' ;
25
20
import PropTypes from 'prop-types' ;
21
+ import { Text , Platform , StyleSheet } from 'react-native' ;
26
22
27
23
const IOS_TOKENS = require ( 'bpk-tokens/tokens/ios/base.react.native.common.js' ) ;
28
24
const ANDROID_TOKENS = require ( 'bpk-tokens/tokens/android/base.react.native.common.js' ) ;
@@ -34,72 +30,58 @@ const tokens = Platform.select({
34
30
35
31
const TEXT_STYLES = [ 'xs' , 'sm' , 'base' , 'lg' , 'xl' , 'xxl' ] ;
36
32
37
- const styleForTextStyle = ( textStyle ) => {
33
+ const getAndroidFontFamily = fontWeight =>
34
+ ( fontWeight === '500' ? 'sans-serif-medium' : 'sans-serif' ) ;
35
+
36
+ const getStyleByTextStyle = ( textStyle ) => {
38
37
const camelCasedStyle = textStyle [ 0 ] . toUpperCase ( ) + textStyle . slice ( 1 ) ;
39
38
40
- const determineAndroidFontFamily = ( fontWeight ) => {
41
- switch ( fontWeight ) {
42
- case '500' :
43
- return 'sans-serif-medium' ;
44
- case '400' :
45
- default :
46
- return 'sans-serif' ;
47
- }
48
- } ;
49
-
50
- return {
51
- fontSize : tokens [ `text${ camelCasedStyle } FontSize` ] ,
52
- lineHeight : tokens [ `text${ camelCasedStyle } LineHeight` ] ,
53
- fontWeight : Platform . OS === 'ios' ? tokens [ `text${ camelCasedStyle } FontWeight` ] : undefined ,
54
- color : tokens . colorGray900 ,
55
- fontFamily : Platform . OS === 'android' ?
56
- determineAndroidFontFamily ( tokens [ `text${ camelCasedStyle } FontWeight` ] ) : 'System' ,
57
- } ;
39
+ const color = tokens . colorGray700 ;
40
+ const fontSize = tokens [ `text${ camelCasedStyle } FontSize` ] ;
41
+ const lineHeight = tokens [ `text${ camelCasedStyle } LineHeight` ] ;
42
+ const fontWeight = Platform . OS === 'ios' ? tokens [ `text${ camelCasedStyle } FontWeight` ] : undefined ;
43
+ const androidFontFamily = getAndroidFontFamily ( tokens [ `text${ camelCasedStyle } FontWeight` ] ) ;
44
+ const fontFamily = Platform . OS === 'android' ? androidFontFamily : 'System' ;
45
+
46
+ return { fontSize, lineHeight, fontWeight, color, fontFamily } ;
58
47
} ;
59
48
60
49
const styles = StyleSheet . create ( {
61
- xs : {
62
- ...styleForTextStyle ( 'xs' ) ,
63
- } ,
64
- sm : {
65
- ...styleForTextStyle ( 'sm' ) ,
66
- } ,
67
- base : {
68
- ...styleForTextStyle ( 'base' ) ,
69
- } ,
70
- lg : {
71
- ...styleForTextStyle ( 'lg' ) ,
72
- } ,
73
- xl : {
74
- ...styleForTextStyle ( 'xl' ) ,
75
- } ,
76
- xxl : {
77
- ...styleForTextStyle ( 'xxl' ) ,
78
- } ,
50
+ xs : { ...getStyleByTextStyle ( 'xs' ) } ,
51
+ sm : { ...getStyleByTextStyle ( 'sm' ) } ,
52
+ base : { ...getStyleByTextStyle ( 'base' ) } ,
53
+ lg : { ...getStyleByTextStyle ( 'lg' ) } ,
54
+ xl : { ...getStyleByTextStyle ( 'xl' ) } ,
55
+ xxl : { ...getStyleByTextStyle ( 'xxl' ) } ,
79
56
} ) ;
80
57
81
58
const BpkText = ( props ) => {
82
- const { children, textStyle, style : innerStyle , ...rest } = props ;
83
- const style = styles [ textStyle ] ;
84
- const finalStyles = [ style ] ;
59
+ const { children, textStyle, style, emphasize, ...rest } = props ;
85
60
86
- if ( innerStyle ) {
87
- finalStyles . push ( innerStyle ) ;
61
+ const emphasizedStyle = emphasize ? { fontWeight : '600' } : { } ;
62
+ const styleClone = Object . assign ( { } , style ) ;
63
+
64
+ if ( Platform . OS === 'ios' ) {
65
+ delete styleClone . fontWeight ;
66
+
67
+ if ( textStyle === 'xxl' ) {
68
+ delete emphasizedStyle . fontWeight ;
69
+ }
88
70
}
89
71
90
- return (
91
- < Text style = { finalStyles } { ...rest } > { children } </ Text >
92
- ) ;
72
+ return < Text style = { [ styles [ textStyle ] , emphasizedStyle , styleClone ] } { ...rest } > { children } </ Text > ;
93
73
} ;
94
74
95
75
BpkText . propTypes = {
96
76
children : PropTypes . node . isRequired ,
97
77
textStyle : PropTypes . oneOf ( TEXT_STYLES ) ,
78
+ emphasize : PropTypes . bool ,
98
79
style : Text . propTypes . style ,
99
80
} ;
100
81
101
82
BpkText . defaultProps = {
102
83
textStyle : 'base' ,
84
+ emphasize : false ,
103
85
style : { } ,
104
86
} ;
105
87
0 commit comments