0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/svelte/client.js

46 lines
835 B
JavaScript
Raw Normal View History

const noop = () => {};
export default (target) => {
return (Component, props, slotted, { client }) => {
if (!target.hasAttribute('ssr')) return;
const slots = {};
for (const [key, value] of Object.entries(slotted)) {
slots[key] = createSlotDefinition(key, value);
}
try {
new Component({
target,
2022-06-23 10:12:46 -05:00
props: {
...props,
$$slots: slots,
2022-06-23 10:12:46 -05:00
$$scope: { ctx: [] },
},
hydrate: client !== 'only',
$$inline: true,
});
} catch (e) {}
};
};
function createSlotDefinition(key, children) {
2022-06-23 10:12:46 -05:00
return [
() => ({
// mount
m(target) {
2022-06-23 10:12:46 -05:00
target.insertAdjacentHTML(
'beforeend',
`<astro-slot${key === 'default' ? '' : ` name="${key}"`}>${children}</astro-slot>`
);
},
// create
c: noop,
// hydrate
l: noop,
// destroy
d: noop,
}),
2022-06-23 10:12:46 -05:00
noop,
noop,
];
}