capr / blag

:suspect: Blog/wiki of a grumpy old programmer
4 stars 0 forks source link

Why web components suck #27

Open capr opened 3 years ago

capr commented 3 years ago

Web components aka extending HTML with custom tags with structure, styling and behavior defined in JavaScript is a great idea (though arguably an obvious one that should've been available from the start) but with a horrible implementation. Here's why:

(reddit thread)

An alternative

A component API that's much more useful can be easily implemented (albeit somewhat inefficiently) by overriding all built-in DOM creation and manipulation API methods so that they scan all attached and detached elements in order to create components, and have them receive attach/detach events. And as a bonus, this API also lets you register an attach/detach handler for any selector including for built-in tags, not only your registered component tags.

This was implemented in divs.js (need to read glue.js as well to understand it). The whole "web components" thing is implemented in:

This implementation doesn't actually override the native DOM manipulation APIs simply because I don't use them (I use my own wrappers that are safer and more convenient), but it totally could, and that would make components available to third-party libraries that create/attach DOM elements.