Page MenuHomeSealhub

jsx.ts
No OneTemporary

Authored By
Unknown
Size
1 KB
Referenced Files
None
Subscribers
None
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 | Promise<Readable>),
properties: ComponentProps,
...children: Component[]
) => Readable | Promise<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),
" ",
])
.flat()
: "";
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
application/javascript
Expires
Sat, Nov 28, 03:59 (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1230235
Default Alt Text
jsx.ts (1 KB)

Event Timeline