summaryrefslogtreecommitdiff
path: root/packages/integrations/svelte/client.js
blob: 36a8e8de16e33ca8b5172a27c4e7a28cae8aeb5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const noop = () => {};

export default (target) => {
	return (Component, props, slotted, { client }) => {
		if (!target.hasAttribute('ssr')) return;
		delete props['class'];
		const slots = {};
		for (const [key, value] of Object.entries(slotted)) {
			slots[key] = createSlotDefinition(key, value);
		}
		try {
			new Component({
				target,
				props: { 
					...props,
					$$slots: slots,
					$$scope: { ctx: [] }
				},
				hydrate: client !== 'only',
				$$inline: true,
			});
		} catch (e) {}
	};
};

function createSlotDefinition(key, children) {
  return [
    () => ({
			// mount
			m(target) {
				target.insertAdjacentHTML('beforeend', `<astro-slot${key === 'default' ? '' : ` name="${key}"`}>${children}</astro-slot>`)
			},
			// create
			c: noop,
			// hydrate
			l: noop,
			// destroy
			d: noop,
		}),
    noop,
    noop,
  ]
}