Web Components, Lit and Santa 🎅

Rate this content
Bookmark

Get started with Web Components – enabling you to define new HTML elements that work everywhere regular HTML does. This talk will focus on Lit, a suite from Google that helps you create WCs with features you'd expect like data-binding and declarative definitions. It'll also cover how we've used them to build one of the web's jolliest sites, Google's Santa Tracker 🎅

FAQ

Web Components allow developers to create reusable custom elements with their functionality encapsulated away from the rest of your code. They are first-class citizens in the HTML world, meaning they behave just like standard HTML elements.

Lit is a highly opinionated library designed by Google to facilitate easier creation of Web Components. It provides tools for efficient data binding and component structuring, making it an ideal starting point for developers looking to utilize Web Components in their projects.

Santa Tracker is a single-page application that uses Web Components to manage its interface and interactions. The platform showcases new technologies and supports legacy code, with many components built using the LitElement library, enhancing the site's modularity and reusability.

Shadow DOM is a part of the Web Components technology that enables encapsulation of CSS and JavaScript, allowing Web Components to have isolated HTML, CSS, and JavaScript. This isolation prevents styles from leaking into the global scope and preserves component independence.

Web Components provide a standard way to create reusable custom elements, enabling better code organization, maintenance, and reusability across projects and teams. They also ensure compatibility across different frameworks and browsers, enhancing performance and scalability.

Yes, Web Components can extend existing HTML elements or create entirely new custom elements. This flexibility allows developers to enhance existing functionality or introduce new features without disrupting the underlying HTML structure.

Lit simplifies the process of building Web Components with features like efficient data binding and declarative templates. It abstracts common tasks, reduces boilerplate, and provides performance optimizations, making it easier to develop robust Web Components.

Yes, Web Components are supported by about 95% of all modern browsers, providing a robust and widely compatible standard for developing complex web applications without relying on third-party libraries or frameworks.

Web Components are typically used to encapsulate and manage specific pieces of functionality within a web application, such as user interface elements that are reused across different parts of an application or across different projects.

Sam Thorogood
Sam Thorogood
28 min
11 Jun, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

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.

1. Introduction to Web Components and Lit Library

Short description:

Hey there, my name's Sam, and I'm here today to talk about Web Components, the Lit library, and Santa Tracker. Firstly, we're here to talk about web components. So let's have a bit of background on elements, and what pages are. Web components, fundamentally, let's us write our own elements. These are first-class citizens that work just like regular HTML elements. The goals of this talk are to introduce you to web components and their component parts, and to learn about Lit, a very opinionated web component library that Google ships.

Hey there, my name's Sam, and I'm here today to talk about Web Components, the Lit library, and Santa Tracker. But first, some background on me. I'm lucky enough to be the lead on Santa Tracker. It's something I've done for five or six years now. Luckily, I don't have to do it all year round, only for a few months around the holiday season. Otherwise, I would go insane, just to be clear. I also work on content sites like web.dev and developer.chrome.com.

For me, personally, what do I like? I like pushing the boundaries, doing weird web things. You can read my blog if you are curious about more. And I have some dislikes here too. I like building websites in a very sudden way, and I think big builds, during development, really slow you down. I'd love to see a much faster development process, and I think a lot of tools now are do help support that.

Firstly, we're here to talk about web components. So let's have a bit of background on elements, and what pages are. Now, we all know HTML pages are made up of a bunch of different elements. These can be semantic elements that mostly just affect your style and also are important for things like screen readers, and also a bit of semantics for you as a developer, things like header and section. But they can also be elements that have functionality, things like A and button, and elements like dialogue and details, and more advanced versions of those that have a state that changes. Web components, fundamentally, when it gets down to it, let's us write our own elements. These are first-class citizens that work just like regular HTML elements. I'm going to talk through an example of my details, which I'll get to shortly.

So what are the goals of this talk? I want you to be introduced to web components and their component parts. This is a fairly high-level talk. I want to make that clear. We'll be skipping over some more nuanced things, which, hopefully, I will point you in the right direction so you can go find out more. I want you to learn about Lit, a very opinionated web component library that Google ships, which is really the best place to get started writing web components. And also, I'd be very happy in another tab if you open Santa Tracker and click around and play some games and just try to work out which games and what parts of the site are run with web components. So let's talk through details. Details is a really interesting element I mentioned before. It's got a bunch of functionality. If you haven't seen it before in action, what it does it shows the summary text, which is the thing on the top here, and only shows the longer form description when you click on that summary.

2. Creating Custom Details Web Component

Short description:

I want to write my details as a web component that opens upwards. It's a powerful example of how web components can reproduce a built-in component but with different functionality. By defining the inner HTML of the shadow root and adding behavior, we can create this element with zero library cost. It's interoperable with evergreen browsers and fundamentally indistinguishable from the regular detailed element. Web components have access to the element lifecycle, allowing us to know when an element is being created, added, or removed from the page.

Makes a lot of sense, and it's really handy. I want to write my details. Now, this will be very similar to the regular details element, but with one key difference. This is what it will look like as a web component. It can't look exactly the same. We have to use this slot thing here, but I'll mention how that works later on.

So my fundamental difference of my details versus details is that these details should open upwards. Now, this may not be the most important thing on the web, but I thought it was a pretty fairly cute example to show just how powerful web components are, that I can reproduce a built-in component, a cute little widget, but do something completely different. So I want to talk through how this works. Fundamentally, we create this class here, and then follow on by defining its inner HTML of something called the shadow root, which I'll get to a little bit later. But I create some code here. I then grab some references to some elements within that shadow root and add some behavior.

Now this behavior is pretty standard stuff. When I click on this element here, I want the other stuff to toggle state. And so I'm going to open and close this element by making it hidden or not hidden, and I'm also going to change the emoji representation of whether the element is open. So we've built this great little element, and the best part of it for me is that it has zero library cost. Obviously, the element has a bit of code and that needs to run, but it costs you nothing else. There's no library to attach or download or include. And every evergreen browser, about 95% of all browsers today, supports this out of the box. This is something you can just use. The next best part is this is just a standard way of interoperability. Turns out my details, if we do a little bit more work to make it a bit more polished, will be fundamentally indistinguishable from the regular detailed element. Nothing you use should really care that it's my details versus details. Of course, it has different functionality, but you know, it looks, smells, tastes, quacks, whatever metaphor you like. Exactly the same as anything else. So, these elements are just like HTML. And the reason they're just like HTML is because we have access to these two basic components of Web components. Firstly, we have access to the element lifecycle. We know when an element is being created, that's in the constructor. We know when it's being added to the page and removed from the page.

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
There has been no shortage of both fair and unfair criticism toward Web Components from a wide range of folks that build for the web, including but not limited to JavaScript Framework authors in supposed competition with the platform. In this talk I'll show you how to navigate and simplify the multifaceted landscape of web components, satisfying common criticisms and showing how you can Use the Platform most effectively today.
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
Over the past few years, the number of web frameworks available to us has exploded. In some ways, the breadth of choice is a clear win for our ecosystem. However, for many of us, it also comes with harsh drawbacks: - Have you ever used a popular open-sourced component built for framework A, and wished it existed in framework B? What about a design system library? - Does your company have frontends built in different frameworks, and your web teams are frustrated about the wasted hours needed to achieve a consistent design system? - Does your team build SDKs for web frameworks, and must manually re-write them for each framework? The solution to all 3 of these problems exists today. To fully understand it, we must first examine today’s web frameworks, re-think what a component should look like, and introduce a new Intermediate Representation of our components. This is what we have done at Builder.io when we created Mitosis, and we’re excited to share it with everyone.
Authoring HTML in a JavaScript World
React Summit US 2023React Summit US 2023
21 min
Authoring HTML in a JavaScript World
 In this talk, Tony shows how an authoring and semantic HTML-first approach to JSX template work leads to more readable, maintainable, and accessible UI components. He demonstrates how to think through implementing a UI prototype in a semantic way, authoring HTML before visuals. He shows how accessible markup improves performance, reduces DOM size, minimizes time spent on CSS, and reduces cognitive load not only for developers, but also for all our users, no matter how they consume our sites and applications.
Immutable Web Apps
JSNation 2022JSNation 2022
20 min
Immutable Web Apps
Resolving dependencies when they are all bundled together is easy. Resolving dependencies when they are in being loaded via script tags is much more challenging. The goal of this talk is to explain how Meltwater handles dependency resolution when building native Web Component based applications that rely on packages published by many different teams.
Web Components are awesome!
JSNation 2022JSNation 2022
10 min
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.
Let's talk about Web Components
React Advanced Conference 2021React Advanced Conference 2021
32 min
Let's talk about Web Components
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.

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.