Earlier quoted context omitted.
How is this different to the standard WebComponents then? I mean, web components are JS with HTML in them and you have all the power of JS to manage state and functionality. In case of zjs you have HTML with JS in it and you get an entry point callback to set up your instance (and another to clean it up). BTW, the API is a little awkward (`exports.onConnect = function () { / ... / }`) and easy to mix up with the stan…
> The point is if HTML inclusion is not the main feature, but rather the per-instance state then regular WebComponents seem like a better solution if only for being a standard. Yes, they are better, but they also require more lines of code to express the same component; I consider zjs-component more as a low-code alternative to extending HTMLElement than as a replacement for HTMLElement. For example, extending HTMLEl…
ZjsComponent: A Pragmatic Approach to Reusable UI Fragments for Web Development
61–65 of 65 posts
Re: ZjsComponent: A Pragmatic Approach to Reusable UI Fragments for Web Development
#62Earlier quoted context omitted.
> The point is if HTML inclusion is not the main feature, but rather the per-instance state then regular WebComponents seem like a better solution if only for being a standard. Yes, they are better, but they also require more lines of code to express the same component; I consider zjs-component more as a low-code alternative to extending HTMLElement than as a replacement for HTMLElement. For example, extending HTMLEl…
I see what you mean. I do understand the desire to reduce boilerplate. I'd go for a small framework like Lit (I'm sure there are even smaller ones out there) for that but I wouldn't fault anyone for writing their own. I guess, good for you you could get a paper out of it, too. It just doesn't feel particularly novel to warrant one.
I appreciate the sentiment; while this is, indeed, a paper, it is not a published or peer-reviewed paper. I wrote it with no intention of actually publishing it anywhere, and putting it on Arxiv is better long-term than putting it into Github or similar (I expect Arxiv to outlive any code forge).
In much the same way that I looked at web components and thought "What a nice idea. Here is how I can make this incrementally better and support client-side includes as well", I am hoping that this 100-lines of code will someday be looked at by someone else, who will (with the benefit of future knowledge and tech), then say "What a nice idea. Here is how I can make this incrementally better AND support ".
In any case, I thank you for your criticism and your time; your criticism can only make this better (for example, after reading your criticism, I think that showing a side-by-side comparison of my counter example with a custom element doing the same thing will make it more obvious why I find zjs-components more pleasant to write and use than Custom Elements).
Cheers :-)
[EDIT: Here is the comparison, in case you are still curious]
Here is the small comparison; I gave ChatGPT the ZjsComponent README.md and got it to write the example in the README as a custom element web component.
Here are the two implementations:
Implementation as a zjs-component:
Counter Value: 0
+1
+2
+5
function increment(amount) {
const el = this.querySelector("[name='counter-value']");
el.textContent = parseInt(el.textContent) + amount;
}
exports.increment = increment;
Usage of zjs-component:
Implementation as a custom element web component:
class CounterComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
Counter Value: 0
+1
+2
+5
`;
}
connectedCallback() {
this.shadowRoot.querySelectorAll('button').forEach(btn => {
btn.addEventListener('click', () => {
const amount = parseInt(btn.getAttribute('data-amount'), 10);
this.increment(amount);
});
});
}
increment(amount) {
const valueEl = this.shadowRoot.getElementById('counter-value');
valueEl.textContent = parseInt(valueEl.textContent, 10) + amount;
}
}
customElements.define('counter-component', CounterComponent);
Usage of the custom element web component: Re: ZjsComponent: A Pragmatic Approach to Reusable UI Fragments for Web Development
#63Shameless plug; I'm the author. Criticism welcome. I use this for client-side includes and web components. No build process, no web-packer, no framework, no npm requirement. Just include the JS in your HTML and then you can create and include components.
This is great! A few comments/questions if you have time to respond: 1) I thought that React already worked this way until I learned it, and was horrified. Similar disillusionment with Ruby on Rails, Objective-C on iOS and Java on Android. Why someone would want to mix code and markup, rather than define components via code to be used within markup, blows my mind. Said as someone who uses PHP daily and sees JSX as it…
> Have you found ZjsComponent easier to debug, and if so, how?
I do find it easier to debug, mostly because the elements show up in the devtools. ISTR putting in a few extra lines to make debugging easier but I've never used the debug feature after putting it in.
It turned out that, due to the reduced functionality (and much reduced surface area) of ZjsComponent, my bugs are all shallow - i.e. they're all in JS methods which can be stepped through in the debugger.
> 5) Do advanced use cases such as components built with other components, subresource integrity (SRI) and observing variables similarly to Vue.js watchers "just work"? I'd be curious to hear your vision for the future.
There is no observability, but composing components with other existing components works quite well[1]. I used a `menu` component that contains subcomponents for client-app routing.
My vision for the future is "I wish someone will look at this and improve the developer experience slightly", just as I looked at custom element web components and improved the developer experience slightly :-)
> (Special mention) after driving myself crazy manually managing recycler views in iOS, Android and React, I wanted to highlight [...] > The idea is to populate callbacks to handle the lifecycle of table view cells. That way, an array or generator in memory can be mapped to a table and automagically handle all scrolling and resizing edge cases. Including dynamically loading images and other async data with caching via SDWebImage, without crashing when navigating between view controllers that get disposed, while running as fast as possible.
This is quite interesting. I will have a look at the links when I have time, but I think that it might be a good experience for me to implement more complex components (such as active tables like you describe) using ZjsComponent. It would be a good experience to determine where my approach falls short.
---------------------------------------------------------- [1] TBH, the version I use in production might be different to the one published - I wrote the paper, archived the source, etc well over a year ago. I may need to update the github code with whatever I am using in production; even in 100 lines, there's a possibility of bugs.
Re: ZjsComponent: A Pragmatic Approach to Reusable UI Fragments for Web Development
#64Earlier quoted context omitted.
I see what you mean. I do understand the desire to reduce boilerplate. I'd go for a small framework like Lit (I'm sure there are even smaller ones out there) for that but I wouldn't fault anyone for writing their own. I guess, good for you you could get a paper out of it, too. It just doesn't feel particularly novel to warrant one.
> I see what you mean. I do understand the desire to reduce boilerplate. I'd go for a small framework like Lit (I'm sure there are even smaller ones out there) for that but I wouldn't fault anyone for writing their own. I guess, good for you you could get a paper out of it, too. It just doesn't feel particularly novel to warrant one. I appreciate the sentiment; while this is, indeed, a paper, it is not a published or…
I know you are trying to avoid boiler plate but I'm wondering how technically difficult it would be to provide an alternative for those of us who really like named components? Something like:
ZjsComponent.register("counter-component", "counter.zjsc");
Then I can just use in the named way, like: Re: ZjsComponent: A Pragmatic Approach to Reusable UI Fragments for Web Development
#65Earlier quoted context omitted.
> I see what you mean. I do understand the desire to reduce boilerplate. I'd go for a small framework like Lit (I'm sure there are even smaller ones out there) for that but I wouldn't fault anyone for writing their own. I guess, good for you you could get a paper out of it, too. It just doesn't feel particularly novel to warrant one. I appreciate the sentiment; while this is, indeed, a paper, it is not a published or…
I really like what you have done. I know you are trying to avoid boiler plate but I'm wondering how technically difficult it would be to provide an alternative for those of us who really like named components? Something like: ZjsComponent.register("counter-component", "counter.zjsc"); Then I can just use in the named way, like: