-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx-style-envvar.js
38 lines (35 loc) · 1.09 KB
/
x-style-envvar.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
(() => {
/**
* x-style plugin for environment variables in the css.
* To define an environment variable, set xstyle.env to an object with:
* xstyle.env = { "--my-var": "red" };
* To use an environment variable, use the env() function in the css:
* <div x-style="color: env(--my-var, blue);"></div>
*/
var xstyle = window.xstyle;
var doEnvVars = (css) => {
var env = xstyle.env;
var bits = [...css.split(/\senv\((--[^),]+)/g)];
var end, variable, fallback;
while (bits.length > 1) {
end = bits.pop();
variable = bits.pop().trim();
fallback;
if (end[0] === ")") {
end = end.slice(1);
} else if (end[0] === ",") {
[fallback, end] = end.split(")", 2);
}
if (env[variable] !== undefined) {
bits[bits.length - 1] += ` ${env[variable]}${end}`;
} else if (fallback) {
bits[bits.length - 1] += ` ${fallback.slice(1).trim()}${end}`;
} else {
bits[bits.length - 1] += ` ${end}`;
}
}
return bits[0]
}
xstyle.env = xstyle.env || {};
xstyle.pre.push(doEnvVars);
})();