snabbdom-jsx-lite
Version:
Write snabbdom templates in .jsx or .tsx (JSX for TypeScript)
90 lines • 3.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.jsx = exports.Fragment = void 0;
var snabbdom_1 = require("snabbdom");
/** Equivalent of <> (React.Fragment) that that wraps children without a containing dom element */
function Fragment(_props, children) {
return snabbdom_1.vnode(undefined, undefined, children, undefined, undefined);
}
exports.Fragment = Fragment;
function flattenAndFilterFalsey(children, flattened) {
for (var _i = 0, children_1 = children; _i < children_1.length; _i++) {
var child = children_1[_i];
// filter out falsey children, except 0 since zero can be a valid value e.g inside a chart
if (child !== undefined && child !== null && child !== false && child !== "") {
if (Array.isArray(child)) {
flattenAndFilterFalsey(child, flattened);
}
else if (typeof child === "string" || typeof child === "number" || typeof child === "boolean") {
flattened.push(snabbdom_1.vnode(undefined, undefined, undefined, String(child), undefined));
}
else if (child.sel === undefined && child.text === undefined) {
// vnode from Fragment
if (Array.isArray(child.children)) {
flattenAndFilterFalsey(child.children, flattened);
}
}
else {
flattened.push(child);
}
}
}
return flattened;
}
function addSvgNs(sel, data, children) {
data.ns = "http://www.w3.org/2000/svg";
if (sel !== "foreignObject" && children !== undefined) {
for (var _i = 0, children_2 = children; _i < children_2.length; _i++) {
var child = children_2[_i];
if (child.data !== undefined) {
addSvgNs(child.sel, child.data, child.children);
}
}
}
}
/**
* jsx/tsx + hyperscript compatible vnode factory function
* see: https://www.typescriptlang.org/docs/handbook/jsx.html#factory-functions
*/
function jsx(tag, data) {
var children = [];
for (var _i = 2; _i < arguments.length; _i++) {
children[_i - 2] = arguments[_i];
}
// don't call flattenAndFilterFalsey if no children
var flattenedChildren = children.length > 0 ? flattenAndFilterFalsey(children, []) : children;
if (typeof tag === "function") {
// tag is a function component
return tag(data, flattenedChildren);
}
else {
if (tag === null) {
// Fragment via jsxFragmentFactory: 'null' compiler option
// change to undefined since snabbdom expects `tag: string | undefined`
tag = undefined; // `as string` so we don't get TS typeguard error
}
else if (tag === "svg") {
// svg elements need recursive namespace
addSvgNs(tag, data, flattenedChildren);
}
// append sel css selector to tag to support equivalent of h('div.foo.bar')
data = data !== null && data !== void 0 ? data : {};
if (data.sel) {
tag += data.sel;
data.sel = undefined;
}
var numFlattenedChildren = flattenedChildren.length;
if (numFlattenedChildren === 0) {
return snabbdom_1.vnode(tag, data, undefined, undefined, undefined);
}
else if (numFlattenedChildren === 1 && flattenedChildren[0].sel === undefined && flattenedChildren[0].text) {
// only child is a simple text node, return as simple text node
return snabbdom_1.vnode(tag, data, undefined, flattenedChildren[0].text, undefined);
}
else {
return snabbdom_1.vnode(tag, data, flattenedChildren, undefined, undefined);
}
}
}
exports.jsx = jsx;
//# sourceMappingURL=jsx.js.map