A Pen by Rodrigo Pombo on CodePen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get an element by id | |
const domRoot = document.getElementById("root"); | |
// Create a new element given a tag name | |
const domInput = document.createElement("input"); | |
// Set properties | |
domInput["type"] = "text"; | |
domInput["value"] = "Hi world"; | |
domInput["className"] = "my-class"; | |
// Listen to events | |
domInput.addEventListener("change", e => alert(e.target.value)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const element = { | |
type: "div", | |
props: { | |
id: "container", | |
children: [ | |
{ type: "input", props: { value: "foo", type: "text" } }, | |
{ type: "a", props: { href: "/bar" } }, | |
{ type: "span", props: {} } | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="container"> | |
<input value="foo" type="text"> | |
<a href="/bar"></a> | |
<span></span> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function render(element, parentDom) { | |
const { type, props } = element; | |
const dom = document.createElement(type); | |
const childElements = props.children || []; | |
childElements.forEach(childElement => render(childElement, dom)); | |
parentDom.appendChild(dom); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function render(element, parentDom) { | |
const { type, props } = element; | |
const dom = document.createElement(type); | |
const isListener = name => name.startsWith("on"); | |
Object.keys(props).filter(isListener).forEach(name => { | |
const eventType = name.toLowerCase().substring(2); | |
dom.addEventListener(eventType, props[name]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reactElement = { | |
type: "span", | |
props: { | |
children: ["Foo"] | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const textElement = { | |
type: "span", | |
props: { | |
children: [ | |
{ | |
type: "TEXT ELEMENT", | |
props: { nodeValue: "Foo" } | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function render(element, parentDom) { | |
const { type, props } = element; | |
// Create DOM element | |
const isTextElement = type === "TEXT ELEMENT"; | |
const dom = isTextElement | |
? document.createTextNode("") | |
: document.createElement(type); | |
// Add event listeners |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const element = { | |
type: "div", | |
props: { | |
id: "container", | |
children: [ | |
{ type: "input", props: { value: "foo", type: "text" } }, | |
{ | |
type: "a", | |
props: { | |
href: "/bar", |