-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathh.js
30 lines (30 loc) · 933 Bytes
/
h.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Shorthand for creating Elements.
* @param {*} tag The tag name of the element.
* @param {*} [props] Optional props.
* @param {*} children Child elements or strings
*/
module.exports = function h(tag, props, ...children) {
let element = document.createElement(tag);
if (props) {
if (props.nodeType || typeof props !== "object") {
children.unshift(props);
}
else {
for (let name in props) {
let value = props[name];
if (name == "style") {
Object.assign(element.style, value);
}
else {
element.setAttribute(name, value);
element[name] = value;
}
}
}
}
for (let child of children) {
element.appendChild(typeof child === "object" ? child : document.createTextNode(child));
}
return element;
}