Package org.jboss.elemento


package org.jboss.elemento
Elemento simplifies working with GWT Elemental2 by providing a fluent API for building HTML element hierarchies, typesafe CSS selectors, and helper methods for common DOM operations.

Builder API

The core of Elemento is its fluent builder API for creating HTML element hierarchies. The Elements class provides factory methods for all common HTML elements:

import static org.jboss.elemento.Elements.*;
import static org.jboss.elemento.InputType.checkbox;

HTMLElement section = section().css("main")
        .add(input(checkbox).id("toggle-all").css("toggle-all"))
        .add(label()
                .apply(l -> l.htmlFor = "toggle-all")
                .text("Mark all as complete"))
        .add(ul().css("todo-list")
                .add(li()
                        .add(div().css("view")
                                .add(input(checkbox).css("toggle").checked(true))
                                .add(label().text("Taste Elemento"))
                                .add(button().css("destroy")))
                        .add(input(text).css("edit"))))
        .element();

Builders are organized into these main classes:

All builders implement TypedBuilder and are composed from method interfaces like ElementAttributeMethods, ElementClassListMethods, ElementContainerMethods, etc. This design allows fine-grained control over which methods are available on each builder type.

Custom Elements

Implement IsElement to create custom components that return a single element:

class TodoItemElement implements IsElement<HTMLElement> {
    private final HTMLElement root;

    TodoItemElement(TodoItem item) {
        this.root = li()
                .add(div().css("view")
                        .add(input(checkbox).css("toggle"))
                        .add(label().text(item.text)))
                .element();
    }

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

Event Handling

Register event handlers using the fluent API or the EventType.bind(elemental2.dom.EventTarget, EventType, EventCallbackFn) method:

import static org.jboss.elemento.EventType.*;

// During element construction
HTMLElement button = button()
        .text("Click me")
        .on(click, event -> console.log("Clicked!"))
        .element();

// Or later using bind()
HandlerRegistration handler = bind(button, click, event -> alert("Clicked"));
// Remove handler when done
handler.removeHandler();

The Key enum provides constants for common keyboard codes to simplify keyboard event handling.

Attach and Detach Callbacks

Implement Attachable to receive notifications when elements are attached to or detached from the DOM tree:

class TodoItemElement implements IsElement<HTMLElement>, Attachable {
    private final HTMLElement root;

    TodoItemElement(TodoItem item) {
        this.root = li().element();
        Attachable.register(root, this);
    }

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

    @Override
    public void attach(MutationRecord mutationRecord) {
        console.log("Attached to DOM");
    }

    @Override
    public void detach(MutationRecord mutationRecord) {
        console.log("Detached from DOM");
    }
}

Elemento uses the MutationObserver API to detect DOM changes. Note that callbacks are only invoked once per registration.

DOM Manipulation

The Elements class provides helper methods for common DOM operations:

All methods are null-safe and support both Element and IsElement parameters.

Typesafe CSS Selectors

The By class provides a typesafe API for building CSS selectors:

import static org.jboss.elemento.By.AttributeOperator.STARTS_WITH;

// Simple selectors
By idSelector = By.id("main");
By classSelector = By.classname("external");

// Complex selector: a[href^="http://"] > .external
By selector = By.element("a")
        .and(By.attribute("href", STARTS_WITH, "http://"))
        .child(By.classname("external"));

// Use with query methods
HTMLElement element = querySelector(document.body, selector);
for (HTMLElement el : querySelectorAll(document.body, selector)) {
    // process elements
}

Iterators and Streams

Elemento provides methods to iterate over and stream DOM elements:

// Iterate over children
for (HTMLElement child : children(parent)) {
    // process child
}

// Stream over children
stream(parent)
        .filter(element -> element.classList.contains("active"))
        .forEach(element -> console.log(element));

Additional Features

See Also: