Why Web Components?
• 382 views
By now, you may have heard a lot of things about Web Components. Some people say it's the promise land, others say it has been oversold.
Although they're not going to solve all your life's problems, they do provide a paradigm shift from the traditional approach of web development.
If you still aren't sure about what exactly is Web Components or even why you should care about it, this article is for you.
At the start of the web we had a small set of tags. We had
<form>, we had
<select> - you would build your pages out of these tags and they had meaning. They had encapsulation, they had default UI, they would emit events when something interesting happened. Most of the time you just worked in markup.
What if HTML was expressive enough to allow us to extend HTML so we can fill in the gaps in functionality with our own tags? Well, Web Components enable that.
Specifically, Web Components are an umbrella term for four different W3C specifications:
- Custom Elements lets you define your own HTML tags;
- HTML Templates enables you to define blocks of markup with the ability to inject dynamic content into;
- Shadow DOM gives you the ability to scope markup and styles in a separate DOM tree;
- HTML Imports provides a way to include and reuse HTML documents in other HTML documents.
Each of these pieces is useful individually. But when combined, this whole package gives you:
- Composability (being able to create whole sites and apps by putting different elements together);
- Encapsulation (isolating markup, style, and behavior logic so they don’t leak into the rest of the page);
- Reusability (extending existing elements to create new elements, allowing you to stop reinventing the wheel).
That means we can finally fill any gaps in the platform in a meaningful way. Which also opens an opportunity for a less fragmented ecosystem, where components can truly interoperate with each other.
We hope this brief guide inspires you to learn more about Web Components and forth experimenting with your own custom elements.