-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
57 lines (53 loc) · 2.54 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
const postcss = require('postcss')
const valueParser = require('postcss-value-parser')
const getColorStops = require('./lib/colorStops.js')
const getCoordinates = require('./lib/coordinates.js')
const helpers = require('./lib/helpers.js')
const defaultPrecision = 0.1
const defaultAlphaDecimals = 3
/**
* The easing gradient function is a postcss plugin which supports the in /.helpers mentioned gradient types.
*/
module.exports = postcss.plugin('easing-gradient', (opts) => {
return function (css) {
const options = opts || {}
css.walkRules((rule) => {
rule.walkDecls((decl) => {
// If declaration value contains a linear-gradient.
if (decl.value.includes('linear-gradient')) {
// Parse the declaration and walk through the nodes — https://github.com/TrySound/postcss-value-parser.
const parsedValue = valueParser(decl.value)
parsedValue.walk((node) => {
// Only modify gradient as the value can contain more e.g. 'linear-gradient(black, pink) center'.
if (node.value === 'linear-gradient') {
// Get a sensible array of gradient parameters where e.g. a function is split into multiple array items
node.nodes = helpers.divToSemiColon(node.nodes)
let gradientParams = valueParser.stringify(node.nodes).split(';').map(str => str.trim())
// Loop and do magic whenever we encounter a timing function
gradientParams = gradientParams.map((param, i) => {
if (helpers.isTimingFunction(param)) {
try {
const colors = [gradientParams[i - 1], gradientParams[i + 1]]
const coordinates = getCoordinates(param, options.precision || defaultPrecision)
const colorStops = getColorStops(colors, coordinates, options.alphaDecimals || defaultAlphaDecimals)
param = colorStops.join(', ')
} catch (error) {
console.log(`While looking at ${param} we got:`, error)
}
}
return param
})
// Filter out empty params (because linear returns an empty array of color stops)
gradientParams = gradientParams.filter(param => param !== '')
// Update node
node.type = 'word'
node.value = `linear-gradient(${gradientParams.join(', ')})`
}
})
// Update our declaration value
decl.value = parsedValue.toString()
}
})
})
}
})