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 🎅

This talk has been presented at JSNation Live 2021, check out the latest edition of this JavaScript Conference.

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
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.
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.
Let's talk about Web Components
React Advanced Conference 2021React Advanced Conference 2021
32 min
Let's talk about Web Components
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.

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.