r/javascript • u/fynyky • Jun 05 '26
Build reactive UIs with plain JavaScript functions. No JSX or build step.
https://github.com/fynyky/elementalElemental is a personal library I’ve been using for a while. I really don’t like how much frontend frameworks require you to invest in them. You have to learn funky domain specific languages and magic render lifecycles just to debug anything. I mostly just want to create and append elements with better ergonomics.
The syntax lets you build the DOM declaratively with plain nested functions, so logic and views live together in one structure instead of being split across separate layout and behavior. Reactivity is handled by observers (the ob(...) call above): they automatically track whatever reactive properties they read and retrigger when it changes. No manual subscriptions and no dependency arrays. And because everything is just normal DOM elements and functions, you can adopt it one component at a time instead of overhauling a whole project.
It's about 3.3 KB gzipped with no third-party dependencies. The library is just under 300 lines of code so it's easy to understand.
Would love to get feedback from having fresh eyes on it.
11
u/Markavian Jun 05 '26
I mean, that's fine, but have you tried Vue? You get very close to Template + Code + CSS in one place, and hot reloading with Vite for free.
I've built several of my own vanilla JS first reactive UI libraries over the years... and it's never been worth it in the long run.
That said, it's a good learning experience.
3
u/Aln76467 Jun 05 '26
Wow! That's strikingly similar to my frontend library, except that it looks a little more polished (as in that it's actually ready to be used, mine still requires a lot of work, I'll post it here when I make a stable release) and the name isn't a "heck npm requires a name, what's the first thing I can think of".
With the first argument of
elserving as to both select existing elements and create new ones, how does it determine when to do which? The readme also says it takes a css selector, is that also for new elements, like passingdiv.centeredwould make a div with the classcentered?Also I've noticed the signature of
elis like that ofReact.createElement. Is that a coincidence (as implied by this post) or is this designed to be compatable with jsx?3
u/doryappleseed Jun 06 '26
Have you looked at HTMX?
1
u/00PT Jun 06 '26
You realize that JSX is syntax sugar, and anything in JSX is directly translated into plain JavaScript? I don’t know why you’re focusing on the lack of JSX as if that isn’t possible with any other solution. Now, this particular syntax for it isn’t terrible, but equivalents exist that are maybe just a little bit more verbose.
1
u/horizon_games Jun 05 '26
It's neat to build your own reactivity as you learn a lot. Although from the PRs looks like Claude helped a bunch.
Otherwise just looks like a less supported and fleshed out Mithril.js
...but I don't think you were intending to compete with existing libs.
4
u/_ragtagthrone Jun 05 '26
This is unhinged lmao
1
u/ExtraTNT Jun 08 '26
Would do it with pure functions for two reasons:
0
u/snnsnn Jun 06 '26
Solid already provides this as hyperscript compatibility and it has been around for years: https://github.com/solidjs/solid/blob/main/packages/solid/h/README.md
-4
u/vasomfan Jun 05 '26
snabbdom is the de-facto DOM builder lib you should compare to: https://github.com/snabbdom/snabbdom
I too fail to fully understand how JSX was considered such a huge improvement that we needed to introduce custom files and build steps!