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.
I'm a normal child of a normal
<div>.
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');
- Declarative: the structure is plain HTML, it works in server-rendered markup, and the slotted sections are never shown as ordinary content first, because the shadow root exists before they are parsed.
attachShadow(): no special markup, and it works on elements you don't control. Until the script runs the sections render as normal page content, so they can flash in place before moving into the 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)
| Result | Experiment | Detail |
|---|
Findings
- A slot in a canvas draws ordinary DOM. It needs a shadow root and it doesn't need a custom element. With declarative shadow DOM the structure is HTML, and the only script is the
painthandler. - A
<slot>in the light DOM does nothing. Outside a shadow tree it's treated like an unknown element, so<canvas><slot name="x">in a normal document never picks upslot="x"content from elsewhere. This is the part of my original idea that the platform doesn't support. - Projection is limited to the host's children. A shadow root on
<body>makes the whole page the host, but only for top-level elements. A deeply nested element can't be slotted without moving it. - The slotted element needs
drawable, or the slot needs a drawable wrapper around it in the shadow tree. Several elements can share one slot name and each is drawable on its own. - Slot fallback content draws too, so a canvas can ship default content that authors override by slotting.
- What a native version might look like: if canvas children could reference elements by id (like
popovertargetoraria-owns), any element could be drawn with no shadow root. I think it's worth raising on the WICG repo.