Page MenuHomeSealhub

jsx.ts
No OneTemporary

import { Readable } from "stream";
import { tempstream } from ".";
import { VOID_HTML_TAGS } from "./html";
import classNames from "classnames";
type Component = string;
type ComponentProps = Record<
string,
JSX.HtmlAttribute | Promise<JSX.HtmlAttribute>
> | null;
type CreateElementFuntion = (
tag: JSX.HtmlElement | ((properties: ComponentProps) => Readable),
properties: ComponentProps,
...children: Component[]
) => Readable;
const resolveGenericProperty = (name: string, value: JSX.HtmlAttribute) => {
if (typeof value === "boolean" && value) {
return name;
}
if (typeof value === "string") {
return tempstream`${name}="${value}"`;
}
if (typeof value === "object" || Array.isArray(value)) {
return tempstream`${name}="${classNames(value)}"`;
}
return "";
};
const createElement: CreateElementFuntion = (tag, properties, ...children) => {
// just typescript being anoying
const voidElements: string[] = [...VOID_HTML_TAGS];
const props = properties
? Object.entries(properties).map(([name, value]) =>
value instanceof Promise
? value.then((val) => resolveGenericProperty(name, val))
: resolveGenericProperty(name, value)
)
: "";
if (typeof tag === "string") {
if (voidElements.includes(tag)) {
return tempstream`<${tag} ${props} />`;
}
return tempstream`<${tag} ${props}>${children}</${tag}>`;
}
return tag(properties);
};
const TempstreamJSX = { createElement };
export { TempstreamJSX };

File Metadata

Mime Type
text/x-java
Expires
Sat, Nov 23, 06:19 (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
547887
Default Alt Text
jsx.ts (1 KB)

Event Timeline