diff --git a/.changeset/eleven-dingos-study.md b/.changeset/eleven-dingos-study.md new file mode 100644 index 0000000..f150063 --- /dev/null +++ b/.changeset/eleven-dingos-study.md @@ -0,0 +1,5 @@ +--- +"ultrahtml": patch +--- + +Update JSX runtime child handling diff --git a/src/jsx-runtime/index.ts b/src/jsx-runtime/index.ts index dac0c8e..314c09d 100644 --- a/src/jsx-runtime/index.ts +++ b/src/jsx-runtime/index.ts @@ -1,26 +1,40 @@ -import type { ElementNode, Node } from '../index.js'; -import { ELEMENT_NODE, Fragment, __unsafeRenderFn } from '../index.js'; +import { ElementNode, Node, TEXT_NODE } from "../index.js"; +import { ELEMENT_NODE, Fragment, __unsafeRenderFn } from "../index.js"; -function createVNode(type: any, props: Record, key: string, __self: string, __source: string) { - const vnode: ElementNode = { - type: ELEMENT_NODE, - name: typeof type === 'function' ? type.name : type, - attributes: props, - children: [], - parent: undefined as any, - loc: [] as any - }; +function createVNode( + type: any, + { children, ...attributes }: Record, + key: string, + __self: string, + __source: string +) { + const vnode: ElementNode = { + type: ELEMENT_NODE, + name: typeof type === "function" ? type.name : type, + attributes, + children: (Array.isArray(children) ? children : [children]).map((child) => { + if (typeof child === "string") { + return { + type: TEXT_NODE, + value: child, + }; + } + return child; + }), + parent: undefined as any, + loc: [] as any, + }; - if (typeof type === 'function') { - __unsafeRenderFn(vnode, type); - } + if (typeof type === "function") { + __unsafeRenderFn(vnode, type); + } - return vnode; + return vnode; } export { - createVNode as jsx, - createVNode as jsxs, - createVNode as jsxDEV, - Fragment + createVNode as jsx, + createVNode as jsxs, + createVNode as jsxDEV, + Fragment, };