mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
a582cb6124
Co-authored-by: Martin Trapp <94928215+martrapp@users.noreply.github.com>
46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
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 });
|
|
};
|
|
};
|