-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag-html.mini-example.js
44 lines (38 loc) · 1.54 KB
/
tag-html.mini-example.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
// shim that allows isoMorphic class myElement extends HTMLElement
// const HTMLElement = globalThis.HTMLElement || class HTMLElement {};
// Would only work with HTML element would require above shim
// const htmlElementToString = exp => exp instanceof HTMLElement ? Object.assign(exp,{toString(){return this.outerHTML}}): exp;
const htmlElementToString = exp => exp.outerHTML ? exp.outerHTML : exp;
const asyncHtml = async (strings=[""],...args) => {
const resolvedArgs = await Promise.all(args);
return strings.reduce((resultString, currentString, i) => {
const exp = resolvedArgs[i] !== undefined ? resolvedArgs[i] : "";
const processedExp = `${htmlElementToString(exp)}`;
return resultString + currentString + processedExp;
}, "");
}
const html = (strings=[""],...args) => strings.reduce((resultString, currentString, i) => {
const exp = args[i] !== undefined ? args[i] : "";
const processedExp = htmlElementToString(exp);
// String + exp auto calls toString on exp like `${exp}`
return resultString + currentString + processedExp;
}, "");
export const productListComponent = {
outerHTML: `<div class="col-8" id="product-list">
Produkt Name
</div>`,
toString() {
return this.outerHTML;
}
};
class ProductListComponent extends HTMLElement {
connectedCallback() {
Object.assign(this,{
classList: "col-8", id: "product-list",
innerHTML: `Produkt Name`,
});
}
toString() {
return this.outerHTML;
}
};