forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.svelte.js
79 lines (73 loc) · 2.18 KB
/
client.svelte.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
import { createRawSnippet, hydrate, mount, unmount } from 'svelte';
/** @type {WrakMap<any, ReturnType<typeof createComponent>} */
const existingApplications = new WeakMap();
export default (element) => {
return async (Component, props, slotted, { client }) => {
if (!element.hasAttribute('ssr')) return;
let children = undefined;
let _$$slots = undefined;
let renderFns = {};
for (const [key, value] of Object.entries(slotted)) {
// Legacy slot support
_$$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>`,
}));
}
// @render support for Svelte ^5.0
if (key === 'default') {
renderFns.children = createRawSnippet(() => ({
render: () => `<astro-slot>${value}</astro-slot>`,
}));
} else {
renderFns[key] = createRawSnippet(() => ({
render: () => `<astro-slot name="${key}">${value}</astro-slot>`,
}));
}
}
const resolvedProps = {
...props,
children,
$$slots: _$$slots,
...renderFns,
};
if (existingApplications.has(element)) {
existingApplications.get(element).setProps(resolvedProps);
} else {
const component = createComponent(Component, element, resolvedProps, client !== 'only');
existingApplications.set(element, component);
element.addEventListener('astro:unmount', () => component.destroy(), { once: true });
}
};
};
/**
* @param {any} Component
* @param {HTMLElement} target
* @param {Record<string, any>} props
* @param {boolean} shouldHydrate
*/
function createComponent(Component, target, props, shouldHydrate) {
let propsState = $state(props);
const bootstrap = shouldHydrate ? hydrate : mount;
const component = bootstrap(Component, { target, props: propsState });
return {
setProps(newProps) {
Object.assign(propsState, newProps);
// Remove props in `propsState` but not in `newProps`
for (const key in propsState) {
if (!(key in newProps)) {
delete propsState[key];
}
}
},
destroy() {
unmount(component);
},
};
}