Web Components are awesome!


Ever wondered how by placing "video" or "audio" into your HTML, you get a media player with controls included? Or how, depending on the type attribute, "input" can be a button, a place to enter text, select a date or file, color picker and more? What if you could create your own element? The answer: Web Components! 🤯 In this talk, we’ll take a look at what Web Components are, how to make one and include it into an application.



Hi, I'm Dwayne, a JavaScript developer advocate at Vonage, and the good folks at JS Nation have given me 10 minutes to make the case on why I think web components are awesome. So what are web components? Have you ever used the video, audio, and or details HTML tags? Ever wondered how just by putting the tag with some attributes and properties you get fully working media player interfaces with volume controls, play and pause buttons, and more or a drop down that just works out of the box? Now imagine creating your own HTML element that does whatever you want it to do. What does the browser support look like? All modern browsers have support for web components. You may be saying to yourself, that sounds cool and all, but are web components a thing yet? Luckily enough, it's a website. And the answer is yes, that's it. Just kidding. This site lists some companies that are using web components. And as an exercise for everyone watching, as I scroll down, if there is a company or logo you recognize, say ding. Here we go. It's a long list. Seen anything yet? The list keeps on going. There's quite a few. And this is just some of them. How many times have you said ding yet? Keeps on going. Here we go. Now here's a couple of specific examples. If you view the source code of YouTube, you'll see many HTML tags that begin with YTD. YTD is not a normal prefix for an HTML element. So that's a good clue that it's a web component. Now when you do things like creating a repo, pushing changes, and or merging PRs on GitHub, you can see how long ago your activity happened. Those are web components. And GitHub is offering them for everyone to use in their own projects. Now I like to make websites. A lot of websites. For each, I like to have an about page that goes over the who, what, and why I made the site. Now creating the page over and over again became a chore. So I created a web component called Dwayne-made. I can pull the data about the website from an endpoint in the project and pass it into the web component and Dwayne-made knows how to display that data. It saves me a bunch of time to focus on the application itself. Another awesome feature is that a web component can have multiple uses. For example, isn't it cool how the humble input HTML element can do so many different things with just a change of the type attribute? So along those lines, I made Dwayne-timer because I was dealing with a lot of time and dates and personal projects and didn't want the hassle with it over and over each time. Not only can it display a date and time local to the visitor of the site, but it can also be a countdown timer just with an addition of a property to the web component. Now the tooling around web components is pretty awesome. There are so many libraries and frameworks to create web components. Want to use TypeScript? There are multiple libraries you can use. More familiar with JSX? No problem. A good resource to give these tools a try without having to download them is webcomponents.dev. Now webcomponents.dev can do a bunch of things, but it can also give you the templates to where you can just kind of kickstart a web component to kind of see if it's what you like as far as coding. Right now there are 64 templates that you can use. Now if you are on a team and want to ensure consistency across all the web components you build, there is Open Web Components which provides guidelines, recommendations, and best practices when creating web components. They also have a CLI tool that will scaffold a project so that all web components will have the same file and folder structure, making it easier for every team member to get up to speed on a component they have never seen before by knowing where to look. Now the fact that there are so many people working on web components means that there is probably something already built that is similar to what you are looking for. No need to start from scratch, which is awesome. Webcomponents.org is a good place to start to see what's out there and even add your own to the list. I'm a big fan of WebXR, augmented and virtual reality on the web, and always wanted to create an application but didn't have enough experience to be able to create something from scratch without studying a long list of different technologies like 3JS, 3D modeling software like Unity, and so much more. That's where the Model Viewer web component comes in. It does everything I need to do to display a 3D model in both virtual and augmented reality and more without needing to know the underlying technologies that went into making it work. With it, I was able to create a WebXR tic-tac-toe game with voice chat in about a week. Here's a quick demo. Now as you can see on the laptop to the left, I have the 3D version of the web component, the actual tic-tac-toe board. On the right, I have the augmented version of it. I'm able to pass the moves back and forth using Vonage APIs. Also I can enable audio chats and that's also done through the Vonage APIs. So you can see the moves are being played back and forth and then at the end when there's a winner, there's some confetti. So if you want to give it a shot, you can give it, you know, play a game or two. I have it deployed to XOXR.games. I'm hoping to add more WebXR games to it as well in the future. Now another awesome thing about web components that you can use them in your favorite framework or by themselves. This site, custom-elements-everywhere.com, lists how well supported web components are with various frameworks and libraries. So let's take a look. There's a quick scroll through. Now just take a note of your, see if your favorite framework or library is here and how well do they work with web components. It's quite a long list. There's a lot of different libraries and frameworks that this website uses and tests. All right. That's enough. You can check out the website itself if you like to see if your favorite framework is on there. Now I created a set of web components to be used with the Vonage Client SDK. Each one knows what events to listen for and what events to send when needed. So they are a straightforward way of using the Vonage Client SDK regardless of the framework you choose. Now I've written a series of blog posts on how to use them with Angular, Vue, and React with more coming soon. Not only can you include them into your application, but you can also style them to look like a part of your application. Here's an example. This is the VC text input from the Client SDK UI web components I mentioned earlier. So here's the default styling. If I uncomment this, you'll see it changed to a totally different styling, which is pretty cool I believe. So I think that is awesome. Well there it is. Just a few of the things I think make web components awesome. Thanks for listening. Hopefully, you think web components are awesome also. Now here is a link to my web components blog series. My contact info is on my website, lifelong.dev, if you'd like to discuss web components or anything Vonage. Check out our APIs if you want to add some real-time communications to your application via video, voice, text, SMS, and many more. If you found them to be awesome and want to learn more, you can check out our APIs. Once again, thank you for listening and have a great rest of the conference. See you around.
10 min
16 Jun, 2022

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

Workshops on related topic