0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Holds inert HTML that is parsed but not rendered. Covers cloning template.content, declarative shadow DOM with shadowrootmode, accessibility, and the VB components that read author templates.
Description
A <template> holds HTML that the browser parses but does not render. Its children go into a separate DocumentFragment, template.content, rather than into the page. Images in it do not load, scripts in it do not run, and its styles do not apply. Nothing happens until script clones the content and inserts the copy.
A template can appear almost anywhere, including inside <table>, <select> and <ul>, where most other wrappers are not allowed. That makes it the usual way to ship row, option or list-item markup for script to stamp out.
Example: Clone and Fill
Clone template.content with cloneNode(true), fill in the copy, then insert it. Each submit below adds a row built from the same template. The template itself never appears.
Template and target list
<ulid="tasks"aria-live="polite"></ul><templateid="task-row"><li><spanclass="task-name"></span><small>added just now</small></li></template>
Clone, fill, insert
const template = document.getElementById('task-row');functionaddTask(name) {const row = template.content.cloneNode(true); // a DocumentFragment row.querySelector('.task-name').textContent = name; // textContent, not innerHTMLdocument.getElementById('tasks').append(row);}
Use document.importNode(template.content, true) instead of cloneNode when the template lives in another document, such as a fetched HTML file. Fill text with textContent, never by concatenating user input into innerHTML.
Declarative Shadow DOM
With a shadowrootmodeattribute, a template does something different. While the HTML is parsed, it attaches a shadow root to its parent element and moves its content into that root. No JavaScript is needed, and the template element itself disappears.
Declarative shadow root
<article><templateshadowrootmode="open"><style>:host { display: grid; gap: var(--size-2xs); }</style><slotname="name"></slot><slot></slot></template><h2slot="name">Ada Lovelace</h2><p>Unnamed children go to the default slot.</p></article>
Declarative shadow roots only attach during parsing. A template with shadowrootmode inserted later through innerHTML stays an ordinary, inert template; use setHTMLUnsafe() or Document.parseHTMLUnsafe() for that. The <slot> page has a live demo and covers slots and ::slotted.
Templates Read by VB Components
Several VB components let you author part of their content as a <template> child. The component reads the template; you never clone it yourself. Because the content is inert, nothing in it renders or loads before the component upgrades.
Template content is not in the accessibility tree. Screen readers do not see it, and nothing in it can take focus until a clone is inserted into the page.
Clones are ordinary DOM once inserted. Give them the same headings, labels and names you would write by hand, and use unique id values per clone (a cloned id is duplicated every time).
Content added after load is not announced on its own. Insert it into an aria-live region, as the demo's list does, or move focus to it when the user needs to act on it.
A declarative shadow root is rendered and exposed to assistive technology like any other content.
Related
<slot> - Content projection in shadow roots, with a declarative shadow DOM demo