2022-04-06 19:42:01 +00:00
|
|
|
import { sharedConfig } from 'solid-js';
|
2022-08-10 19:13:28 +00:00
|
|
|
import { createComponent, hydrate, render } from 'solid-js/web';
|
2022-03-18 22:35:45 +00:00
|
|
|
|
2022-08-10 19:10:31 +00:00
|
|
|
export default (element: HTMLElement) =>
|
|
|
|
(Component: any, props: any, slotted: any, { client }: { client: string }) => {
|
2022-05-31 16:47:13 +00:00
|
|
|
// Prepare global object expected by Solid's hydration logic
|
2022-08-10 19:10:31 +00:00
|
|
|
if (!(window as any)._$HY) {
|
|
|
|
(window as any)._$HY = { events: [], completed: new WeakSet(), r: {} };
|
2022-05-31 16:47:13 +00:00
|
|
|
}
|
|
|
|
if (!element.hasAttribute('ssr')) return;
|
2022-05-31 16:29:36 +00:00
|
|
|
|
2022-05-31 16:47:13 +00:00
|
|
|
const fn = client === 'only' ? render : hydrate;
|
|
|
|
|
2022-08-10 19:10:31 +00:00
|
|
|
let _slots: Record<string, any> = {};
|
2022-06-23 15:10:54 +00:00
|
|
|
if (Object.keys(slotted).length > 0) {
|
|
|
|
// hydrating
|
|
|
|
if (sharedConfig.context) {
|
|
|
|
element.querySelectorAll('astro-slot').forEach((slot) => {
|
|
|
|
_slots[slot.getAttribute('name') || 'default'] = slot.cloneNode(true);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
for (const [key, value] of Object.entries(slotted)) {
|
|
|
|
_slots[key] = document.createElement('astro-slot');
|
|
|
|
if (key !== 'default') _slots[key].setAttribute('name', key);
|
|
|
|
_slots[key].innerHTML = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const { default: children, ...slots } = _slots;
|
2022-08-10 19:10:31 +00:00
|
|
|
const renderId = element.dataset.solidRenderId;
|
2022-06-23 15:10:54 +00:00
|
|
|
|
2022-05-31 16:47:13 +00:00
|
|
|
fn(
|
|
|
|
() =>
|
|
|
|
createComponent(Component, {
|
|
|
|
...props,
|
2022-06-23 15:10:54 +00:00
|
|
|
...slots,
|
2022-06-23 15:12:46 +00:00
|
|
|
children,
|
2022-05-31 16:47:13 +00:00
|
|
|
}),
|
2022-08-10 19:10:31 +00:00
|
|
|
element,
|
|
|
|
{
|
2022-08-10 19:13:28 +00:00
|
|
|
renderId,
|
2022-08-10 19:10:31 +00:00
|
|
|
}
|
2022-05-31 16:47:13 +00:00
|
|
|
);
|
|
|
|
};
|