Skip to content

Instantly share code, notes, and snippets.

@Nikaoto
Last active October 24, 2022 18:04
Show Gist options
  • Save Nikaoto/f40a108f43318046e8c181e9a0b7a9d9 to your computer and use it in GitHub Desktop.
Save Nikaoto/f40a108f43318046e8c181e9a0b7a9d9 to your computer and use it in GitHub Desktop.
js micro framework
// Copied from https://news.ycombinator.com/item?id=23590750
const $t = document.createTextNode.bind(document);
const $e = (t = 'div', p = {},c = []) => {
let el = document.createElement(t);
Object.assign(el,p);
el.append(...c);
return el;
}
// Make a button
let button = $e("button", { className: "my-button" }, ["my text"])
document.appendChild(button)
// Button constructor
let $button = (p={}, c=[]) => $e("button", {className: "my-button", ...p}, c)
// Make a menu of buttons (using the button constructor as well)
let menuStyle = "background-color: cyan;"
let menu = $e("div", { className: "my-menu", style: menuStyle }, [
$e("button", { className: "my-button" }, ["button1"]),
$button({}, "button2"),
$button({}, "button3"),
])
@Nikaoto
Copy link
Author

Nikaoto commented Oct 24, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment