summaryrefslogtreecommitdiff
path: root/packages/integrations/solid/client.js
blob: d31c5cecd1ecb7386e89ae8927e63727caa5bd35 (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
import { sharedConfig } from 'solid-js';
import { hydrate, createComponent } from 'solid-js/web';

export default (element) => (Component, props, childHTML) => {
	// Prepare global object expected by Solid's hydration logic
	if (!window._$HY) {
		window._$HY = { events: [], completed: new WeakSet(), r: {} };
	}
	// Perform actual hydration
	let children;
	hydrate(
		() =>
			createComponent(Component, {
				...props,
				get children() {
					if (childHTML != null) {
						// hydrating
						if (sharedConfig.context) children = element.querySelector('astro-fragment');

						if (children == null) {
							children = document.createElement('astro-fragment');
							children.innerHTML = childHTML;
						}
					}
					return children;
				},
			}),
		element
	);
};