liveBook · Manning

Chapter 8: Working with templates and slots

What you will learn in chapter 8 of Vanilla Web

The introduction explains why inline web-component markup may become less suitable as components and applications grow, then previews the chapter’s alternatives.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why inline web-component markup may become less suitable as components and applications grow, then previews the chapter’s alternatives.

Key terms: web components, innerHTML, DOM APIs

8.1 The Template Content HTML Element

This section introduces <template> as an inert, reusable HTML blueprint and explains how it relates to web components.

Key terms: <template>, DocumentFragment, live DOM, clone, structure, logic, Template Content elements, Custom Elements, shadow DOM

8.1.1 Usage

This section shows how to find a <template>, clone its content, and insert visible, independently modifiable instances into the DOM.

Key terms: <template>, id, content property, DocumentFragment, clone, template, cloned nodes, instances

8.1.2 The Need for Cloning Templates

This section explains why reusable template content must be cloned, how the two cloning methods differ, and what cloning means for inert content, custom elements, and event listeners.

Key terms: template.content, DocumentFragment, clone, cloneNode(true), document.importNode, deep copy, adopted, inert document, contents owner document, custom element

8.1.3 Advantages

This section explains why templates are useful for reusable markup, how their inert content behaves, and why they should not be used merely to hide content.

Key terms: separation of concerns, reduced duplication, template, cloned, parsed, inert, network requests, assistive technologies, search engines, hidden

8.2 Using Templates in Web Components

Introduces why web components need reusable markup and styles and identifies three ways to provide their markup.

Key terms: Web Components, encapsulated styles, DOM API, <template>, custom element

8.2.1 Defining a Template in the HTML document

Shows how a web component can use a template declared in the main HTML document and notes a limit of changing that template after cloning.

Key terms: <template> element, main HTML document, web components, server-rendered apps, clones, DOM

8.2.2 Using external HTML files

This section explains how web components can load templates from external HTML files, reuse the loaded template, and handle loading and error states.

Key terms: external HTML files, Fetch API, parsed template, static variable, connectedCallback, loading message, error, alternative content, base class, ExternalTemplateElement

8.2.3 Loading external CSS stylesheets

This section explains why external stylesheets in templates can cause a flash of unstyled content and outlines ways to reduce it.

Key terms: <template>, FOUC, Flash of Unstyled Content, external stylesheets, CSSStyleSheet, adopted stylesheets, <style>, preload, loading state

8.3 Rendering Dynamic Data in Templates

Explains how to put dynamic data into pre-parsed HTML templates by filling DOM placeholders or cloning a template for each item in a collection.

Key terms: <template>, DOM API, textContent, data-fill, innerHTML, clone, fragment, text nodes, <cart-summary>

8.3.1 Creating a Custom Template Engine

This section explains the template-engine rendering pattern and sketches a reusable interpolation helper for web components.

Key terms: template engine, template, data object, rendering function, shadow DOM, interpolate, double curly braces, regular expression, TreeWalker

8.3.2 Using Community Template Engines

This section explains when a community template engine is worth using in a web component, how rendering approaches differ, and where JSX fits.

Key terms: interpolate function, community template engine, partials, string-based engines, tagged template literal libraries, precompilation, lit-html, Mustache, shadow root, light DOM

8.4 Using Slots

Introduces slots as customization points in a web component’s shadow DOM and explains how default and named slots receive content.

Key terms: <slot>, shadow DOM, light DOM distribution, content projection, unnamed slot, default slot, named slot, name attribute, slot attribute

8.4.1 Fallback Content

This subsection explains when a slot displays its fallback content instead of content supplied by the page.

Key terms: slot, fallback content, page DOM, projected

8.4.2 Full example

A product-details example shows how named slots place consumer content, how fallback content appears, and how to inspect slot projection in developer tools.

Key terms: slots, product-details, fallback, slot projection, slot, reveal

8.4.3 The Flattened Tree

This subsection explains that slots affect a component’s rendered tree without moving page DOM children into the shadow DOM.

Key terms: flattened tree, shadow DOM, projected light DOM, page DOM children, slots

8.4.4 Multiple Elements in One Slot

This subsection explains that a slot can render multiple matching child elements in order.

Key terms: slot, slot attribute, default slot, projected

8.5 Styling Slotted Content

This section explains why slotted content keeps the styling of the document where it is defined, even when it appears inside a shadow DOM.

Key terms: slotted content, shadow DOM, projected

8.5.1 The ::slotted() pseudo-Element

Explains how ::slotted() styles slotted content and why its reach is limited.

Key terms: shadow DOM, slotted content, ::slotted(), encapsulation

8.5.2 Styling Strategies

This subsection names practical ways to style slotted content when ::slotted() is limited.

Key terms: ::slotted(), slot’s container, CSS custom properties, slotted content

8.6 Slots API

8.6.1 The slotchange Event

This section explains when a slot’s slotchange event fires and which changes do not trigger it.

Key terms: slotchange event, <slot>, direct nodes, slot attribute

8.6.2 assignedNodes and assignedElements

This subsection explains how to retrieve a slot’s projected content and how to get the content rendered through nested slots.

Key terms: assignedNodes(options), assignedElements(options), projected content, flatten: true, nested <slot>, options object

8.6.3 Practical Example: Tab Component

This section illustrates how a tab component builds and updates its tab buttons from slotted panels.

Key terms: <tab-container>, Slots API, slotted panels, data-tab-title, slotchange

8.6.4 Finding a Slot’s Assigned Slot

This subsection shows how to find the slot into which a main-document element is projected.

Key terms: assignedSlot, light DOM, projection point

8.7 Nested Slots

This section explains how content passes through nested component slots and why deeply nested slot chains need care.

Key terms: nested slots, outer-component, inner-component, slot hierarchies, slot chains

8.8 Slot Best Practices

This section gives practical guidelines for designing slots that are clear, usable, and limited to appropriate customization points.

Key terms: slot name, public API, README, JSDoc, fallback content, slot hierarchies, nested slots, slots, attributes, properties

8.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 7: Web components Chapter 9: Advanced web components →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.