Web component / Examples

<script-dependent>

Reveal dependent controls when their module is ready. Read the README for the complete API.

Readiness, not just downloading

Give the wrapper a script URL and author it with hidden. If the module exports initialize(element, { signal }), the wrapper waits for it. Without that export, successful module evaluation is enough.

These word counters wait briefly to make loading visible. The text fields stay usable throughout. Both successful examples use the same initializer, but each gets its own setup.

Existing markup

The button and output already exist. They become available only after the module has attached their behavior. The field stays outside the gate.

<label for="existing-text">Your draft</label>
<textarea id="existing-text"></textarea>
<script-dependent script="./word-count.js" hidden>
  <button type="button">Count words</button>
  <p>Words: <output aria-live="polite">0</output></p>
</script-dependent>

You can edit the draft while the counter loads.

Inert template content

A single direct-child template keeps enhancement-only markup inert. After module evaluation, the wrapper instantiates it once and passes the resulting content to the initializer.

<label for="template-text">Another draft</label>
<textarea id="template-text"></textarea>
<script-dependent script="./word-count.js" hidden>
  <template>
    <button type="button">Count words</button>
    <p>Words: <output aria-live="polite">0</output></p>
  </template>
</script-dependent>

The template counter is loading; your draft is available.

Failure and recovery

This initializer deliberately rejects. The unavailable button stays hidden, and the ordinary link remains usable. retry() starts another attempt; this example will fail again rather than pretend to recover.

The status paragraph and hidden retry button are authored in the HTML, outside the gate. The demo JavaScript replaces the paragraph’s text and reveals the retry button when it receives script-dependent:error. The component emits the event; it doesn’t create messages or recovery controls.

<script-dependent id="failure" script="./failure.js" hidden>
  <button type="button">Open enhanced editor</button>
</script-dependent>
<p><a href="#existing-text">Use the ordinary text field</a></p>
<p id="failure-status" role="status">
  The ordinary text field is always available.
</p>
<button id="retry" type="button" hidden>Try again</button>

<script type="module">
  const gate = document.querySelector('#failure');
  const status = document.querySelector('#failure-status');
  const retry = document.querySelector('#retry');

  gate.addEventListener('script-dependent:error', () => {
    status.textContent =
      'The enhancement could not start. ' +
      'The ordinary text field is still available.';
    retry.hidden = false;
  });
  retry.addEventListener('click', () => gate.retry());

  await import('../define.js');
</script>

Use the ordinary text field

The ordinary text field is always available.