cohost/html/jsx/jsx-runtime.ts
2023-07-09 02:34:35 -04:00

48 lines
1.1 KiB
TypeScript

import { Attributes, fr, Html, Prop } from "../html.ts";
type SadProp = Prop | undefined | Html | Html[];
export type Component = (
attributes: Attributes,
children: Html[],
) => Html;
interface Props {
[key: string]: typeof key extends "children" ? never : SadProp;
children: undefined | Html | Html[];
}
export function create_element(
type: Component | string,
props_?: Props,
): Html {
console.log(arguments);
const props: Props = props_ ?? { children: [] };
const children: Html[] = [props.children ?? []].flat();
const attributes: Attributes = {};
for (const [key, val] of Object.entries(props)) {
if (
key == "children" || typeof val == "undefined" || typeof val == "object"
) continue;
attributes[key] = val;
}
console.log(attributes, children);
if (typeof type == "string") {
return { tag: type, attributes, children };
} else {
return type(attributes, children);
}
}
export function Fragment(
_: Record<string | number | symbol, never>,
children: Html[],
) {
return fr(...children);
}
export const jsx = create_element;
export const jsxs = create_element;