Skip to the content.

#patterns #integrations #getting-started

Version: Applicable for Datastar v1.0.0-RC.5

Overview

Third-party libraries or custom scripts sometimes need to know when Datastar has fully initialized before they can safely interact with Datastar-managed elements or signals. This guide shows how to dispatch a custom event when Datastar is ready.

Implementation

Use the data-on-load attribute to dispatch a custom event when Datastar loads an element into the DOM:

<div data-on-load="document.dispatchEvent(new CustomEvent('datastar-loaded', {bubbles: true}))"></div>

Place this element in your HTML where you want to signal Datastar’s initialization (typically near the top of your <body> or after including the Datastar script).

How It Works

Attribute Explained

data-on-load

Flow

  1. Element Load: When Datastar processes the element with data-on-load, it executes the expression
  2. Custom Event: document.dispatchEvent(new CustomEvent('datastar-loaded', {bubbles: true})) creates and dispatches a custom event that bubbles up through the DOM
  3. Third-party integration: External scripts can listen for this event to know when Datastar is ready

Listening for the Event

Third-party libraries or scripts can listen for the event:

document.addEventListener('datastar-loaded', () => {
  console.log('Datastar is now loaded and ready');
  // Initialize third-party library that depends on Datastar
  initMyLibrary();
});

To handle cases where the script loads after Datastar:

let datastarReady = false;

document.addEventListener('datastar-loaded', () => {
  datastarReady = true;
  initMyLibrary();
});

// Fallback: check if we missed the event
setTimeout(() => {
  if (!datastarReady && document.querySelector('[data-on-load]')) {
    // Datastar likely already loaded
    initMyLibrary();
  }
}, 100);

Use Cases

  1. Analytics Integration: Initialize analytics that track Datastar signal changes
  2. UI Component Libraries: Bootstrap components that interact with Datastar’s reactive system
  3. Polyfills/Feature Detection: Execute code that needs to run after Datastar initializes
  4. Web Components: Signal custom elements that they can safely interact with Datastar
  5. Development Tools: Initialize debugging or development utilities that monitor Datastar

Notes

Alternative Approaches