openpencil/packages/core/src/render/mini-react.ts

31 lines
741 B
TypeScript
Raw Normal View History

export type ReactElement = {
type: string | Function
props: Record<string, unknown> & { children?: ReactNode[] }
}
export type ReactNode = ReactElement | string | number | null | undefined | ReactNode[]
export type FC<P = Record<string, unknown>> = (props: P) => ReactElement
export function createElement(
type: string | Function,
props: Record<string, unknown> | null,
...children: ReactNode[]
): ReactElement {
const flatChildren = children.flat()
return {
type,
props: {
...props,
children:
flatChildren.length === 1
? (flatChildren as ReactNode[])
: flatChildren.length > 0
? flatChildren
: undefined
}
}
}
export default { createElement }