What if we'd had better HTML-in-JS syntax all along?
21–30 of 170 posts
Re: What if we'd had better HTML-in-JS syntax all along?
#22I think the JSX is just fine as an HTML-in-JS syntax. You can use JSX independently of React. Here's a 200-line library that lets you use JSX as a templating language just like Handlebars: https://github.com/wisercoder/uibuilder
What's the issue with web components that they have never took off?
(1) Poor native browser support until recently, Firefox originally refused to support Web Components v0 at all (now called Custom Elements in v1, they are supported in all evergreen browsers mostly).
(1a) Available polyfills were initially buggy and had severe performance issues on older browsers.
(2) Lack of proper templating, though I personally quite adore template literals and approaches like e.g. `lit-html`.
(3) Lack of robust styling API - custom styles felt coarse and tacked on in v0 especially, and were poorly supported by pre-processors like LESS/SASS.
(4) Lots of impedance mismatch issues connecting standard JS/HTML/CSS libraries with the Shadow DOM.
Re: What if we'd had better HTML-in-JS syntax all along?
#23Something close to this is possible already in JS, I'm working on a library for generating HTML in node, heavily inspired by Haskell's blaze and lucid. Example code: div( { class: "table-responsive" }, table( { class: ["table table-sm", opts.onRowSelect && "table-hover"] }, thead(tr(hdrs.map(hdr => headerCell(hdr)))), tbody( vs.map(v => tr( mkClickHandler(opts, v), hdrs.map(hdr => td(typeof hdr.key === "string" ? tex…
This actually already exists as a library. This type of syntax is called Hyperscript[1], and it's existed (at least) since 2012. [1]: https://github.com/hyperhype/hyperscript (Although this library does not look maintained anymore)
I think combinator libraries like this and one I'm building are definitely better than templating languages, although JSX can also work really well, in particular because it retains the HTML syntax making it easy to copy from the browser into code.
Re: What if we'd had better HTML-in-JS syntax all along?
#24 {
tag: "button",
attributes: {
"id": "baz",
"class": "foo bar",
"data": "1",
"onclick": f,
},
children: [
"Hello",
{
tag: "em",
attributes: {},
children: ["there"],
}
]
}
Would be represented as: [:button#baz.foo.bar {:data 1 :on-click f} "Hello" [:em "there"]]Re: What if we'd had better HTML-in-JS syntax all along?
#25Re: What if we'd had better HTML-in-JS syntax all along?
#26The "object" syntax reminds me a lot of Clojure/script's Hiccup ( https://github.com/weavejester/hiccup ) style, which has become a de-facto standard across many popular libraries. In hiccup, this example: { tag: "button", attributes: { "id": "baz", "class": "foo bar", "data": "1", "onclick": f, }, children: [ "Hello", { tag: "em", attributes: {}, children: ["there"], } ] } Would be represented as: [:button#baz.foo.b…
Re: What if we'd had better HTML-in-JS syntax all along?
#27Earlier quoted context omitted.
This actually already exists as a library. This type of syntax is called Hyperscript[1], and it's existed (at least) since 2012. [1]: https://github.com/hyperhype/hyperscript (Although this library does not look maintained anymore)
Cool! I was looking for something like this but couldn't find it. Oh well, building my own kept me sane during lockdown. I think combinator libraries like this and one I'm building are definitely better than templating languages, although JSX can also work really well, in particular because it retains the HTML syntax making it easy to copy from the browser into code.
[0] https://github.com/MithrilJS/mithril.js/blob/next/render/hyperscript.jsRe: What if we'd had better HTML-in-JS syntax all along?
#28Re: What if we'd had better HTML-in-JS syntax all along?
#29Earlier quoted context omitted.
Wow. I'd never heard of this. And now instead we have virtually the same thing via JSX except we now require a compilation stage to achieve essentially the same thing...
I'm sure it was one of the inspirations for JSX -- https://facebook.github.io/jsx/#prior-art
Re: What if we'd had better HTML-in-JS syntax all along?
#30Something close to this is possible already in JS, I'm working on a library for generating HTML in node, heavily inspired by Haskell's blaze and lucid. Example code: div( { class: "table-responsive" }, table( { class: ["table table-sm", opts.onRowSelect && "table-hover"] }, thead(tr(hdrs.map(hdr => headerCell(hdr)))), tbody( vs.map(v => tr( mkClickHandler(opts, v), hdrs.map(hdr => td(typeof hdr.key === "string" ? tex…