2024-02-26 03:10:47 -05:00
|
|
|
import { hydrate, mount, unmount } from 'svelte';
|
2024-04-02 08:58:39 -05:00
|
|
|
import { add_snippet_symbol } from 'svelte/internal/client';
|
2023-12-07 09:47:04 -05:00
|
|
|
|
|
|
|
// Allow a slot to be rendered as a snippet (dev validation only)
|
|
|
|
const tagSlotAsSnippet = import.meta.env.DEV ? add_snippet_symbol : (s) => s;
|
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)) {
|
|
|
|
if (key === 'default') {
|
|
|
|
children = createSlotDefinition(key, value);
|
|
|
|
} else {
|
|
|
|
$$slots ??= {};
|
|
|
|
$$slots[key] = createSlotDefinition(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function createSlotDefinition(key, children) {
|
|
|
|
/**
|
|
|
|
* @param {Comment} $$anchor A comment node for slots in Svelte 5
|
|
|
|
*/
|
2023-12-07 09:47:04 -05:00
|
|
|
const fn = ($$anchor, _$$slotProps) => {
|
2023-11-15 10:40:23 -05:00
|
|
|
const parent = $$anchor.parentNode;
|
|
|
|
const el = document.createElement('div');
|
|
|
|
el.innerHTML = `<astro-slot${
|
|
|
|
key === 'default' ? '' : ` name="${key}"`
|
|
|
|
}>${children}</astro-slot>`;
|
|
|
|
parent.insertBefore(el.children[0], $$anchor);
|
|
|
|
};
|
2023-12-07 09:47:04 -05:00
|
|
|
return tagSlotAsSnippet(fn);
|
2023-11-15 10:40:23 -05:00
|
|
|
}
|