logoalt Hacker News

spankaleeyesterday at 4:53 PM6 repliesview on HN

I'm a big web components guy, but calling these web components is a massive stretch of the word component.

The word "component" has to mean something ultimately, and to me the defining feature of a web component is that it's self-contained: it brings along its own dependencies, whether that's JavaScript, templates, CSS, etc. Web components shouldn't require an external framework or external CSS (except for customization by the user) - those things should be implementation details depended on directly by the component.

This here is just CSS using tag names for selectors. The element is doing nothing on its own.

Which is fine! It's just not web components.

edit: Also, don't do this:

    <link-button>
      <a href="">Learn more</a>
    </link-button>
That just adds HTML bloat to the page, something people with a singular focus on eliminating JavaScript often forget to worry about. Too many HTML elements can slow the page to a crawl.

Use classes:

    <a class="button" href="">Learn more</a>
They're meant for this, lighter weight, and highly optimized.

Replies

raframyesterday at 5:15 PM

> To many HTML elements can slow the page to a crawl.

You can read the entirety of War and Peace in a single HTML file: https://standardebooks.org/ebooks/leo-tolstoy/war-and-peace/...

A marketing page, SaaS app landing, etc., will not even begin to approach that size, whether or not you add an extra wrapper around your <a>s.

show 5 replies
dfabulichyesterday at 6:56 PM

HTML elements can style themselves now using the @scope rule. (It's Baseline Newly Available.) Unlike the "style" attribute, @scope blocks can include @media and other @ rules. You can't get more self-contained than this.

    <swim-lane>
        <style>
            @scope {
                background: pink;
                b {
                    background: lightblue
                }
                @media (max-width: 650px) {
                    /* Mobile responsive styles */
                }
            }
        </style>
        something <b>cool</b>
    </swim-lane>
You can also extract them to a CSS file, instead.

    @scope (swim-lane) { /* ... */ }
The reason approaches like this continue to draw crowds is that Web Components™ as a term is a confluence of the Custom Elements JS API and Shadow DOM.

Shadow DOM is awful. Nobody should be using it for anything, ever. (It's required for putting child-element "slots" in custom elements, and so nobody should use those, either.) Shadow DOM is like an iframe in your page; styles can't escape the shadow root and they can't get into the shadow root, either. IDs are scoped in shadow roots, too, so the aria-labelledby attribute can't get in or out, either.

@scope is the right abstraction: parent styles can cascade in, but the component's styles won't escape the element, giving you all of the (limited) performance advantages of Shadow DOM with none of the drawbacks.

show 2 replies
lucideeryesterday at 5:25 PM

There's a lot of contradictions in this comment.

> it's self-contained: it brings along its own dependencies, whether that's JavaScript, templates, CSS

> Also, don't do this [...] That just adds HTML bloat to the page, something people with a singular focus on eliminating JavaScript often forget to worry about. To many HTML elements can slow the page to a crawl.

A static JS-less page can handle a lot of HTML elements - "HTML bloat" isn't really a thing unless those HTML elements come with performance-impacting behaviour. Which "self-contained" web-components "bringing along their own dependencies" absolutely will.

> shouldn't require an external framework

If you're "bringing along your own dependencies" & you don't have any external framework to manage those dependencies, you're effectively loading each component instance as a kind of "statically linked" entity, whereby those links are in-memory. That's going to bloat your page enormously in all but the simplest of applications.

graypeggyesterday at 5:52 PM

I might toss it out there that upcoming changes to attr() [0] as well as typed properties [1] will add some interesting features. Being able to provide a value that's subbed into a stylesheet from the HTML itself is neat.

You can try to get by with auto-generated selectors for every possible value today, ([background="#FFFFFF"]{background: #FFFFFF}[background="#FFFFFE"]{background: #FFFFFE}...) but just mapping attributes to styles 1:1 does begin to feel like a very lightweight component.

(Note... I'm not convinced this is a great idea... but it could be interesting to mess around with.)

[0] https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/V...

[1] https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/A...

akstyesterday at 7:54 PM

I disagree on the number of elements actually approaching problematic territory, but agree this just isn’t something you can’t do already without web components

akagusuyesterday at 5:17 PM

According to the dictionary, the word component means "a part or element of a larger whole" which I think goes to the opposite direction of "self contained"