2021-06-14 07:35:25 -05:00
|
|
|
import { Component as BaseComponent, createElement as h } from 'react';
|
2021-05-26 13:30:22 -05:00
|
|
|
import { renderToStaticMarkup as renderToString } from 'react-dom/server.js';
|
|
|
|
import StaticHtml from './static-html.js';
|
|
|
|
|
2021-06-14 07:35:25 -05:00
|
|
|
const reactTypeof = Symbol.for('react.element');
|
|
|
|
|
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 07:36:32 -05:00
|
|
|
if (typeof Component.prototype.render === 'function') {
|
2021-06-14 07:35:25 -05:00
|
|
|
return BaseComponent.isPrototypeOf(Component);
|
|
|
|
}
|
|
|
|
|
|
|
|
let error = null;
|
|
|
|
let isReactComponent = false;
|
|
|
|
function Tester(...args) {
|
|
|
|
try {
|
|
|
|
const vnode = Component(...args);
|
2021-06-14 07:36:32 -05:00
|
|
|
if (vnode && vnode['$$typeof'] === reactTypeof) {
|
2021-06-14 07:35:25 -05:00
|
|
|
isReactComponent = true;
|
|
|
|
}
|
2021-06-14 07:36:32 -05:00
|
|
|
} catch (err) {
|
2021-06-14 07:35:25 -05:00
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return h('div');
|
|
|
|
}
|
|
|
|
|
|
|
|
renderToStaticMarkup(Tester, props, children);
|
|
|
|
|
2021-06-14 07:36:32 -05:00
|
|
|
if (error) {
|
2021-06-14 07:35:25 -05:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return isReactComponent;
|
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,
|
2021-05-26 13:30:22 -05:00
|
|
|
};
|