-
-
Save fre-sch/cd45f6973a52cbc507e9bc439cc6e455 to your computer and use it in GitHub Desktop.
trying syntax simplifications for using preact h without jsx
This file contains hidden or 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 (type, props, children) { | |
// placeholder for preact h | |
return {type, props, children} | |
} | |
var ElementPropSetter = { | |
get: function(target, name, proxy) { | |
return function(...values) { | |
if (name == "class") | |
target.props.class = values.join(" ") | |
else if (values.length > 0) | |
target.props[name] = values[0] | |
return proxy | |
} | |
} | |
} | |
var ElementFactory = { | |
get: function(target, name) { | |
var el = Object.assign( | |
function (...children) { | |
return h(name, el.props, children) | |
}, {props: {}} | |
) | |
return new Proxy(el, ElementPropSetter) | |
} | |
} | |
var _ = new Proxy({}, ElementFactory) | |
var t = _.nav.class("navbar navbar-dark navbar-fixed bg-dark container-fluid").id("main").onclick(e => console.debug(e))( | |
_.ul( | |
_.li.class("nav-item", "active")( | |
"one" | |
), | |
_.li.class("nav-item")( | |
"two" | |
), | |
_.li.class("nav-item")( | |
"three" | |
), | |
_["special-element"]() | |
) | |
) | |
console.debug(t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment