mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -05:00
4dee38711c
* Fix client hydration in experimentalReactChildren * Add tests * Add a changeset * Use recursion instead of walking * getChildren -> swap order --------- Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
30 lines
1,023 B
JavaScript
30 lines
1,023 B
JavaScript
import { parse, DOCUMENT_NODE, ELEMENT_NODE, TEXT_NODE } from 'ultrahtml';
|
|
import { createElement, Fragment } from 'react';
|
|
|
|
let ids = 0;
|
|
export default function convert(children) {
|
|
let doc = parse(children.toString().trim());
|
|
let id = ids++;
|
|
let key = 0;
|
|
|
|
function createReactElementFromNode(node) {
|
|
const childVnodes = Array.isArray(node.children) ? node.children.map(child => {
|
|
if(child.type === ELEMENT_NODE) {
|
|
return createReactElementFromNode(child);
|
|
} else if(child.type === TEXT_NODE) {
|
|
// 0-length text gets omitted in JSX
|
|
return child.value.trim() ? child.value : undefined;
|
|
}
|
|
}).filter(n => !!n) : undefined;
|
|
|
|
if(node.type === DOCUMENT_NODE) {
|
|
return createElement(Fragment, {}, childVnodes);
|
|
} else if(node.type === ELEMENT_NODE) {
|
|
const { class: className, ...props } = node.attributes;
|
|
return createElement(node.name, { ...props, className, key: `${id}-${key++}` }, childVnodes);
|
|
}
|
|
}
|
|
|
|
const root = createReactElementFromNode(doc);
|
|
return root.props.children;
|
|
}
|