-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
209 lines (176 loc) · 4.96 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
const { create } = require('nano-css')
const { addon: cache } = require('nano-css/addon/cache')
const { addon: nesting } = require('nano-css/addon/nesting')
const { addon: keyframes } = require('nano-css/addon/keyframes')
const { addon: rule } = require('nano-css/addon/rule')
const { addon: globalAddon } = require('nano-css/addon/global')
const { addon: hydrate } = require('nano-css/addon/hydrate')
const defaultCssProps = require('./properties')
function obj (o, theme) {
return typeof o === 'function' ? o(theme) : o
}
/**
* Expand macros and variants, then expand all shorthand props.
*
* Returns a hypostyle object
*/
function explode (props, theme) {
const styles = {}
// expand macros and variants, copy other props
for (const prop of Object.keys(props)) {
// macro exists AND prop is true
if (theme.macros[prop] && (props[prop] === true || props[prop] === false)) {
if (props[prop] === true) Object.assign(styles, theme.macros[prop])
} else if (theme.variants[prop]) {
Object.assign(styles, theme.variants[prop][props[prop]])
} else {
styles[prop] = props[prop]
}
}
// recursively expand all shorthands
for (const prop of Object.keys(styles)) {
const value = styles[prop]
// Could be nested object, ignore responsive array/object syntax
if (
typeof value === 'object' &&
!Array.isArray(value) &&
!/^\d/.test(Object.keys(value)[0])
) {
styles[prop] = explode(value, theme)
continue
}
if (theme.shorthands[prop]) {
for (const p of [].concat(theme.shorthands[prop])) {
styles[p] = value
}
delete styles[prop] // remove shorthand key
} else {
styles[prop] = value
}
}
return styles
}
/**
* Accepts a hypostyle object and converts it to a CSS object intelligible by
* any CSS-in-JS library that supports objects.
*/
function style (props, theme) {
const styles = {}
for (const prop of Object.keys(props)) {
const value = props[prop]
const { token, unit } = theme.properties[prop] || {}
const tokens = theme.tokens[token]
if (typeof value === 'object' && !Array.isArray(value)) {
const keyIndicies = Object.keys(value)
// convert responsive object to array syntax
if (/^\d/.test(keyIndicies[0])) {
const arr = []
for (const i of keyIndicies) {
arr[i] = value[i]
}
props[prop] = arr
} else {
styles[prop] = style(value, theme)
continue // continue main loop
}
}
const values = [].concat(props[prop])
for (let i = 0; i < values.length; i++) {
const value = values[i]
const token = tokens ? tokens[value] || value : value
const unitValue = unit ? unit(token) : token
// drop undefined values, all others pass through
if (unitValue === undefined) continue
let s = styles
const breakpoint = theme.breakpoints[i - 1]
if (breakpoint) {
const media = `@media (min-width: ${breakpoint})`
// drop down a level (into breakpoint)
s = styles[media] = styles[media] || {}
}
// if someone passes a breakpoint that doesn't exist
if (!breakpoint && i > 0) continue
s[prop] = unitValue
}
}
return styles
}
/**
* Separates style props from everything else
*/
function pick (props, theme) {
const styles = {}
const extra = {}
for (const prop of Object.keys(props)) {
if (
theme.macros[prop] ||
theme.variants[prop] ||
theme.shorthands[prop] ||
theme.properties[prop]
) {
styles[prop] = props[prop]
} else {
extra[prop] = props[prop]
}
}
return {
styles,
props: extra
}
}
function createNano ({ addons, prefix = '_' }) {
const nano = create({
pfx: prefix,
sh: typeof document === 'object' ? document.getElementById('hypo') : null
})
addons.map(a => a(nano))
return nano
}
function hypostyle (theme = {}, config = {}) {
const t = {
tokens: {},
breakpoints: [],
macros: {},
variants: {},
...theme,
properties: {
...defaultCssProps,
...(theme.properties || {})
},
shorthands: {
...(theme.shorthands || {})
}
}
const addons = [cache, nesting, keyframes, rule, globalAddon, hydrate].concat(
config.addons || []
)
let nano = createNano({ addons, prefix: config.prefix })
return {
explode (props) {
return explode(obj(props, t), t)
},
style (props) {
return style(explode(obj(props, t), t), t)
},
css (props) {
const styles = style(explode(obj(props, t), t), t)
return Object.keys(styles).length ? nano.rule(styles) : ''
},
pick (props) {
return pick(props, t)
},
injectGlobal (props) {
nano.global(style(explode(obj(props, t), t), t))
},
keyframes: nano.keyframes,
flush () {
const raw = nano.raw
nano = createNano({ addons })
return raw
},
get theme () {
return t
}
}
}
module.exports = { hypostyle }