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
80
81
82
83
84
85
|
import { createRawSnippet, hydrate, mount, unmount } from 'svelte';
const existingApplications = new WeakMap<HTMLElement, ReturnType<typeof createComponent>>();
export default (element: HTMLElement) => {
return async (
Component: any,
props: Record<string, any>,
slotted: Record<string, any>,
{ client }: Record<string, string>,
) => {
if (!element.hasAttribute('ssr')) return;
let children = undefined;
let _$$slots: Record<string, any> | undefined = undefined;
let renderFns: Record<string, any> = {};
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 });
}
};
};
function createComponent(
Component: any,
target: HTMLElement,
props: Record<string, any>,
shouldHydrate: boolean,
) {
let propsState = $state(props);
const bootstrap = shouldHydrate ? hydrate : mount;
if (!shouldHydrate) {
target.innerHTML = '';
}
const component = bootstrap(Component, { target, props: propsState });
return {
setProps(newProps: Record<string, any>) {
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);
},
};
}
|