Skip to content

Commit

Permalink
fix: improve React node resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
luwes committed Jul 4, 2023
1 parent a7c78c4 commit 7005536
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
16 changes: 14 additions & 2 deletions src/react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,27 @@ function resolve(children, result = []) {

for (let node of children) {

if (typeof node.type === 'string') {
if (typeof node === 'string') {
result.push(node);

} else if (typeof node.type === 'string') {
let copy = { ...node, props: { ...node.props } };
if (copy.props.children) copy.props.children = [];

resolve(node.props.children, copy.props.children);
result.push(copy);

} else if (typeof node.type === 'function') {
resolve(node.type(node.props), result);

if (/^\s*class\s+/.test(node.type.toString())) {
// Class component
const comp = new node.type(node.props);
const vnode = comp.render();
resolve(vnode, result);
} else {
// Function component
resolve(node.type(node.props), result);
}

} else if (typeof node.type === 'object' && typeof node.type.render === 'function') {
resolve(node.type.render(node.props), result);
Expand Down
35 changes: 18 additions & 17 deletions src/react/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,26 @@ function renderChildren(children, domNode) {

if (dom.shadowRoot) {

for (let style of dom.shadowRoot.querySelectorAll('style')) {
style.textContent = minimizeCss(style.textContent);
}

const templateShadowRoot = createElement('template', {
shadowrootmode: dom.shadowRoot.mode ?? 'open',
dangerouslySetInnerHTML: {
__html: dom.shadowRoot.innerHTML,
},
});

if (node.props.children) {
node.props.children.unshift(templateShadowRoot);
} else {
node.props.children = [templateShadowRoot];
}

// Some web components defer updates. Add a timeout larger than micro task.
setTimeout(() => {

for (let style of dom.shadowRoot.querySelectorAll('style')) {
style.textContent = minimizeCss(style.textContent);
}

const templateShadowRoot = createElement('template', {
shadowrootmode: dom.shadowRoot.mode ?? 'open',
dangerouslySetInnerHTML: {
__html: dom.shadowRoot.innerHTML,
},
});

if (node.props.children) {
node.props.children.unshift(templateShadowRoot);
} else {
node.props.children = [templateShadowRoot];
}

Object.assign(node.props, attributesToProps(dom.attributes));

if (typeof node.props.style === 'string') {
Expand Down
1 change: 1 addition & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function shim() {
getComputedStyle: function getComputedStyle() { return new CSSStyleDeclaration(); },
requestAnimationFrame: function requestAnimationFrame() {},
cancelAnimationFrame: function cancelAnimationFrame() {},
navigator: {},
};

preshimGlobalThis = {};
Expand Down

0 comments on commit 7005536

Please sign in to comment.