-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
42 lines (33 loc) · 858 Bytes
/
init.js
File metadata and controls
42 lines (33 loc) · 858 Bytes
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
31
32
33
34
35
36
37
38
39
40
41
42
// Virtual DOM by Tim Bowman
"Use Strict";
// Create an element
function createElement(tag, config, children = null) {
const { className } = config;
return {
tag: tag,
props: {
children: children
},
className: className,
dom: null
};
}
// Mount elements
function mountElement(element, parentDOMNode) {
const { tag, className, props } = element;
const domNode = document.createElement(tag);
element.dom = domNode;
if(props.children) {
props.children.forEach(function(child) {
mountElement(child, domNode);
});
}
if(className !== undefined) {
domNode.className = className;
}
//append domNode to DOM
parentDOMNode.appendChild(domNode);
}
const root = document.getElementById('root');
const app = createElement('div', {className: 'wrapper'}, [createElement('h1', {className:'title'})]);
mountElement(app, root);