Interface Attachable

All Known Subinterfaces:
AttachableElement<E>

public interface Attachable
Interface to get notified when an element has been attached / detached from the DOM.

Important:
The attach(MutationRecord) / detach(MutationRecord) methods are only called once after the attachable implementation has been registered with register(HTMLElement, Attachable). If you hold a reference to the element, and the element is attached to the DOM again, the callbacks are not called again, unless the element has been registered again. This is done for reasons of optimisation and performance.

Performance characteristics

Elemento uses a single global MutationObserver on document.body (configured with childList: true, subtree: true), not one observer per element. This follows the recommended pattern from the DOM spec and is the same approach used by libraries like lit-element.

Each registration has one-shot semantics: when the callback fires, the observer entry is immediately removed from the internal list and the UID attribute is cleaned from the element. This prevents the observer lists from growing unboundedly. After initial page render, the lists are typically empty.

On each DOM mutation, the observer iterates the pending list and checks whether each registered element is among the added/removed nodes — either by direct reference or via querySelector on the added subtree. The cost is proportional to the number of pending (not yet attached) registrations, which drains to zero as elements enter the DOM.

In component libraries where parent components and their sub-components both implement Attachable (e.g., a menu with N menu items), this results in 1 + N registrations per component tree. Because of the one-shot semantics, these all drain in a single microtask batch during initial render. For typical usage (tens of components), the performance impact is negligible.

public class Greeting implements AttachableElement<HTMLElement> {

    private final HTMLElement root;

    Greeting(String name) {
        root = div().text("Hello " + name).element();
        Attachable.register(this);
    }

    @Override
    public void attach(MutationRecord mutationRecord) {
        console.log("Greeting has been attached to the DOM!");
    }

    @Override
    public HTMLElement element() {
        return root;
    }

    public static void init() {
        body().add(new Greeting("Smilla"));
    }
}
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    attach(MutationRecord mutationRecord)
    Called if the element has been attached.
    default void
    detach(MutationRecord mutationRecord)
    Called if the element has been detached.
    static void
    register(HTMLElement element, Attachable attachable)
    Registers the specified element for both attach and detach.
    static <E extends HTMLElement>
    void
    register(AttachableElement<E> attachableElement)
    Registers the specified element for both attach and detach.
    static <E extends HTMLElement>
    void
    register(IsElement<E> element, Attachable attachable)
    Registers the specified element for both attach and detach.
    static void
    Unregisters the specified element for both attach and detach.
  • Method Details

    • register

      static void register(HTMLElement element, Attachable attachable)
      Registers the specified element for both attach and detach.
    • register

      static <E extends HTMLElement> void register(IsElement<E> element, Attachable attachable)
      Registers the specified element for both attach and detach.
    • register

      static <E extends HTMLElement> void register(AttachableElement<E> attachableElement)
      Registers the specified element for both attach and detach.
    • unregister

      static void unregister(HTMLElement element)
      Unregisters the specified element for both attach and detach.
    • attach

      void attach(MutationRecord mutationRecord)
      Called if the element has been attached.
    • detach

      default void detach(MutationRecord mutationRecord)
      Called if the element has been detached. Default implementation does nothing.