aboutsummaryrefslogtreecommitdiffstats
path: root/src/dom.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/dom.js')
-rw-r--r--src/dom.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/dom.js b/src/dom.js
new file mode 100644
index 0000000..9e44d0a
--- /dev/null
+++ b/src/dom.js
@@ -0,0 +1,48 @@
+export const css = ([ code] ) => {
+ const style = document.createElement('style');
+ document.head.appendChild(style);
+ style.appendChild(document.createTextNode(''));
+
+ let i = 0;
+ for (const v of code.split('}')) {
+ if (v.indexOf('{') < 0) continue;
+ style.sheet.insertRule(v + '}', i++);
+ }
+
+ return style;
+};
+
+const appendChild = (parent, child) => {
+ if (Array.isArray(child)) {
+ child.forEach((nestedChild) => appendChild(parent, nestedChild));
+ } else {
+ parent.appendChild(child.nodeType ? child : document.createTextNode(child));
+ }
+}
+
+const objectProps = { style: true, dataset: true };
+
+export const createElement = (tag, props, ...children) => {
+ if ('function' === typeof tag)
+ return tag(props, children);
+
+ const element = document.createElement(tag);
+
+ for (const [name, value] of Object.entries(props || {})) {
+ if (name.startsWith('on') && name.toLowerCase() in window) {
+ element.addEventListener(name.toLowerCase().substr(2), value);
+ } else if (objectProps[name]) {
+ Object.assign(element[name], value);
+ } else {
+ element.setAttribute(name, value.toString());
+ }
+ }
+
+ for (const child of children) {
+ appendChild(element, child);
+ }
+
+ return element;
+}
+
+export const createFragment = (props, ...children) => children;