-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcss.js
33 lines (32 loc) · 974 Bytes
/
css.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
import { createTemplate } from "./util.js";
/**
* The 'css' tag function parses template literals which can contain expressions
* of the type 'string' or 'HTMLTemplateElement'.
* @param {TemplateStringsArray} strings
* @param {(string | HTMLTemplateElement | HTMLTemplateElement[])[]} values
* @returns {HTMLTemplateElement[]}
*/
export function css(strings, ...values) {
const cssTemplates = [];
cssTemplates.push(
createTemplate(`<style>${
values.reduce(
(acc, value, i) => {
if (value instanceof HTMLTemplateElement) {
cssTemplates.push(value);
return acc + strings[i + 1];
} else if (
Array.isArray(value)
) {
value.forEach((el) => cssTemplates.push(el));
return acc + strings[i + 1];
} else {
return acc + value + strings[i + 1];
}
},
strings[0],
)
}</style>`),
);
return cssTemplates;
}