-
Notifications
You must be signed in to change notification settings - Fork 1
/
postcss.config.js
64 lines (53 loc) · 1.55 KB
/
postcss.config.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
module.exports = {
plugins: {
'postcss-import': {},
'postcss-mixins': {},
'postcss-nested': {},
'postcss-simple-vars': {},
'postcss-functions': { functions: { toRem, stripUnit, unit, fluid, alpha } },
'postcss-preset-env': {},
},
};
/* Postcss functions
------------------ */
function toRem(number) {
return `${stripUnit(number) / 16}rem`;
}
function toPx(number) {
return `${stripUnit(number) * 16}px`;
}
function stripUnit(value) {
let number = '';
for (let char of String(value)) {
if (!isNaN(char) || char == '.' || char == '-') number += char;
}
return Number(number);
}
function unit(value) {
let unit = '';
for (let char of String(value)) {
if (isNaN(char) && char != '.') unit += char;
}
return unit;
}
function fluid(minValue, maxValue, convertTo) {
let minVW = '400px'; // TODO: Convert to css custom properties
let maxVW = '760px';
if (convertTo == 'toRem') {
minValue = toRem(minValue);
maxValue = toRem(maxValue);
} else if (convertTo == 'toPx') {
minValue = toPx(minValue);
maxValue = toPx(maxValue);
}
if (unit(minValue) == 'rem') {
minVW = toRem(minVW);
maxVW = toRem(maxVW);
}
const targetValue = `calc(${minValue} + (${stripUnit(maxValue) - stripUnit(minValue)}) * ((100vw - ${minVW}) / (${stripUnit(maxVW) - stripUnit(minVW)})))`;
return `clamp(${minValue}, ${targetValue}, ${maxValue})`;
}
function alpha(color, alpha) {
const colorValues = color.match(/(?<=\().*(?=\))/)[0]; // Everything between brackets
return `rgb(${colorValues} / ${alpha})`;
}