2023-11-15 10:40:23 -05:00
|
|
|
import { mount } from 'svelte';
|
2023-12-07 09:47:04 -05:00
|
|
|
import { add_snippet_symbol } from 'svelte/internal';
|
|
|
|
|
|
|
|
// 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) => {
|
|
|
|
return async (Component, props, slotted) => {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const [, destroy] = mount(Component, {
|
|
|
|
target: element,
|
|
|
|
props: {
|
|
|
|
...props,
|
|
|
|
children,
|
|
|
|
$$slots,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
element.addEventListener('astro:unmount', () => destroy(), { once: true });
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|