DenReg/jsx-html/jsx.ts

34 lines
867 B
TypeScript
Raw Normal View History

2020-10-14 00:56:11 +00:00
import type {
NodePropsType,
ComponentFunctionType,
NullableChildType,
ChildType,
} from './types.ts';
import { ElementNode } from './node/ElementNode.ts';
import { ComponentNode } from './node/ComponentNode.ts';
export const jsx = <P extends NodePropsType = NodePropsType>(
element: string | ComponentFunctionType,
props: P | null,
...children: NullableChildType[]
) => {
const nodeProps = props || {};
if (typeof element === 'string') {
return new ElementNode(element, nodeProps, children);
}
if (typeof element === 'function') {
return new ComponentNode(element, nodeProps, children);
}
throw new TypeError(`Expected jsx element to be a string or a function`);
};
export const Fragment = (
props: NodePropsType,
children: ChildType,
): NullableChildType => {
return children;
};