astro/packages/integrations/preact/client.js

18 lines
650 B
JavaScript
Raw Permalink Normal View History

2022-08-30 15:17:44 +00:00
import { h } from 'preact';
import { createPortal } from 'preact/compat';
import StaticHtml from './static-html.js';
2022-06-23 15:12:46 +00:00
export default (element) =>
(Component, props, { default: children, ...slotted }) => {
if (!element.hasAttribute('ssr')) return;
2022-08-30 15:17:44 +00:00
const { addChild } = globalThis['@astrojs/preact'];
while (!!element.firstElementChild) {
element.firstElementChild.remove();
}
2022-06-23 15:12:46 +00:00
for (const [key, value] of Object.entries(slotted)) {
props[key] = h(StaticHtml, { value, name: key });
}
2022-08-30 15:17:44 +00:00
const Portal = createPortal(h(Component, props, children != null ? h(StaticHtml, { value: children }) : children), element)
addChild(Portal);
2022-06-23 15:12:46 +00:00
};