Rich Harris (Svelte) is really giving Web Components some attention now.
His latest crusade against Web Components is basically a rant against the standard behavior of the <template>
tag (available since 2015, yes 15)
https://x.com/Rich_Harris/status/1844134732306792631
And when people try to explain to him this just is what the <template>
does, he continues:
Use the standard any way you want
Like it or not this <template>
has been around since 2015, and will be around for a long time.
I say, learn what it does, and doesn't, and use what is useful to you
I extended Rich his complaint into a playground to play with.
Here are two examples using both a <template>
and a regular <div>
The whole point of a <template>
is to be different, as it was for the past 9 years
If you don't want its behavior, use a <div>
Note how this blogpost was not about Custom Elements or shadowDOM
PS: @rich, calm down, this is not good for your health
To some limited extend I agree that this behaviour is very weird. At least when viewed through the lens of traditional object orientation. But alas, JavaScript is not an object-oriented language:
And with that context, the current behaviour is definitely better than the alternatives. Basically, the options are:
The second option seems marginally better in theory, but would cause way more weird bugs for developers.
The second option would make custom elements basically unusable.
In terms of sanity, this design choice still seems miles ahead of svelte, considering some of the weird quirks that framework has.