Let's talk about Web Components

Rate this content
Bookmark

Before the dawn of some of the most popular frameworks (read: React and Vue), there was Web components. Web Components take one of the best parts of these frameworks (reusable components) and combine it with the best parts of web development (native browser support and not needing to set up a build process). As if that's not enough, web components allow you use the same functions across any framework.

If at this point, you're wondering "If web components are so awesome, why haven't I heard about them before?", then you're in luck because that's exactly what this talk is about.

In this presentation, we'll take a look at what web components are, why web components are awesome, why web components can be a pain and how we can use web components both as a standalone tool and together with frameworks.

This talk has been presented at React Advanced 2021, check out the latest edition of this React Conference.

FAQ

Web components are a suite of technologies that allow you to create reusable HTML elements. They include features like custom elements, the shadow DOM, and HTML templates which enable developers to encapsulate functionality, style, and content separately from the main document.

Custom elements are part of the web components suite that allows developers to define new HTML tags with custom behavior. You can create your own HTML tags using the custom elements API, enabling more modular and reusable code.

The shadow DOM is a technology within web components that allows you to encapsulate elements, functionality, and styles, keeping them separate from the rest of the document. This means changes in the shadow DOM do not affect the main document and vice versa.

HTML templates in web components are used to declare fragments of HTML that can be cloned and inserted in the web document multiple times. The content inside a template tag is not rendered until it is activated via JavaScript, allowing for efficient DOM manipulation.

Yes, web components are designed to be cross-framework compatible, meaning they can be used with various frameworks like React, Angular, and Vue. This interoperability allows for the reuse of web components across different projects and frameworks.

Web components offer several benefits including reusability of code, encapsulation of functionality and style, and compatibility across different browsers and frameworks without the need for additional libraries or tools. They simplify the development process and enhance maintainability.

No, web components do not require any installation. They are supported natively in most modern browsers, enabling developers to use them directly without additional setups or tools.

Jemima Abu
Jemima Abu
32 min
22 Oct, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Web Components are a suite of technologies that allow you to create reusable HTML elements, combining the fun of React with regular HTML. Custom elements and the shadow DOM are key features of Web Components, allowing you to define new elements, encapsulate functionality and styles, and interact with a separate DOM. HTML templates serve as placeholders for content to be rendered later in custom components. Custom elements and HTML templates are cross-framework compatible and can be used in React, Angular, Vue, or any other framework.
Available in Español: Hablemos de los Componentes Web

1. Introduction to Web Components

Short description:

Let's talk about web components. I'm a self-taught frontend developer and a school taught systems engineer from Lagos, Nigeria. I like to build things with HTML and CSS. When I was first learning React, I didn't understand why things were the way they were. I prefer using HTML. Web Components are a suite of technologies that allow you to create reusable HTML elements. It's like using regular HTML, but with all the fun of React. Custom elements are an API that allow you to define new elements and their behavior. The shadow DOM allows you to encapsulate elements, functionality, and styles.

Let's talk about web components. So I should probably introduce myself first. I'm a self-taught frontend developer and a school taught systems engineer from Lagos, Nigeria. So I'm a long way from home, but I live in London now, so not that long. And you can find me online anywhere at JemimaAbu. So that's LinkedIn, Twitter, Medium. I make my username easy, so you guys don't have to go to stress to stalk me. It's all there.

All right. So let's get into it. Web components, what are they? So I'd like to preface by saying why I wanted to give this talk. I like to build things with HTML and CSS. I'm not a huge fan of JavaScript. When I was first learning React, I didn't understand why things were the way they were. I didn't understand why I had to use set states to change a state property, but then you couldn't just change it as a variable. It didn't make sense to me. So personally I prefer using HTML.

What I don't like about HTML is if I want to create six blocks of the same thing, I have to copy and paste the same thing many times over. And that's what I do like about React. And I was like, is there a way to have the simplicity of HTML with the reusability of React? And the answer was Web Components. So basically Web Components are a suite of technologies that allow you to create reusable HTML elements. So right off the bat, they're already there in your browser. You don't need to do any installation. It's like using vanilla HTML. Is that what it's called? It's like using regular HTML, but with all the fun of React.

And how does it work? So first of all, we have our custom elements. Custom elements are an API that allow you to define new elements and their behavior. So you can create your own HTML tags to use. And then we have the shadow DOM, which sounds very cool and I like it. Which allows you to encapsulate elements, functionality, and styles.

2. HTML Templates and Repeating Elements

Short description:

So this is what allows you to remove your custom elements from the regular flow of the page and just have it in a different DOM. And then finally we have HTML templates. So I was going to go through a little bit of code to show what I'm talking about. This is a simple blog template that we create in regular HTML. So if you're using regular HTML for this, it's just a lot of copying and pasting and changing the content. So first we're gonna look at what it looks like in regular HTML. The regular HTML is just a lot of pasting of the div with the same content everywhere. And then we have the method I usually use when I'm trying to repeat things. So on my portfolio page, if I was trying to repeat the websites I've built, I usually just create an array with all the content I need and then I render that on the page using vanilla JavaScript.

So this is what allows you to remove your custom elements from the regular flow of the page and just have it in a different DOM. Which is fun. And then finally we have HTML templates. So these are the custom tags that you're actually rendering on the pages. Yeah.

So I was going to go through a little bit of code to show what I'm talking about. So this is a simple blog template that we create in regular HTML. So if you're using regular HTML for this, it's just a lot of copying and pasting and changing the content. So let's see some code. Which is the part I'm good at. All right.

So first we're gonna look at what it looks like in regular HTML. Click. So we have this... We have our code up here. And the regular HTML is just a lot of pasting of the div with the same content everywhere. So I'm gonna zoom in. There we go. So you can see you just have the post div and it's just repeated multiple times throughout the page. Because that is what HTML does. It does not give you variety in how you decide to code. And then we have the method I usually use when I'm trying to repeat things. So on my portfolio page, if I was trying to repeat the websites I've built, because I use basic HTML to build my portfolio page, instead of having to write them all at the same time and then just changing the content, I usually just create an array with all the content I need and then I render that on the page using vanilla JavaScript. So if we're going to do that for this... It's a hard scroll bar. If we're going to do that for this thingy, it would look something like this. So I was meant to be live coding, but then I realized that that's just... I am not brave enough to write code live. Sometimes things go wrong and I break down, and I'm not doing that. So if you were doing this with vanilla JavaScript, it would be something like...

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

The Good, The Bad, and The Web Components
JSNation 2023JSNation 2023
29 min
The Good, The Bad, and The Web Components
Top Content
Web Components are a piece of reusable UI enabled by web standards and built into the web platform. They offer the potential for faster component initialization and less library overhead. Web Components can be created from scratch and utilized with existing component libraries. Shadow DOM and Declarative Shadow DOM provide benefits such as scoped CSS and server-rendered components. The tradeoff between not repeating oneself and achieving full server-side rendering support is discussed. User experience is deemed more important than developer experience.
It's Time to De-Fragment the Web
React Day Berlin 2022React Day Berlin 2022
34 min
It's Time to De-Fragment the Web
Top Content
Today's Talk introduces Mitosis, an open source project that solves the problem of building framework agnostic components. It supports JSX and Svelte syntax and outputs human-readable code for various web frameworks. Mitosis is already being used by enterprise customers and can be easily integrated with React, Svelte, and other frameworks. It saves time, allows for easy handling of framework version migrations, and enables efficient unit and integration testing.
Web Components, Lit and Santa 🎅
JSNation Live 2021JSNation Live 2021
28 min
Web Components, Lit and Santa 🎅
Web Components and the Lit library are introduced, highlighting their ability to create custom elements and replicate built-in components with different functionality. The use of semantic HTML and the benefits of web components in development are emphasized. The features of Lit, such as data binding and rendering, are discussed. The Santa Tracker is showcased as an example of web components being used in educational games. The compatibility of web components with other frameworks and their versatility in creating small widgets or large applications are highlighted.
Authoring HTML in a JavaScript World
React Summit US 2023React Summit US 2023
21 min
Authoring HTML in a JavaScript World
Watch video: Authoring HTML in a JavaScript World
This Talk by Tony Alicia focuses on authoring HTML in a JavaScript world. The speaker challenges developers to change their approach to building React components by starting with HTML first. By authoring HTML in a semantic way, readability and maintainability can be improved. Well-authored HTML provides better understanding of content and improves accessibility. It also has performance benefits by reducing DOM size. Investing time in HTML can save time and make applications more future-proof.
Immutable Web Apps
JSNation 2022JSNation 2022
20 min
Immutable Web Apps
Today's Talk discusses immutable web apps and their benefits, such as faster loading times and easy version tracking. The use of Universal Module Definition (UMD) style bundling allows for flexible dependency management and gradual upgrades. Tools like Webpack and Rollup provide ways to reference UMDs in bundles and automate dependency configuration. Arborist and YAML files help resolve dependency trees and handle conflicts, while the Orchard CLI tool automates dependency ordering. Internal and external dependencies can be initialized and managed effectively for optimal performance.
Web Components are awesome!
JSNation 2022JSNation 2022
10 min
Web Components are awesome!
Web components allow you to create your own HTML elements that can do anything you want, and they are supported by all modern browsers. Many companies, including YouTube and GitHub, use web components to enhance their websites. There are extensive tooling and libraries available for web component development. The Model Viewer Web Component enables the display of 3D models in Virtual and Augmented Reality without needing to know underlying technologies. Web components can be used with various frameworks and libraries, and there are resources available to help with compatibility.

Workshops on related topic

Web Components in Action
JSNation Live 2021JSNation Live 2021
184 min
Web Components in Action
Workshop
Joren Broekema
Alex Korzhikov
2 authors
The workshop extends JavaScript programming language knowledge, overviews general software design patterns. It is focused on Web Components standards and technologies built on top of them, like Lit-HTML and Lit-Element. We also practice writing Web Components both with native JavaScript and using Lit-Element. In the end we overview key tooling to develop an application - open-wc.