-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (38 loc) · 1.31 KB
/
Copy pathindex.js
File metadata and controls
44 lines (38 loc) · 1.31 KB
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
// This is where the custom component is defined
try {
fetch("FloatingDropdown/template.html", { mode: "cors" })
.then((stream) => stream.text())
.then((text) => define(text));
} catch (error) {
console.error(
"[FloatingDropdown > index.js] unable to define FloatingDropdown element.\n",
error
);
}
/**
* Sets up the custom component as defined in the FloatingDropdown template
*
* @param {string} html the outerHTML attribute of the floating dropdown element defined in `index.html`
*/
function define(html) {
class FloatingDropdown extends HTMLElement {
constructor() {
self = super();
}
connectedCallback() {
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
const template = doc.getElementById("floating-dropdown");
//Custom element
const dropdown = template.content.cloneNode(true);
const shadowRoot = this.attachShadow({ mode: "open" });
shadowRoot.appendChild(dropdown);
// Apply external styles to the shadow dom
const styles = document.createElement("link");
styles.setAttribute("rel", "stylesheet");
styles.setAttribute("href", "FloatingDropdown/styles.css");
shadowRoot.appendChild(styles);
}
}
customElements.define("floating-dropdown", FloatingDropdown);
}