Userinterface.js – A small library to build front-end JavaScript applications
github.com
github.com
Would I use this in production? No. But the author is not asking for that. It takes a lot of guts to create something and show it to this community. I really enjoyed looking at how the code progressed, how the author tackled a difficult subject, and their replies to constructive comments. Keep it up!
My main reason for preferring templates over jsx is that it allows to separate the functional component code behind the scenes from the actual rendered html in a cleaner fashion, imo, than just embedding the html directly into the JS code. I could also say that, due to my background, I am just more comfortable with it, as well.
I'm not certain I would call Vue.js templates as an 'almost-JS' templating language. It is pure html. A browser could render it without any js backing it up.
I can't speak for many other template specs, but I believe Angular.js was just pure html, as well, using custom properties to perform the binding between the html and the backing js, which is very similar to what Vue.js does, in that aspect at least.
Input:
UserInterface.model({ name: "children", method: UserInterface.appendChild, properties: { tagName: "div", className: "model", children: [ { tagName: "div", className: "child", textContent: "My first child" // and so on.. } ] } });
UserInterface.runModel("children", { parentNode: document.body });
Output:
<body> <div class="model"> <div class="child">My first child</div> </div> </body>
userinterface.js was made out of the frustration I went through when I was maintaining multiple Browser Extensions. I wanted it to exploit my existing knowledge of DOM API and JavaScript language. Something that would at least try to befriend CSS, HTML and JS to make them work together in a semantic fashion.
Giving their own world to each of the parts of the UI was for the most part the core aspect of it.
Most of the frameworks you mentionned have their own ecosystem, I find them pretty hard to integrate in contexts such as Browser Extension if you do not wish for a never ending build toolchain.
I think a fair comparison for userinterface.js would be the Web Components API.
I'm glad you're even asking the question, thanks.
I will keep on doing what I like to do and keep a very close eye on my enthusiast so that it will never leave my side. We are very much a like in the regard, variety make for a great part of the world in its own.
I am not sure if there would be anything for you to gain today but if it's not today I surely will be making something that is at least worth seconds of your time.
UserInterface.model({
name: "children",
method: UserInterface.appendChild,
properties: {
tagName: "div",
className: "model",
children: [
{
tagName: "div",
className: "child",
textContent: "My first child"
// and so on..
}
]
}
});
vdom was probably the wrong terminology for me to use. What I'm getting at with that is that your properties are like a virtual dom tree.The majority of what I see being painful to use here is caused by exposing all of that manual boilerplate. You can do something equivalent with a hyperscript-like syntax:
h('div.model', [
h('div.child', 'My first child')
])
Or with JSX that compiles down to what you have in your `properties`: <div class="model">
<div class="child">My first child</div>
</div>
You could even trivially add a helper function with low runtime costs that makes it easier to write out the properties. Just using `h` here since it's like hyperscript, but you could call it something better: function h(tagName, attributes, ...children) {
return {
tagName,
...attributes,
children
}
}
That alone would make it easer to read and write properties. The example would become: UserInterface.model({
name: "children",
method: UserInterface.appendChild,
properties: (
h('div', { className: 'model' }, [
h('div', {
className: 'child',
textContent: 'My first child'
})
])
)
});
There are other things that you can't fix with syntax changes, though. For example, each model defining the `method` used to add it to the parent seems backwards to me. Shouldn't a component only care about its own state? Down the road, if you wanted to add one of these `children` things to another element by prepending it rather than appending it, you'd have to have to change the child.I get your point, and it's a fair one. Right now if you want to "prepend" you have to add a container as first child and give it a className to identify it and then use the "runModel" on it.
I like the idea of going the opposite way and I also share your thoughts about component only caring for its own state.
Thank you (again) very much for your feedback, much appreciated.
Kudos to the author for sharing and good luck.
edit: what I don't get is how this post ended up at #10 of /news (I'm jealous)
Because someone wrote it and wanted to share it. That's reason enough.