Labels & rendering

A new control. The same reference.

The outside label references the component. Svelte renders the checkbox inside its shadow root.

Try: Click the label, replace the checkbox, then click the label again. Each replacement starts unchecked.

Follow a rendered target

DOM observationWaiting for Svelte to render.

Popover targeting

An outside button. A rendered panel.

Svelte creates a native popover inside another component. The outside buttons reference its host.

Try: Open the popover, then dismiss it with its close button or Escape.

Open an internal popover

DOM observationWaiting for Svelte to render.
How the examples are connected

Each component sets its shadow root’s referenceTarget to the internal target ID. The fallback loads before the component definitions, so it can capture the roots when the renderer creates them and observe later DOM changes.

The page waits for the renderer’s first update before enabling its buttons. Replacing the checkbox changes the component’s revision attribute; the renderer creates a new input with the same ID.

<label for="renderer-checkbox">Send me release notes</label>
<rt-svelte-checkbox id="renderer-checkbox" revision="0"></rt-svelte-checkbox>

<button type="button" popovertarget="renderer-popover">Open popover</button>
<rt-svelte-popover id="renderer-popover"></rt-svelte-popover>

The selected setup uses labels({ activation: "focus-and-click", naming: true }) and popoverTargets(). The page’s event listeners only display state; they do not forward label or popover actions.

Component source

checkbox.svelte

<svelte:options customElement={{
  shadow: "open",
  props: { revision: { type: "Number", reflect: true } },
  extend: Base => class extends Base {
    constructor() {
      super();
      this.shadowRoot.referenceTarget = "control";
    }
  },
}} />

<script>
  import { onMount } from "svelte";
  import componentStyles from "../shared/components.css";

  let { revision = 0 } = $props();

  onMount(() => {
    const host = $host();
    host.setAttribute("data-renderer-ready", "");
    host.dispatchEvent(new Event("renderer-ready"));
    return () => host.removeAttribute("data-renderer-ready");
  });
</script>

<svelte:element this={"style"}>{componentStyles}</svelte:element>
<div class="component-preview">
  {#key revision}
    <input id="control" type="checkbox" data-revision={revision}>
  {/key}
  <span aria-hidden="true">Native checkbox</span>
</div>
<p class="hint">Render revision {revision}</p>

popover.svelte

<svelte:options customElement={{
  shadow: "open",
  extend: Base => class extends Base {
    constructor() {
      super();
      this.shadowRoot.referenceTarget = "panel";
    }
  },
}} />

<script>
  import { onMount } from "svelte";
  import componentStyles from "../shared/components.css";

  onMount(() => {
    const host = $host();
    host.setAttribute("data-renderer-ready", "");
    host.dispatchEvent(new Event("renderer-ready"));
    return () => host.removeAttribute("data-renderer-ready");
  });
</script>

<svelte:element this={"style"}>{componentStyles}</svelte:element>
<div id="panel" popover="auto" role="dialog" aria-labelledby="panel-title">
  <p class="eyebrow">Svelte custom element · shadow DOM</p>
  <h2 id="panel-title">Rendered with Svelte</h2>
  <p>This native popover lives inside a compiler-generated Svelte custom element.</p>
  <button type="button" popovertarget="panel" popovertargetaction="hide">Close popover</button>
</div>

components.js

import Checkbox from "./checkbox.svelte";
import Popover from "./popover.svelte";
import { whenRendererEvent } from "../shared/renderer-readiness.js";

customElements.define("rt-svelte-checkbox", Checkbox.element);
customElements.define("rt-svelte-popover", Popover.element);

// Svelte mounts its inner component on the next tick after connection. The
// onMount signal waits for that actual render, including its native targets.
export async function whenReady() {
  const hosts = ["renderer-checkbox", "renderer-popover"].map(id => {
    const host = document.getElementById(id);
    if (!host) throw new Error(`Missing Svelte demo host: ${id}`);
    return host;
  });
  await Promise.all(hosts.map(host => whenRendererEvent(host, "Svelte")));
}

Functional JavaScript sizes

Functional page JavaScript

64.011 KB 23.887 KB gzip

Always loaded.

Fallback additional

23.929 KB 8.430 KB gzip

Core + selected adapters.

Fallback route total

87.940 KB 32.317 KB gzip

Always-loaded and fallback files; shared files counted once.

Native-surface route total

65.000 KB 24.425 KB gzip

Includes 0.989 KB (0.538 KB gzip) of behavioral probes requested only after the property surface is present.

Minified / gzip. 1 KB = 1,000 bytes. Demo-only syntax highlighting is excluded from the functional totals and reported separately below.

Whole-page delivery context

HTML document

19.554 KB 1 request

Generated markup, including these measurements.

Initial supporting assets

18.520 KB raw 17.897 KB local transfer · 3 requests

Styles, icon, and the highlighting scheduler; functional JS is separate above.

Initial page with fallback

126.014 KB raw 69.768 KB local transfer · 9 requests

Document + functional fallback path + initial supporting assets, with unique files counted once.

Deferred highlighting

20.851 KB raw 9.402 KB local transfer · 11 requests

Engine, theme, and this page’s grammar closure, requested after a source disclosure opens or during idle time.

“Local transfer” matches the included development server: gzip JavaScript sidecars, but uncompressed HTML, CSS, and SVG. GitHub Pages, browser caching, HTTP headers, and production compression can change transferred bytes and request scheduling. Module preloads fetch functional modules early without evaluating the app and do not add duplicate transfers.

Inspect the 6 generated JavaScript files

Selected adapters: labels, popover-targets.

Gzip totals sum each compressed file. The fallback and native-probe routes are mutually exclusive. Separate CSS files, HTML, JSON, source maps, HTTP headers, and Microlighter assets are excluded. Component styles embedded in JavaScript are included. Transfer sizes depend on compression and caching. Microlighter MIT license.

Files included in this page’s totals
FileDeliveryMinifiedGzip
shared/chunks/app-QN33QWVY.js Page 60.612 KB 22.397 KB
shared/chunks/chunk-CLCOZ6O7.js Fallback / native probe shared 0.849 KB 0.422 KB
shared/chunks/chunk-S2QLMWTP.js Page 0.142 KB 0.138 KB
shared/chunks/detect-X6QVIAY4.js Native-surface probe 0.140 KB 0.116 KB
shared/chunks/reference-target.setup-F3BB382F.js Fallback additional 23.080 KB 8.008 KB
svelte/main.js Page 3.257 KB 1.352 KB

Inspect the size manifest

Renderer integration & limitations

Svelte and Reference Target

Svelte compiles these components into custom element classes during the build. Its extend option sets referenceTarget on the generated shadow root, a keyed block replaces the checkbox, and onMount signals first-render readiness. Mounting and destruction follow Svelte’s deferred custom element lifecycle.

The library and its component code are included in Page JavaScript above. Each renderer is built as an independent page with only the labels and popover-targets adapters in its optional fallback bundle.

These examples use open, client-rendered roots. They do not test server rendering or hydration. The label fallback does not recreate native label.control or input.labels relationships. Readouts describe DOM state; computed names and assistive technology need separate validation.

Svelte documentation ↗