janadom
Version:
A minimal and complete implementation of React JSX creating native DOM elements.
119 lines (118 loc) • 4.34 kB
JavaScript
(function(root, factory) {
if (typeof define === "function" && define.amd) {
define(factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory();
} else {
root.janadom = factory();
}
}(typeof self !== "undefined" ? self : this, () => {
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {get: all[name], enumerable: true});
};
var __exportStar = (target, module, desc) => {
if (module && typeof module === "object" || typeof module === "function") {
for (let key of __getOwnPropNames(module))
if (!__hasOwnProp.call(target, key) && key !== "default")
__defProp(target, key, {get: () => module[key], enumerable: !(desc = __getOwnPropDesc(module, key)) || desc.enumerable});
}
return target;
};
var __toModule = (module) => {
if (module && module.__esModule)
return module;
return __exportStar(__markAsModule(__defProp(module != null ? __create(__getProtoOf(module)) : {}, "default", {value: module, enumerable: true})), module);
};
var src_exports = {};
__export(src_exports, {
createEl: () => createEl,
createRef: () => createRef
});
var import_svg_tags = __toModule(require("./svg-tags"));
function createRef(el = null) {
return {current: el};
}
function createEl(type, props, ...children) {
let el;
if (typeof type === "string") {
el = import_svg_tags.default.includes(type) ? document.createElementNS("http://www.w3.org/2000/svg", type) : document.createElement(type);
} else if (type === DocumentFragment) {
el = document.createDocumentFragment();
} else if (typeof type === "function") {
return type(props, ...children);
} else {
throw new TypeError(`invalid element type ${type}`);
}
for (const name in props) {
const value = props[name];
if (value != null && value !== false) {
if (name === "ref") {
if (typeof value === "object")
value.current = el;
else if (typeof value === "function")
value(el);
else
setAttr(el, name, value);
} else if (name === "style") {
if (typeof value !== "object")
setAttr(el, name, value);
else
Object.assign(el.style, value);
} else if (name.startsWith("on") && typeof value === "function") {
listenTo(el, name, value);
} else {
setAttr(el, name, value);
}
}
}
const parent = el instanceof HTMLTemplateElement ? el.content : el;
for (const child of children)
appendChild(parent, child);
return el;
}
function setAttr(el, name, value) {
if (typeof value === "string") {
if (name.startsWith("xlink:"))
el.setAttributeNS("http://www.w3.org/1999/xlink", name, value);
else
el.setAttribute(name, value);
} else if (typeof value === "number") {
el.setAttribute(name, value.toString());
} else if (typeof value === "boolean") {
el.setAttribute(name, "");
} else {
throw new TypeError(`invalid attribute type ${typeof value} for ${name}`);
}
}
function listenTo(el, name, listener) {
let useCapture;
if (name.endsWith("Capture")) {
name = name.substring(2, name.length - 7);
useCapture = true;
} else {
name = name.substr(2);
}
el.addEventListener(name.toLowerCase(), listener, useCapture);
}
function appendChild(parent, child) {
if (typeof child === "string")
parent.appendChild(document.createTextNode(child));
else if (typeof child === "number")
parent.appendChild(document.createTextNode(child.toString()));
else if (child instanceof Element)
parent.appendChild(child);
else if (Array.isArray(child))
for (const grandchild of child)
appendChild(parent, grandchild);
}
return src_exports;
}));
//# sourceMappingURL=index.umd.js.map