forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-v5.js
45 lines (41 loc) · 1.15 KB
/
client-v5.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
import { createRawSnippet, hydrate, mount, unmount } from 'svelte';
const existingApplications = new WeakMap();
export default (element) => {
return async (Component, props, slotted, { client }) => {
if (!element.hasAttribute('ssr')) return;
let children = undefined;
let $$slots = undefined;
for (const [key, value] of Object.entries(slotted)) {
$$slots ??= {};
if (key === 'default') {
$$slots.default = true;
children = createRawSnippet(() => ({
render: () => `<astro-slot>${value}</astro-slot>`,
}));
} else {
$$slots[key] = createRawSnippet(() => ({
render: () => `<astro-slot name="${key}">${value}</astro-slot>`,
}));
}
}
const bootstrap = client !== 'only' ? hydrate : mount;
if (existingApplications.has(element)) {
existingApplications.get(element).$set({
...props,
children,
$$slots,
});
} else {
const component = bootstrap(Component, {
target: element,
props: {
...props,
children,
$$slots,
},
});
existingApplications.set(element, component);
element.addEventListener('astro:unmount', () => unmount(component), { once: true });
}
};
};