-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
61 lines (50 loc) · 1.38 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
var x = require('xtend')
var gr8util = require('gr8-util')
var gr8utils = require('./utils')
module.exports = gr8
var defaults = {
breakpoints: {
sm: 768,
md: 1024,
lg: 1280
},
breakpointSelector: 'attribute',
utils: [],
exclude: []
}
function gr8 (options) {
options = x(defaults, options)
var cssString = ''
cssString += defaultUtils(options)
cssString += customUtils(options.utils)
Object.keys(options.breakpoints).forEach(function (key) {
var selector = breakpointSelectorShortcut(options.breakpointSelector)(key)
cssString += `\n@media ${breakpointShortcut(options.breakpoints[key])} {\n`
cssString += defaultUtils(options, { selector: selector })
cssString += customUtils(options.utils, { selector: selector })
cssString += '\n}'
})
return cssString
}
function defaultUtils (defaults, options) {
options = options || {}
return gr8utils(x(defaults, options))
}
function customUtils (utils, options) {
options = options || {}
return utils.map(function (util) {
return gr8util(x(util, options))
}).join('\n')
}
function breakpointShortcut (value) {
return Number.isInteger(value)
? `(min-width:${value}px)`
: value
}
function breakpointSelectorShortcut (selector) {
return selector === 'attribute'
? key => s => `[${key}~="${s}"]`
: selector === 'class'
? key => s => `.${key}-${s}`
: selector
}