So in a way, this pattern is exclusively server rendered. We didn't do anything but add a few event listeners here. We're not using any library JavaScript, there's no library overhead, and we can have nice progressive enhancement without any layout shift which is great for a core web vitals as well.
So there are a few web components that I built in the wild that kind of reuse this pattern. There's a details utils component that I built to sort of add additional behaviors to your detailed summary elements on your page. And you can go and check that component out if you're interested in it. This one just adds a behavior to close the details element when you click outside of it.
In this example, I have a video radio star component that only plays a video element on the page when it's visible, which I think is a nice pattern, especially when you're you don't necessarily want to play all the way through before the user has even encountered them on the page. So we want autoplay, but only when the user scrolls to it. So in this way, we're enhancing existing HTML that is the child of our web component.
And another example that I have here on my Github is a is lan component. And now Astro is a very popular component framework, or a popular framework that really relies heavily on islands architecture, which is just kind of spicy, fancy, lazy loading, which I know is maybe a controversial thing to say, but I'll say it anyway. So yeah, there's an is land web component that does something very similar. So any web components that are nested inside of this is land component are only initialized when they're visible to the end user, which I think is great. And there's a bunch of different mechanisms to that, too. You can only do it when it's idle, you can do it when save data is enabled, you can do it under media queries, or only when the user has interacted with it.
Now, the problem with this pattern here is that all of the content inside of our web component is sort of repeated. So the authoring experience is not great, because we really don't want to have to nest all of this repeated HTML throughout all of our content. So my number one grievance probably with this pattern is that I do have to duplicate this HTML myself, unless I have an existing library or framework on top to do that for me. And I don't like to repeat myself. And I don't like to repeat myself. So when we have three counter instances on the page, this is kind of what the authoring experience is of what I'd expect or what I'd want to do. I want the unique things inside of the component instances. I don't want any repeated things that I have to manage myself.
So to do that, we have to graduate to a level two of Web Components and go to use ShadowDOM. So ShadowDOM is a way to have a reusable template that you can inject yourself in JavaScript. And this is what this might look like. So we have a template element on our page. We have our button inside of it. We have our slot.
Comments