-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
space.js
133 lines (114 loc) Β· 2.76 KB
/
space.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
import { is, string, negative, getThemeValue } from '@xstyled/util'
import { style, themeGetter, compose } from '../style'
import { getPx } from './basics'
function toNegative(value) {
if (string(value)) return `-${value}`
return value * -1
}
// Getters
export const getSpace = themeGetter({
name: 'space',
key: 'space',
defaultVariants: [0, 4, 8, 16, 24, 48, 96, 144, 192, 240],
compose: getPx,
transform: (_, { rawValue, variants, props }) => {
if (string(rawValue)) {
const neg = rawValue.startsWith('-')
const absoluteValue = neg ? rawValue.substr(1) : rawValue
const variantValue = getThemeValue(props, absoluteValue, variants)
const value = is(variantValue) ? variantValue : absoluteValue
return neg ? toNegative(value) : value
}
const abs = Math.abs(rawValue)
const neg = negative(rawValue)
const value = is(variants[abs]) ? variants[abs] : abs
return neg ? toNegative(value) : value
},
})
// Styles
export const margin = style({
prop: ['margin', 'm'],
cssProperty: 'margin',
themeGet: getSpace,
})
export const marginTop = style({
prop: ['marginTop', 'mt'],
cssProperty: 'marginTop',
themeGet: getSpace,
})
export const marginRight = style({
prop: ['marginRight', 'mr'],
cssProperty: 'marginRight',
themeGet: getSpace,
})
export const marginBottom = style({
prop: ['marginBottom', 'mb'],
cssProperty: 'marginBottom',
themeGet: getSpace,
})
export const marginLeft = style({
prop: ['marginLeft', 'ml'],
cssProperty: 'marginLeft',
themeGet: getSpace,
})
export const mx = style({
prop: 'mx',
cssProperty: ['marginRight', 'marginLeft'],
themeGet: getSpace,
})
export const my = style({
prop: 'my',
cssProperty: ['marginTop', 'marginBottom'],
themeGet: getSpace,
})
export const padding = style({
prop: ['padding', 'p'],
cssProperty: 'padding',
themeGet: getSpace,
})
export const paddingTop = style({
prop: ['paddingTop', 'pt'],
cssProperty: 'paddingTop',
themeGet: getSpace,
})
export const paddingRight = style({
prop: ['paddingRight', 'pr'],
cssProperty: 'paddingRight',
themeGet: getSpace,
})
export const paddingBottom = style({
prop: ['paddingBottom', 'pb'],
cssProperty: 'paddingBottom',
themeGet: getSpace,
})
export const paddingLeft = style({
prop: ['paddingLeft', 'pl'],
cssProperty: 'paddingLeft',
themeGet: getSpace,
})
export const px = style({
prop: 'px',
cssProperty: ['paddingRight', 'paddingLeft'],
themeGet: getSpace,
})
export const py = style({
prop: 'py',
cssProperty: ['paddingTop', 'paddingBottom'],
themeGet: getSpace,
})
export const space = compose(
margin,
marginTop,
marginRight,
marginBottom,
marginLeft,
mx,
my,
padding,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft,
px,
py,
)