@@ -18,17 +18,20 @@ const aliases = {
18
18
mr : 'marginRight' ,
19
19
mb : 'marginBottom' ,
20
20
ml : 'marginLeft' ,
21
- mx : [ 'marginLeft' , 'marginRight' ] ,
22
- my : [ 'marginTop' , 'marginBottom' ] ,
23
- marginX : [ 'marginLeft' , 'marginRight' ] ,
24
- marginY : [ 'marginTop' , 'marginBottom' ] ,
21
+ mx : 'marginX' ,
22
+ my : 'marginY' ,
25
23
p : 'padding' ,
26
24
pt : 'paddingTop' ,
27
25
pr : 'paddingRight' ,
28
26
pb : 'paddingBottom' ,
29
27
pl : 'paddingLeft' ,
30
- px : [ 'paddingLeft' , 'paddingRight' ] ,
31
- py : [ 'paddingTop' , 'paddingBottom' ] ,
28
+ px : 'paddingX' ,
29
+ py : 'paddingY' ,
30
+ }
31
+
32
+ const directions = {
33
+ marginX : [ 'marginLeft' , 'marginRight' ] ,
34
+ marginY : [ 'marginTop' , 'marginBottom' ] ,
32
35
paddingX : [ 'paddingLeft' , 'paddingRight' ] ,
33
36
paddingY : [ 'paddingTop' , 'paddingBottom' ] ,
34
37
}
@@ -42,11 +45,15 @@ const scales = {
42
45
marginRight : 'space' ,
43
46
marginBottom : 'space' ,
44
47
marginLeft : 'space' ,
48
+ marginX : 'space' ,
49
+ marginY : 'space' ,
45
50
padding : 'space' ,
46
51
paddingTop : 'space' ,
47
52
paddingRight : 'space' ,
48
53
paddingBottom : 'space' ,
49
54
paddingLeft : 'space' ,
55
+ paddingX : 'space' ,
56
+ paddingY : 'space' ,
50
57
fontFamily : 'fonts' ,
51
58
fontSize : 'fontSizes' ,
52
59
fontWeight : 'fontWeights' ,
@@ -74,6 +81,26 @@ const scales = {
74
81
maxHeight : 'sizes' ,
75
82
}
76
83
84
+ const getMargin = ( scale , value ) => {
85
+ if ( typeof value !== 'number' || value >= 0 ) {
86
+ return get ( scale , value , value )
87
+ }
88
+ const absolute = Math . abs ( value )
89
+ const n = get ( scale , absolute , absolute )
90
+ if ( typeof n === 'string' ) return '-' + n
91
+ return n * - 1
92
+ }
93
+
94
+ const transforms = {
95
+ margin : getMargin ,
96
+ marginTop : getMargin ,
97
+ marginRight : getMargin ,
98
+ marginBottom : getMargin ,
99
+ marginLeft : getMargin ,
100
+ marginX : getMargin ,
101
+ marginY : getMargin ,
102
+ }
103
+
77
104
export const responsive = styles => theme => {
78
105
const next = { }
79
106
const breakpoints = get ( theme , 'breakpoints' , [ '40em' , '52em' , '64em' ] )
@@ -106,8 +133,8 @@ export const css = args => (props = {}) => {
106
133
const styles = responsive ( obj ) ( theme )
107
134
108
135
for ( const key in styles ) {
109
- const prop = aliases [ key ] || key
110
- const scaleName = scales [ prop ] || scales [ prop [ 0 ] ]
136
+ const prop = get ( aliases , key , key )
137
+ const scaleName = get ( scales , prop )
111
138
const scale = get ( theme , scaleName , get ( theme , prop , { } ) )
112
139
const x = styles [ key ]
113
140
const val = typeof x === 'function' ? x ( theme ) : x
@@ -120,10 +147,13 @@ export const css = args => (props = {}) => {
120
147
result [ prop ] = css ( val ) ( theme )
121
148
continue
122
149
}
123
- const value = get ( scale , val , val )
124
- if ( Array . isArray ( prop ) ) {
125
- for ( let i = 0 ; i < prop . length ; i ++ ) {
126
- result [ prop [ i ] ] = value
150
+ const transform = get ( transforms , prop , get )
151
+ const value = transform ( scale , val , val )
152
+ if ( directions [ prop ] ) {
153
+ // if (Array.isArray(prop)) {
154
+ const dirs = directions [ prop ]
155
+ for ( let i = 0 ; i < dirs . length ; i ++ ) {
156
+ result [ dirs [ i ] ] = value
127
157
}
128
158
} else {
129
159
result [ prop ] = value
0 commit comments