1010 */
1111
1212import normalizeValue from './normalizeValue' ;
13+ import resolveBoxShadow from './resolveBoxShadow' ;
14+ import resolveTextShadow from './resolveTextShadow' ;
15+ import resolveTransform from './resolveTransform' ;
1316
1417const emptyObject = { } ;
1518const styleShortFormProperties = {
@@ -28,46 +31,83 @@ const styleShortFormProperties = {
2831 writingDirection : [ 'direction' ]
2932} ;
3033
31- const alphaSort = ( arr ) => arr . sort ( ( a , b ) => {
34+ const alphaSortProps = ( propsArray ) => propsArray . sort ( ( a , b ) => {
3235 if ( a < b ) { return - 1 ; }
3336 if ( a > b ) { return 1 ; }
3437 return 0 ;
3538} ) ;
3639
37- const createStyleReducer = ( originalStyle ) => {
38- const originalStyleProps = Object . keys ( originalStyle ) ;
40+ const expandStyle = ( style ) => {
41+ if ( ! style ) { return emptyObject ; }
42+ const styleProps = Object . keys ( style ) ;
43+ const sortedStyleProps = alphaSortProps ( styleProps ) ;
44+ let hasResolvedBoxShadow = false ;
45+ let hasResolvedTextShadow = false ;
3946
40- return ( style , prop ) => {
41- const value = normalizeValue ( prop , originalStyle [ prop ] ) ;
42- const longFormProperties = styleShortFormProperties [ prop ] ;
47+ const reducer = ( resolvedStyle , prop ) => {
48+ const value = normalizeValue ( prop , style [ prop ] ) ;
49+ if ( value == null ) { return resolvedStyle ; }
4350
44- // React Native treats `flex:1` like `flex:1 1 auto`
45- if ( prop === 'flex' ) {
46- style . flexGrow = value ;
47- style . flexShrink = 1 ;
48- style . flexBasis = 'auto' ;
49- // React Native accepts 'center' as a value
50- } else if ( prop === 'textAlignVertical' ) {
51- style . verticalAlign = ( value === 'center' ? 'middle' : value ) ;
52- } else if ( longFormProperties ) {
53- longFormProperties . forEach ( ( longForm , i ) => {
54- // the value of any longform property in the original styles takes
55- // precedence over the shortform's value
56- if ( originalStyleProps . indexOf ( longForm ) === - 1 ) {
57- style [ longForm ] = value ;
51+ switch ( prop ) {
52+ // ignore React Native styles
53+ case 'elevation' :
54+ case 'resizeMode' : {
55+ break ;
56+ }
57+ case 'flex' : {
58+ resolvedStyle . flexGrow = value ;
59+ resolvedStyle . flexShrink = 1 ;
60+ resolvedStyle . flexBasis = 'auto' ;
61+ break ;
62+ }
63+ case 'shadowColor' :
64+ case 'shadowOffset' :
65+ case 'shadowOpacity' :
66+ case 'shadowRadius' : {
67+ if ( ! hasResolvedBoxShadow ) {
68+ resolveBoxShadow ( resolvedStyle , style ) ;
5869 }
59- } ) ;
60- } else {
61- style [ prop ] = value ;
70+ hasResolvedBoxShadow = true ;
71+ break ;
72+ }
73+ case 'textAlignVertical' : {
74+ resolvedStyle . verticalAlign = ( value === 'center' ? 'middle' : value ) ;
75+ break ;
76+ }
77+ case 'textShadowColor' :
78+ case 'textShadowOffset' :
79+ case 'textShadowRadius' : {
80+ if ( ! hasResolvedTextShadow ) {
81+ resolveTextShadow ( resolvedStyle , style ) ;
82+ }
83+ hasResolvedTextShadow = true ;
84+ break ;
85+ }
86+ case 'transform' : {
87+ resolveTransform ( resolvedStyle , style ) ;
88+ break ;
89+ }
90+ default : {
91+ const longFormProperties = styleShortFormProperties [ prop ] ;
92+ if ( longFormProperties ) {
93+ longFormProperties . forEach ( ( longForm , i ) => {
94+ // the value of any longform property in the original styles takes
95+ // precedence over the shortform's value
96+ if ( styleProps . indexOf ( longForm ) === - 1 ) {
97+ resolvedStyle [ longForm ] = value ;
98+ }
99+ } ) ;
100+ } else {
101+ resolvedStyle [ prop ] = value ;
102+ }
103+ }
62104 }
63- return style ;
105+
106+ return resolvedStyle ;
64107 } ;
65- } ;
66108
67- const expandStyle = ( style = emptyObject ) => {
68- const sortedStyleProps = alphaSort ( Object . keys ( style ) ) ;
69- const styleReducer = createStyleReducer ( style ) ;
70- return sortedStyleProps . reduce ( styleReducer , { } ) ;
109+ const resolvedStyle = sortedStyleProps . reduce ( reducer , { } ) ;
110+ return resolvedStyle ;
71111} ;
72112
73113module . exports = expandStyle ;
0 commit comments