-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
56 lines (42 loc) · 1.31 KB
/
client.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
/**
* @param {HTMLElement} element
*/
export default (element) => async (Component, props, slotted) => {
let component = element.firstElementChild;
if (!Component?.props) return;
/**
* reference from lit
* @link { https://github.com/withastro/astro/blob/main/packages/integrations/lit/src/client.ts}
*/
const isClientOnly = element.getAttribute("client") === "only";
if (isClientOnly) {
component = new Component();
Object.entries(slotted).forEach(([slot, html]) => {
const template = document.createElement("template");
template.innerHTML = html;
template.content
.querySelectorAll("[data-hydrate]")
.forEach((el) => el.removeAttribute("data-hydrate"));
const { children } = template.content;
if (slot != "default") {
for (let i = 0; i < children.length; i++) {
children[i].setAttribute("slot", slot);
}
}
component.appendChild(template.content);
});
element.appendChild(component);
for (const prop in props) {
if (!(prop in Component.prototype)) {
component.setAttribute(prop, props[prop]);
}
}
}
if (!component) return;
for (const prop in props) {
if (prop in Component.prototype) {
component[prop] = props[prop];
}
}
component.removeAttribute("slot");
};