-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (64 loc) · 2.12 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const createContainerForExample = () => {
const container = document.createElement("div");
container.classList.add("example");
return container;
};
const addContainerToDOM = (container) => {
document.body.appendChild(container);
};
const createCustomElement = ({ selector, element }) => {
customElements.define(selector, element);
const nodeElement = document.createElement(selector);
return nodeElement;
};
const appendCustomElementToContainer = (container, element) => {
container.appendChild(element);
};
const generateCustomElement = ({ selector, element }) => {
const customElement = createCustomElement({ selector, element });
const container = createContainerForExample();
appendCustomElementToContainer(container, customElement);
addContainerToDOM(container);
};
class CustomElement extends HTMLElement {
constructor() {
super();
}
// This is called when the component is inserted
connectedCallback() {
this.innerHTML = "<h2>Custom element</h2>";
}
}
class CustomShadowElement extends HTMLElement {
#shadow = null;
constructor() {
super();
this.#shadow = this.attachShadow({ mode: 'open' });
}
// This is called when the component is inserted
connectedCallback() {
const h2 = document.createElement("h2");
h2.textContent = "Custom Element";
this.#shadow.appendChild(h2);
}
}
class CustomShadowTemplateElement extends HTMLElement {
#shadow = null;
constructor() {
super();
this.#shadow = this.attachShadow({ mode: 'open' });
}
// This is called when the component is inserted
connectedCallback() {
const template = this.#getTemplate();
const clonedTemplate = document.importNode(template.content, true);
this.#shadow.appendChild(clonedTemplate);
}
#getTemplate() {
const template = document.getElementById("custom-shadow-template-el");
return template;
}
}
generateCustomElement({ selector: "custom-el", element: CustomElement });
generateCustomElement({ selector: "custom-shadow-el", element: CustomShadowElement });
generateCustomElement({ selector: "custom-shadow-template-el", element: CustomShadowTemplateElement });