Just <slot> inside <canvas>

This page has no custom element and no library. A <slot name="…"> sits inside the canvas, and ordinary DOM with a matching slot="…" attribute is projected into it. It turns out a slot only does anything inside a shadow tree, so the canvas has to be in a shadow root. With declarative shadow DOM that is still plain HTML.

This demo uses declarative shadow DOM: the <template shadowrootmode="open"> in the markup. The same demo without it is further down.

slot="first"
I'm a normal child of a normal <div>.
slot="second"
The complete page for this demo, with declarative shadow DOM (paste it into a blank .html file)

Isn't <template> inert? Why is this canvas live?

A plain <template> is inert: its content sits in a separate fragment, never renders and never runs. A <template shadowrootmode="open"> is a different thing called declarative shadow DOM. It's an instruction to the HTML parser: “attach a shadow root to my parent and put my content in it.” The parser does that as it reads the page, and the <template> element itself never ends up in the document. So by the time any script runs, the canvas is an element inside host.shadowRoot, already laid out, and its slots work.

const host = document.getElementById('host');

host.querySelector('template');          // null: the parser consumed it
host.shadowRoot;                         // a ShadowRoot, made by the parser
host.shadowRoot.querySelector('canvas'); // the live canvas, already on screen

What the parser does not do is draw. A canvas shows nothing until script draws into it, so the page still needs the four small pieces of init code in the listing above: find the canvas in host.shadowRoot, size it with a ResizeObserver, draw the slotted elements in onpaint, and repaint on slotchange.

Only the parser does this. element.innerHTML = '<template shadowrootmode…>' leaves an ordinary inert template. From script, use setHTMLUnsafe(), Document.parseHTMLUnsafe() or attachShadow(). The live tests below check each of these claims.

The same demo without declarative shadow DOM: attachShadow()

Declarative shadow DOM is optional. This is the same page with no <template> at all: the script calls host.attachShadow() and fills the shadow root itself. The rest (the slotted sections, sizing, paint, slotchange) is the same.

The only lines that differ between the two versions:

Declarative shadow DOM (in the HTML)

<div id="host">
  <template shadowrootmode="open">
    <canvas content="drawable" layoutsubtree>
      <slot name="first"></slot>
      <slot name="second"></slot>
    </canvas>
  </template>
  …
</div>

<script type="module">
  const host = document.getElementById('host');
  const canvas = host.shadowRoot.querySelector('canvas');

attachShadow() (in the script)

<div id="host">
  …
</div>

<script type="module">
  const host = document.getElementById('host');
  const root = host.attachShadow({ mode: 'open' });
  root.innerHTML = `
    <canvas content="drawable" layoutsubtree>
      <slot name="first"></slot>
      <slot name="second"></slot>
    </canvas>`;
  const canvas = root.querySelector('canvas');
The complete page for the attachShadow() version (paste it into a blank .html file)

Why the <template>? Because a <slot> only projects content inside a shadow tree. See the same thing with no template at all: just <canvas><slot> in a plain page, what the browser does with it natively, and a one-script upgrade that makes it work.

Page-level slots: a shadow root on <body>

document.body can host a shadow root. Put the canvas and its named slots there, plus a default <slot> for everything else, and any top-level element on the page can opt in to the canvas by adding slot="stage". The page's markup is otherwise untouched. Open it on its own.

The complete page for this demo (paste it into a blank .html file)

It comes down to the five lines that call document.body.attachShadow(): a canvas holding <slot name="stage">, then a default <slot> so the rest of the page renders as before. Everything after that is the same sizing and paint code as above.

Does the bare pattern work? (live)

ResultExperimentDetail

Findings