Skip to content

Instantly share code, notes, and snippets.

View mikaelbr's full-sized avatar

Mikael Brevik mikaelbr

View GitHub Profile
@mikaelbr
mikaelbr / own-jsx-11.js
Created October 8, 2019 19:59
Code for blogpost "Using JSX for your own lightweight declarative library": https://medium.com/@mikaelbrevik/using-jsx-for-your-own-lightweight-declarative-library-a773d3796475
function createElement(tag, props, ...children) {
const el = attrs(document.createElement(tag), props);
// ... rest of the implementation here
return el;
}
@mikaelbr
mikaelbr / own-jsx-10.js
Created October 8, 2019 19:58
Code for blogpost "Using JSX for your own lightweight declarative library": https://medium.com/@mikaelbrevik/using-jsx-for-your-own-lightweight-declarative-library-a773d3796475
const isEvent = (k, v) => k.startsWith("on") && typeof v === "function";
const eventName = k => k.substr(2).toLowerCase();
const isString = s => typeof s === "string";
function attrs(el, props) {
// Remember, JSX sets props to `null` if nothing is defined, so in that case, we just return unchanged el
if (!props) {
return el;
}
@mikaelbr
mikaelbr / own-jsx-9.js
Created October 8, 2019 19:57
Code for blogpost "Using JSX for your own lightweight declarative library": https://medium.com/@mikaelbrevik/using-jsx-for-your-own-lightweight-declarative-library-a773d3796475
const isString = s => typeof s === "string";
function createElement(tag, props, ...children) {
const el = document.createElement(tag);
// Iterate thorugh all rest arguments as children.
children.forEach(child => {
// If string, we make it a text node.
const node = !isString(child) ? child : document.createTextNode(child);
// Add the child to the newly created elements.
@mikaelbr
mikaelbr / own-jsx-8.js
Created October 8, 2019 19:56
Code for blogpost "Using JSX for your own lightweight declarative library": https://medium.com/@mikaelbrevik/using-jsx-for-your-own-lightweight-declarative-library-a773d3796475
function createElement(tag, props, ...children) {
// Create a new html node element.
const el = document.createElement(tag);
// just take the first child for now
el.textContent = children[0];
return el;
}
const h1 = <h1>Hello, World</h1>;
//> <h1>Hello, World</h1>
@mikaelbr
mikaelbr / own-jsx-7.js
Created October 8, 2019 19:56
Code for blogpost "Using JSX for your own lightweight declarative library": https://medium.com/@mikaelbrevik/using-jsx-for-your-own-lightweight-declarative-library-a773d3796475
function createElement(tag, props, ...children) {
// Return something
}
@mikaelbr
mikaelbr / own-jsx-6.js
Created October 8, 2019 19:17
Code for blogpost "Using JSX for your own lightweight declarative library": https://medium.com/@mikaelbrevik/using-jsx-for-your-own-lightweight-declarative-library-a773d3796475
const list = createElement(
"ul",
null,
createElement("li", null, "Peter"),
createElement("li", null, "Paul"),
createElement("li", null, "Mary")
);
@mikaelbr
mikaelbr / own-jsx-6.js
Created October 8, 2019 19:16
Code for blogpost "Using JSX for your own lightweight declarative library": https://medium.com/@mikaelbrevik/using-jsx-for-your-own-lightweight-declarative-library-a773d3796475
// ...
"babel": {
"plugins": [
["@babel/plugin-transform-react-jsx",
{
"pragma": "createElement"
}]
]
}
// ...
@mikaelbr
mikaelbr / own-jsx-5.js
Created October 8, 2019 19:16
Code for blogpost "Using JSX for your own lightweight declarative library": https://medium.com/@mikaelbrevik/using-jsx-for-your-own-lightweight-declarative-library-a773d3796475
function createElement(tag, props, ...children) {
// Return something
}
@mikaelbr
mikaelbr / own-jsx-4.js
Created October 8, 2019 19:15
Code for blogpost "Using JSX for your own lightweight declarative library": https://medium.com/@mikaelbrevik/using-jsx-for-your-own-lightweight-declarative-library-a773d3796475
const list = (
<ul>
<li>Peter</li>
<li>Paul</li>
<li>Mary</li>
</ul>
);
// Outputs
const list = React.createElement(
"ul",
@mikaelbr
mikaelbr / own-jsx-3.js
Created October 8, 2019 19:15
Code for blogpost "Using JSX for your own lightweight declarative library": https://medium.com/@mikaelbrevik/using-jsx-for-your-own-lightweight-declarative-library-a773d3796475
const h1 = <h1 id="myId">Hello, World</h1>;
// Outputs
const h1 = React.createElement(
"h1",
{
id: "myId"
},
"Hello, World"
);