2024-07-18 08:32:39 -05:00
|
|
|
import { createRawSnippet, hydrate, mount, unmount } from 'svelte';
|
2023-11-15 10:40:23 -05:00
|
|
|
|
|
|
|
export default (element) => {
|
2024-02-26 03:10:47 -05:00
|
|
|
return async (Component, props, slotted, { client }) => {
|
2023-11-15 10:40:23 -05:00
|
|
|
if (!element.hasAttribute('ssr')) return;
|
|
|
|
|
|
|
|
let children = undefined;
|
|
|
|
let $$slots = undefined;
|
|
|
|
for (const [key, value] of Object.entries(slotted)) {
|
2024-07-18 08:32:39 -05:00
|
|
|
$$slots ??= {};
|
2023-11-15 10:40:23 -05:00
|
|
|
if (key === 'default') {
|
2024-07-18 08:32:39 -05:00
|
|
|
$$slots.default = true;
|
|
|
|
children = createRawSnippet(() => ({
|
|
|
|
render: () => `<astro-slot>${value}</astro-slot>`,
|
|
|
|
}));
|
2023-11-15 10:40:23 -05:00
|
|
|
} else {
|
2024-07-18 08:32:39 -05:00
|
|
|
$$slots[key] = createRawSnippet(() => ({
|
|
|
|
render: () => `<astro-slot name="${key}">${value}</astro-slot>`,
|
|
|
|
}));
|
2023-11-15 10:40:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 03:10:47 -05:00
|
|
|
const bootstrap = client !== 'only' ? hydrate : mount;
|
|
|
|
|
|
|
|
const component = bootstrap(Component, {
|
2023-11-15 10:40:23 -05:00
|
|
|
target: element,
|
|
|
|
props: {
|
|
|
|
...props,
|
|
|
|
children,
|
|
|
|
$$slots,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-02-26 03:10:47 -05:00
|
|
|
element.addEventListener('astro:unmount', () => unmount(component), { once: true });
|
2023-11-15 10:40:23 -05:00
|
|
|
};
|
|
|
|
};
|