2021-06-14 07:35:25 -05:00
|
|
|
import { h, Component as BaseComponent } from 'preact';
|
2021-05-26 13:30:22 -05:00
|
|
|
import { renderToString } from 'preact-render-to-string';
|
|
|
|
import StaticHtml from './static-html.js';
|
|
|
|
|
2021-05-28 17:19:40 -05:00
|
|
|
function check(Component, props, children) {
|
2021-06-14 07:36:32 -05:00
|
|
|
if (typeof Component !== 'function') return false;
|
2021-06-14 07:35:25 -05:00
|
|
|
|
2021-06-14 12:24:37 -05:00
|
|
|
if (Component.prototype != null && typeof Component.prototype.render === 'function') {
|
2021-06-14 07:35:25 -05:00
|
|
|
return BaseComponent.isPrototypeOf(Component);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { html } = renderToStaticMarkup(Component, props, children);
|
2021-07-21 18:10:03 -05:00
|
|
|
|
|
|
|
if (typeof html !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are edge cases (SolidJS) where Preact *might* render a string,
|
|
|
|
// but components would be <undefined></undefined>
|
|
|
|
|
|
|
|
return !/\<undefined\>/.test(html);
|
2021-05-26 13:31:27 -05:00
|
|
|
}
|
2021-05-26 13:30:22 -05:00
|
|
|
|
|
|
|
function renderToStaticMarkup(Component, props, children) {
|
2021-05-28 17:19:40 -05:00
|
|
|
const html = renderToString(h(Component, { ...props, children: h(StaticHtml, { value: children }), innerHTML: children }));
|
2021-05-26 13:30:22 -05:00
|
|
|
return { html };
|
|
|
|
}
|
|
|
|
|
2021-05-26 13:31:27 -05:00
|
|
|
export default {
|
|
|
|
check,
|
|
|
|
renderToStaticMarkup,
|
|
|
|
};
|