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

42 lines
1.1 KiB
JavaScript
Raw Normal View History

import { sharedConfig } from 'solid-js';
import { hydrate, render, createComponent } from 'solid-js/web';
2022-05-31 11:47:13 -05:00
export default (element) =>
(Component, props, slotted, { client }) => {
2022-05-31 11:47:13 -05:00
// Prepare global object expected by Solid's hydration logic
if (!window._$HY) {
window._$HY = { events: [], completed: new WeakSet(), r: {} };
}
if (!element.hasAttribute('ssr')) return;
2022-05-31 11:47:13 -05:00
const fn = client === 'only' ? render : hydrate;
let _slots = {};
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-05-31 11:47:13 -05:00
fn(
() =>
createComponent(Component, {
...props,
...slots,
2022-06-23 10:12:46 -05:00
children,
2022-05-31 11:47:13 -05:00
}),
element
);
};