Lots of fun. π
Here's a pattern I quite like:
const vnodes = new Proxy({}, { get: (_, tag) => (...rest) => React.createElement(tag, ...rest) })
Usage:
const { h1, p, form, input } = vnodes function App() { return form(null, h1(null, 'Hello, World!'), p(null, "Won't you click that lovely button?"), input({ type: 'button', value: 'Lovely button!' }) ) }
For when you absolutely, positively don't want JSX or a build-system.
that is very cool :D - really a sweet way around JSX :D
I have something similar in vanilla style - was planning on posting about it in a few days... this is just the basic idea:
const spec = { get(o, key) { return o[key] != null ? o[key] : o[key] = (...args) => { const el = document.createElement(key); args.forEach(arg => { if (typeof arg === 'string') { const span = document.createElement('span'); span.innerHTML = arg; el.appendChild(span); } else if (typeof arg === 'object') { if (arg.tagName != null) { el.appendChild(arg); } else { for (let i in arg) { el.setAttribute(i, arg[i]); } } } }); return el; } }, set(o, key, v) { o[key] = v; } } const dom = new Proxy({}, spec); document.body.appendChild( dom.div( dom.button('cool'), dom.h2('some text', { style: 'font-style: italic' }), dom.br(), dom.input({ placeholder: 'testing 123' }) ) ); const { div, input, label } = dom; document.body.appendChild( div( label( 'Slider:', { for: 'slider', style: 'padding:1em;display:block' }, input({ id: 'slider', type: 'range' }) ) ) );
Cool! Really nice creative opportunities with Proxy, and stuff like this is a great application of its usage.
Proxy
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Lots of fun. π
Here's a pattern I quite like:
Usage:
For when you absolutely, positively don't want JSX or a build-system.
that is very cool :D - really a sweet way around JSX :D
I have something similar in vanilla style - was planning on posting about it in a few days... this is just the basic idea:
Cool! Really nice creative opportunities with
Proxy
, and stuff like this is a great application of its usage.