-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
client.ts
45 lines (40 loc) · 1.24 KB
/
client.ts
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
import { sharedConfig } from 'solid-js';
import { createComponent, hydrate, render } from 'solid-js/web';
export default (element: HTMLElement) =>
(Component: any, props: any, slotted: any, { client }: { client: string }) => {
// Prepare global object expected by Solid's hydration logic
if (!(window as any)._$HY) {
(window as any)._$HY = { events: [], completed: new WeakSet(), r: {} };
}
if (!element.hasAttribute('ssr')) return;
const fn = client === 'only' ? render : hydrate;
let _slots: Record<string, any> = {};
if (Object.keys(slotted).length > 0) {
// hydrating
if (sharedConfig.context) {
element.querySelectorAll('astro-slot').forEach((slot) => {
_slots[slot.getAttribute('name') || 'default'] = slot.cloneNode(true);
});
} else {
for (const [key, value] of Object.entries(slotted)) {
_slots[key] = document.createElement('astro-slot');
if (key !== 'default') _slots[key].setAttribute('name', key);
_slots[key].innerHTML = value;
}
}
}
const { default: children, ...slots } = _slots;
const renderId = element.dataset.solidRenderId;
fn(
() =>
createComponent(Component, {
...props,
...slots,
children,
}),
element,
{
renderId,
}
);
};