forked from styled-system/styled-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
532 lines (450 loc) · 11.5 KB
/
index.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import PropTypes from 'prop-types'
export const defaultBreakpoints = [40, 52, 64].map(n => n + 'em')
export const propType = PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
PropTypes.array,
PropTypes.object,
])
export const cloneFunction = fn => (...args) => fn(...args)
export const get = (obj, ...paths) => {
const value = paths.reduce((a, path) => {
if (is(a)) return a
const keys = typeof path === 'string' ? path.split('.') : [path]
return keys.reduce((a, key) => (a && is(a[key]) ? a[key] : null), obj)
}, null)
return is(value) ? value : paths[paths.length - 1]
}
export const themeGet = (path, fallback = null) => props =>
get(props.theme, path, fallback)
export const is = n => n !== undefined && n !== null
export const isObject = n => typeof n === 'object' && n !== null
export const num = n => typeof n === 'number' && !isNaN(n)
export const px = n => (num(n) && n !== 0 ? n + 'px' : n)
export const createMediaQuery = n => `@media screen and (min-width: ${px(n)})`
const getValue = (n, scale) => get(scale, n)
// loosely based on deepmerge package
export const merge = (a, b) => {
const result = {}
for (const key in a) {
result[key] = a[key]
}
for (const key in b) {
if (!a[key]) {
result[key] = b[key]
} else {
result[key] = merge(a[key], b[key])
}
}
return result
}
const mergeAll = (...args) => {
let result = {}
for (let i = 0; i < args.length; i++) {
result = merge(result, args[i])
}
return result
}
export const style = ({
prop,
cssProperty,
alias,
key,
transformValue = getValue,
scale: defaultScale = {},
}) => {
const property = cssProperty || prop
const func = props => {
const value = get(props, prop, alias, null)
if (!is(value)) return null
const scale = get(props.theme, key, defaultScale)
const createStyle = n =>
is(n)
? {
[property]: transformValue(n, scale),
}
: null
if (!isObject(value)) return createStyle(value)
const breakpoints = get(props.theme, 'breakpoints', defaultBreakpoints)
const styles = []
if (Array.isArray(value)) {
styles.push(createStyle(value[0]))
for (let i = 1; i < value.slice(0, breakpoints.length + 1).length; i++) {
const rule = createStyle(value[i])
if (rule) {
const media = createMediaQuery(breakpoints[i - 1])
styles.push({ [media]: rule })
}
}
} else {
for (let key in value) {
const breakpoint = breakpoints[key]
const media = createMediaQuery(breakpoint)
const rule = createStyle(value[key])
if (!breakpoint) {
styles.unshift(rule)
} else {
styles.push({ [media]: rule })
}
}
styles.sort()
}
return mergeAll(...styles)
}
func.propTypes = {
[prop]: cloneFunction(propType),
}
func.propTypes[prop].meta = {
prop,
themeKey: key,
}
if (alias) {
func.propTypes[alias] = cloneFunction(propType)
func.propTypes[alias].meta = {
prop: alias,
themeKey: key,
}
}
return func
}
export const compose = (...funcs) => {
const func = props => {
const n = funcs.map(fn => fn(props)).filter(Boolean)
return mergeAll(...n)
}
func.propTypes = {}
funcs.forEach(fn => {
func.propTypes = {
...func.propTypes,
...fn.propTypes,
}
})
return func
}
export const mapProps = mapper => func => {
const next = props => func(mapper(props))
for (const key in func) {
next[key] = func[key]
}
return next
}
export const variant = ({ key, prop = 'variant' }) => {
const fn = props => get(props.theme, [key, props[prop]].join('.'), null)
fn.propTypes = {
[prop]: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
}
return fn
}
// space
const spaceScale = [0, 4, 8, 16, 32, 64, 128, 256, 512]
const getSpace = (n, scale) => {
if (!num(n)) {
return px(get(scale, n, n))
}
const isNegative = n < 0
const absolute = Math.abs(n)
const value = get(scale, absolute)
if (!num(value)) {
return isNegative ? '-' + value : value
}
return px(value * (isNegative ? -1 : 1))
}
export const margin = style({
prop: 'margin',
alias: 'm',
key: 'space',
transformValue: getSpace,
scale: spaceScale,
})
export const marginTop = style({
prop: 'marginTop',
alias: 'mt',
key: 'space',
transformValue: getSpace,
scale: spaceScale,
})
export const marginBottom = style({
prop: 'marginBottom',
alias: 'mb',
key: 'space',
transformValue: getSpace,
scale: spaceScale,
})
export const marginLeft = style({
prop: 'marginLeft',
alias: 'ml',
key: 'space',
transformValue: getSpace,
scale: spaceScale,
})
export const marginRight = style({
prop: 'marginRight',
alias: 'mr',
key: 'space',
transformValue: getSpace,
scale: spaceScale,
})
export const padding = style({
prop: 'padding',
alias: 'p',
key: 'space',
transformValue: getSpace,
scale: spaceScale,
})
export const paddingTop = style({
prop: 'paddingTop',
alias: 'pt',
key: 'space',
transformValue: getSpace,
scale: spaceScale,
})
export const paddingBottom = style({
prop: 'paddingBottom',
alias: 'pb',
key: 'space',
transformValue: getSpace,
scale: spaceScale,
})
export const paddingLeft = style({
prop: 'paddingLeft',
alias: 'pl',
key: 'space',
transformValue: getSpace,
scale: spaceScale,
})
export const paddingRight = style({
prop: 'paddingRight',
alias: 'pr',
key: 'space',
transformValue: getSpace,
scale: spaceScale,
})
export const space = mapProps(props => ({
...props,
mt: is(props.my) ? props.my : props.mt,
mb: is(props.my) ? props.my : props.mb,
ml: is(props.mx) ? props.mx : props.ml,
mr: is(props.mx) ? props.mx : props.mr,
pt: is(props.py) ? props.py : props.pt,
pb: is(props.py) ? props.py : props.pb,
pl: is(props.px) ? props.px : props.pl,
pr: is(props.px) ? props.px : props.pr,
}))(
compose(
margin,
marginTop,
marginBottom,
marginLeft,
marginRight,
padding,
paddingTop,
paddingBottom,
paddingLeft,
paddingRight
)
)
// color
export const textColor = style({
prop: 'color',
key: 'colors',
})
export const backgroundColor = style({
prop: 'backgroundColor',
alias: 'bg',
key: 'colors',
})
export const color = compose(
textColor,
backgroundColor
)
// width
export const getWidth = (n, scale) => (!num(n) || n > 1 ? px(n) : n * 100 + '%')
export const width = style({
prop: 'width',
key: 'widths',
transformValue: getWidth,
})
// typography
export const getPx = (n, scale) => px(get(scale, n))
export const fontSize = style({
prop: 'fontSize',
key: 'fontSizes',
transformValue: getPx,
scale: [12, 14, 16, 20, 24, 32, 48, 64, 72],
})
export const fontFamily = style({
prop: 'fontFamily',
key: 'fonts',
})
export const fontWeight = style({
prop: 'fontWeight',
key: 'fontWeights',
})
export const lineHeight = style({
prop: 'lineHeight',
key: 'lineHeights',
})
export const textAlign = style({
prop: 'textAlign',
})
export const fontStyle = style({
prop: 'fontStyle',
})
export const letterSpacing = style({
prop: 'letterSpacing',
key: 'letterSpacings',
transformValue: getPx,
})
// layout
export const display = style({
prop: 'display',
})
export const maxWidth = style({
prop: 'maxWidth',
key: 'maxWidths',
transformValue: getPx,
})
export const minWidth = style({
prop: 'minWidth',
key: 'minWidths',
transformValue: getPx,
})
export const height = style({
prop: 'height',
key: 'heights',
transformValue: getPx,
})
export const maxHeight = style({
prop: 'maxHeight',
key: 'maxHeights',
transformValue: getPx,
})
export const minHeight = style({
prop: 'minHeight',
key: 'minHeights',
transformValue: getPx,
})
export const size = mapProps(props => ({
...props,
width: props.size,
height: props.size,
}))(
compose(
width,
height
)
)
export const verticalAlign = style({ prop: 'verticalAlign' })
// flexbox
export const alignItems = style({ prop: 'alignItems' })
export const alignContent = style({ prop: 'alignContent' })
export const justifyItems = style({ prop: 'justifyItems' })
export const justifyContent = style({ prop: 'justifyContent' })
export const flexWrap = style({ prop: 'flexWrap' })
export const flexBasis = style({ prop: 'flexBasis', transformValue: getWidth })
export const flexDirection = style({ prop: 'flexDirection' })
export const flex = style({ prop: 'flex' })
export const justifySelf = style({ prop: 'justifySelf' })
export const alignSelf = style({ prop: 'alignSelf' })
export const order = style({ prop: 'order' })
// grid
export const gridGap = style({
prop: 'gridGap',
key: 'space',
transformValue: getPx,
scale: spaceScale,
})
export const gridColumnGap = style({
prop: 'gridColumnGap',
key: 'space',
transformValue: getPx,
scale: spaceScale,
})
export const gridRowGap = style({
prop: 'gridRowGap',
key: 'space',
transformValue: getPx,
scale: spaceScale,
})
export const gridColumn = style({ prop: 'gridColumn' })
export const gridRow = style({ prop: 'gridRow' })
export const gridAutoFlow = style({ prop: 'gridAutoFlow' })
export const gridAutoColumns = style({ prop: 'gridAutoColumns' })
export const gridAutoRows = style({ prop: 'gridAutoRows' })
export const gridTemplateColumns = style({ prop: 'gridTemplateColumns' })
export const gridTemplateRows = style({ prop: 'gridTemplateRows' })
export const gridTemplateAreas = style({ prop: 'gridTemplateAreas' })
export const gridArea = style({ prop: 'gridArea' })
// borders
export const border = style({
prop: 'border',
key: 'borders',
})
export const borderWidth = style({
prop: 'borderWidth',
key: 'borderWidths',
transformValue: getPx,
})
export const borderStyle = style({
prop: 'borderStyle',
key: 'borderStyles',
})
export const borderColor = style({
prop: 'borderColor',
key: 'colors',
})
export const borderTop = style({
prop: 'borderTop',
key: 'borders',
})
export const borderRight = style({
prop: 'borderRight',
key: 'borders',
})
export const borderBottom = style({
prop: 'borderBottom',
key: 'borders',
})
export const borderLeft = style({
prop: 'borderLeft',
key: 'borders',
})
export const borderRadius = style({
prop: 'borderRadius',
key: 'radii',
transformValue: getPx,
})
export const borders = compose(
border,
borderTop,
borderRight,
borderBottom,
borderLeft,
borderWidth,
borderStyle,
borderColor,
borderRadius
)
export const boxShadow = style({
prop: 'boxShadow',
key: 'shadows',
})
export const opacity = style({ prop: 'opacity' })
export const overflow = style({ prop: 'overflow' })
// backgrounds
export const background = style({ prop: 'background' })
export const backgroundImage = style({ prop: 'backgroundImage' })
export const backgroundSize = style({ prop: 'backgroundSize' })
export const backgroundPosition = style({ prop: 'backgroundPosition' })
export const backgroundRepeat = style({ prop: 'backgroundRepeat' })
// position
export const position = style({ prop: 'position' })
export const zIndex = style({ prop: 'zIndex', key: 'zIndices' })
export const top = style({ prop: 'top', transformValue: getPx })
export const right = style({ prop: 'right', transformValue: getPx })
export const bottom = style({ prop: 'bottom', transformValue: getPx })
export const left = style({ prop: 'left', transformValue: getPx })
// variants
export const buttonStyle = variant({ key: 'buttons' })
export const textStyle = variant({ key: 'textStyles', prop: 'textStyle' })
export const colorStyle = variant({ key: 'colorStyles', prop: 'colors' })