/
styles.js
320 lines (276 loc) · 6.19 KB
/
styles.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import test from 'ava'
import {
space,
color,
width,
fontSize,
size,
gridGap,
gridRowGap,
gridColumnGap,
buttonStyle,
textStyle,
colorStyle,
} from '../src'
const theme = {
colors: {
blue: '#07c',
black: '#111',
},
}
test('returns color values from theme', t => {
const a = color({ theme, color: 'blue', bg: 'black' })
t.deepEqual(a, [{ color: '#07c' }, { backgroundColor: '#111' }])
})
test('returns raw color values', t => {
const a = color({
theme,
color: 'inherit',
bg: 'tomato',
})
t.deepEqual(a, [{ color: 'inherit' }, { backgroundColor: 'tomato' }])
})
test('backgroundColor prop overrides bg prop', t => {
const a = color({
backgroundColor: 'tomato',
bg: 'blue',
})
t.deepEqual(a, [{ backgroundColor: 'tomato' }])
})
test('returns a pixel font-size', t => {
const a = fontSize({ fontSize: 48 })
t.deepEqual(a, { fontSize: '48px' })
})
test('uses a default font-size scale', t => {
const a = fontSize({ fontSize: 2 })
t.deepEqual(a, { fontSize: '16px' })
})
test('returns a string font-size', t => {
const a = fontSize({ fontSize: '2em' })
t.deepEqual(a, { fontSize: '2em' })
})
test('returns a percentage based width', t => {
const a = width({ width: 1 / 2 })
t.deepEqual(a, { width: '50%' })
})
test('returns a pixel based width', t => {
const a = width({ width: 256 })
t.deepEqual(a, { width: '256px' })
})
test('returns a string width', t => {
const a = width({ width: 'auto' })
t.deepEqual(a, { width: 'auto' })
})
test('returns an array of style objects', t => {
const styles = space({
m: '4px',
})
t.deepEqual(styles, [{ margin: '4px' }])
})
test('returns 0 values', t => {
const styles = space({ m: 0 })
t.deepEqual(styles, [{ margin: 0 }])
})
test('returns negative pixel values', t => {
const styles = space({ m: -2 })
t.deepEqual(styles, [{ margin: '-8px' }])
})
test('returns negative em values', t => {
const styles = space({ m: '-16em' })
t.deepEqual(styles, [{ margin: '-16em' }])
})
test('returns negative theme values', t => {
const styles = space({
theme: {
space: [0, 4, 8],
},
m: -2,
})
t.deepEqual(styles, [{ margin: '-8px' }])
})
test('returns positive theme values', t => {
const styles = space({
theme: {
space: [0, '1em', '2em'],
},
m: 2,
})
t.deepEqual(styles, [{ margin: '2em' }])
})
test('returns responsive values', t => {
const styles = space({
m: [0, 2, 3],
})
t.deepEqual(styles, [
[
{ margin: 0 },
{ '@media screen and (min-width: 40em)': { margin: '8px' } },
{ '@media screen and (min-width: 52em)': { margin: '16px' } },
],
])
})
test('returns aliased values', t => {
const styles = space({
px: 2,
})
t.deepEqual(styles, [{ paddingLeft: '8px' }, { paddingRight: '8px' }])
})
test('returns string values from theme', t => {
const styles = space({
theme: {
space: [0, '1em'],
},
padding: 1,
})
t.deepEqual(styles, [{ padding: '1em' }])
})
test('returns negative string values from theme', t => {
const styles = space({
theme: {
space: [0, '1em'],
},
margin: -1,
})
t.deepEqual(styles, [{ margin: '-1em' }])
})
test('returns values from theme object', t => {
const styles = space({
theme: {
space: { sm: 1 },
},
margin: 'sm',
})
t.deepEqual(styles, [{ margin: '1px' }])
})
test('pl prop sets paddingLeft', t => {
const styles = space({ pl: 2 })
t.deepEqual(styles, [{ paddingLeft: '8px' }])
})
test('pl prop sets paddingLeft 0', t => {
const styles = space({ pl: 0 })
t.deepEqual(styles, [{ paddingLeft: 0 }])
})
test('px prop overrides pl prop', t => {
const styles = space({
pl: 1,
px: 2,
})
t.deepEqual(styles, [{ paddingLeft: '8px' }, { paddingRight: '8px' }])
})
test('py prop overrides pb prop', t => {
const styles = space({
pb: 1,
py: 2,
})
t.deepEqual(styles, [{ paddingTop: '8px' }, { paddingBottom: '8px' }])
})
test('mx prop overrides mr prop', t => {
const styles = space({
mr: 1,
mx: 2,
})
t.deepEqual(styles, [{ marginLeft: '8px' }, { marginRight: '8px' }])
})
test('my prop overrides mt prop', t => {
const styles = space({
mt: 1,
my: 2,
})
t.deepEqual(styles, [{ marginTop: '8px' }, { marginBottom: '8px' }])
})
test('margin overrides m prop', t => {
const styles = space({
m: 1,
margin: 2,
})
t.deepEqual(styles, [{ margin: '8px' }])
})
test('size returns width and height', t => {
const styles = size({
size: 4,
})
t.deepEqual(styles, [{ width: '4px' }, { height: '4px' }])
})
// grid
test('gridGap returns a scalar style', t => {
const a = gridGap({
theme: {
space: [0, 2, 4, 8],
},
gridGap: 3,
})
t.deepEqual(a, { gridGap: '8px' })
})
test('gridGap uses the default scale', t => {
const a = gridGap({
theme: {},
gridGap: 2,
})
t.deepEqual(a, { gridGap: '8px' })
})
test('gridRowGap returns a scalar style', t => {
const a = gridRowGap({
theme: {
space: [0, 2, 4, 8],
},
gridRowGap: 3,
})
t.deepEqual(a, { gridRowGap: '8px' })
})
test('gridRowGap uses the default scale', t => {
const a = gridRowGap({
theme: {},
gridRowGap: 2,
})
t.deepEqual(a, { gridRowGap: '8px' })
})
test('gridColumnGap returns a scalar style', t => {
const a = gridColumnGap({
theme: {
space: [0, 2, 4, 8],
},
gridColumnGap: 3,
})
t.deepEqual(a, { gridColumnGap: '8px' })
})
test('gridColumnGap uses the default scale', t => {
const a = gridColumnGap({
theme: {},
gridColumnGap: 2,
})
t.deepEqual(a, { gridColumnGap: '8px' })
})
test('textStyle prop returns theme.textStyles object', t => {
const a = textStyle({
theme: {
textStyles: {
heading: {
fontWeight: 'bold',
lineHeight: 1.25,
},
},
},
textStyle: 'heading',
})
t.deepEqual(a, {
fontWeight: 'bold',
lineHeight: 1.25,
})
})
test('colors prop returns theme.colorStyles object', t => {
const a = colorStyle({
theme: {
colorStyles: {
dark: {
color: '#fff',
backgroundColor: '#000',
},
},
},
colors: 'dark',
})
t.deepEqual(a, {
color: '#fff',
backgroundColor: '#000',
})
})