-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (61 loc) · 1.78 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
var postcss = require('postcss');
var reduceFunctionCall = require('reduce-function-call');
var helpers = require('postcss-message-helpers');
var colorScale = require('color-scale');
module.exports = postcss.plugin('postcss-color-scale', function(opts) {
opts = opts || {};
var color = opts.color || 'grey';
var variance = opts.variance || 0;
var colors = {};
return function(css) {
var cs;
var declarations = [];
var colorKey;
css.walkDecls(function transformDecl(decl) {
if (!decl.value) {
return;
}
if (decl.parent.selector === ':root') {
if (decl.prop === '--cs-variance') {
variance = decl.value;
} else {
colorKey = decl.prop.split('--cs-')[1];
if (colorKey) {
colors[colorKey] = {
value: decl.value
};
}
}
}
if (decl.value.indexOf('cs(') !== -1) {
declarations.push(decl);
}
});
Object.keys(colors).forEach(function(key) {
var decl = colors[key];
decl.func = colorScale({
variance: variance,
color: decl.value || color
});
});
declarations.forEach(function(decl) {
decl.value = helpers.try(function transformCS() {
return reduceFunctionCall(decl.value, 'cs', function reduceCS(body) {
if (!body) {
return false;
}
var parts = body.split(',');
var value = parseInt(parts[0], 10);
// Defaults to the --cs-color variable
var name = parts[1] ? parts[1].trim() : 'color';
var colorObj = colors[name];
if (!colorObj) {
return 'cs(' + body + ')';
}
cs = colorObj.func;
return cs(value);
});
}, decl.source);
});
};
});