Skip to main content
Dublin Library

The Publishing Project

Web Components for Publishing

 

I've been playing with web components for a few years. Until not too long ago the biggest drawback was that only one browser supported the original (v0) family of Web Component specifications. Things are looking better now. V1 of most web component specifications, Templates, shadow DOM and Custom Elements, are supported in most major browsers with HTML Imports being the only part of Web Components where vendors have not agreed on implementation and are waiting for ES6 modules to become more widely adopted before deciding where the future of this specification lies. ## What are Web Components For those of you who are not familiar with them. Web Components is a set of four specifications being worked on at W3C and WHATWG. The specs are: - Custom Elements - shadow DOM - Templates - HTML Imports On their own each of these technologies enhance web development. **Custom Elements** allow developers to create new HTML elements and integrate them into existing web content. **shadow DOM** allows for encapsulation. The content you create is hidden from end user's scripts and stylesheets. The styles in your element cannot style elements outside and vice versa. **Templates** give us the possibility of creating inert HTML that will only become live when the custom element the template is attached to the live DOM on the browser. **HTML Imports** are a declarative way to include external resources in our pages. There is very little interest from browser vendors other than Google to implement HTML Imports. Because of this we need to wait until an alternative proposal like \[HTML Modules\](HTML Imports and ES Modules), a reformulation of HTML Imports based on ES2015 modules. Until then remember, polyfills are your friends. We'll explore how to combine these 4 technologies (as currently implemented and polyfilled) to create composable and reusable content that makes it easier to create book-like resources. ## Assumptions Before diving into what we're building I want to touch on some of the assumptions I'm making. Because web components must have a `-` character as part of the name and must start with a lowercase character (see the [spec](https://html.spec.whatwg.org/multipage/scripting.html#valid-custom-element-name), I've chosen to use `wcp-` for all the web components I'm building as part of this project. All styles, content and scripts will be hidden inside the component's shadow DOM. It's not 100% foolproof but it will hide the element's styles and scripts from most casual scans and, I hope, that will be enough to discourage overrides as the overrides will become more complicated if they have to pierce the shadow boundary, it's possible but I wonder if it'll be worth the effort. Web components should be fully interoperable so we can look at the catalog at [webcomponents.org](https://www.webcomponents.org/) to look for additional components to test the hypothesis with. I'm particularly interested in the [Google elements](https://www.webcomponents.org/author/GoogleWebComponents) that provide declarative access to Google APIs and resources. Because we're working with modern web technologies we must polyfill depending on the browser we use, not all browsers support all the Web Components specifications. See below for the polyfills' complete browser support matrix: | Polyfill | IE11+ | Chrome\* | Firefox\* | Safari 9+\* | Chrome Android\* | Mobile Safari\* | | --- | --- | --- | --- | --- | --- | --- | | Custom Elements | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | HTML Imports | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Shady CSS/DOM | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | \*Indicates the current version of the browser ## Making the case for web components in publishing Let's think of web components as patterns: reusable pieces of code that we can use in different pages and contexts. We can also come to an agreement on how we build those components and how much we choose to override them as content creators, publishers and tool creators. An example of this is Google's [Accelerated Mobile Pages (AMP) project](https://www.ampproject.org/). They've subset HTML, provided their own set of custom elements (AMP HTML) and Javascript to handle ads and other potential performance bottlenecks. While AMP is geared towards mobile performance, the proposed set of components seeks to provide a starting point for common development and a unified user interface where vendors don't feel the need to override all properties. Because we encapsulate the components inside a shadow DOM we protect the content from outside misuse. This is similar but not identical to what browser vendors themselves use to compose elements like `