Package org.jboss.elemento
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:
HTMLContainerBuilder- for container elements that can hold childrenHTMLElementBuilder- for simple HTML elementsHTMLInputElementBuilder- specialized builder for input elementsHTMLSelectElementBuilder- specialized builder for select elementsHTMLTextAreaElementBuilder- specialized builder for textarea elements
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:
Elements.lazyAppend(elemental2.dom.Element, elemental2.dom.Element)- append if not already presentElements.insertBefore(elemental2.dom.Element, elemental2.dom.Element)- insert element before anotherElements.insertAfter(elemental2.dom.Element, elemental2.dom.Element)- insert element after anotherElements.insertFirst(elemental2.dom.Element, elemental2.dom.Element)- insert as first childElements.removeChildrenFrom(elemental2.dom.Element)- remove all childrenElements.failSafeRemoveFromParent(elemental2.dom.Element)- safely remove from parent
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
- Visibility control -
Elements.isVisible(elemental2.dom.HTMLElement),Elements.setVisible(elemental2.dom.HTMLElement, boolean) - Resize observers -
Elements.resizeObserver(elemental2.dom.HTMLElement, ResizeCallback) - Element queries -
Elements.querySelector(elemental2.dom.Node, By),Elements.querySelectorAll(elemental2.dom.Node, By) - Utility classes -
Id,Key,Role,InputType,ButtonType
- See Also:
-
ClassDescriptionInterface to get notified when an element has been attached / detached from the DOM.AttachableElement<E extends HTMLElement>Union interface that combines
AttachableandIsElementand makes the registration simpler and more meaningful.Typesafe CSS selector API.Operator used for attribute selectors.General purpose functional interface representing a callback function.Provides methods and default implementations for modifying the attributes of an element.Provides methods and default implementations for modifying the class list of an element.Provides methods and default implementations for modifying the element using consumers.Delegates all methods fromElementContainerMethodsto the element returned byElementContainerDelegate.containerDelegate().Provides methods and default implementations for adding elements.Provides methods and default implementations for listening to element events.Provides methods and default implementations for modifying the inner HTML of an element.Provides methods and default implementations for modifying element IDs.ElementQueryMethods<E extends Element>Provides methods and default implementations for finding elements.Builder and helper methods for working withHTMLElements and/orIsElement.Delegates all methods fromElementTextMethodsto the element returned byElementTextDelegate.textDelegate().Provides methods and default implementations for modifying the text of an element.EventCallbackFn<E extends Event>Event handler callback.Known event types used inElementEventMethods.on(EventType, EventCallbackFn).HTMLContainerBuilder<E extends HTMLElement>Builder for container-like HTML elements.Provides methods and default implementations for modifying the attributes of an HTML element.HTMLElementBuilder<E extends HTMLElement>Builder for simple HTML elements.Provides methods and default implementations for modifying the data attributes of an HTML element.Provides methods and default implementations for modifying the style of an HTML element.Provides methods and default implementations for modifying the visibility of an HTML element.HTMLInputElementBuilder<E extends HTMLInputElement>Builder for input elements.Provides methods and default implementations for modifying the attributes of an HTML input element.HTMLSelectElementBuilder<E extends HTMLSelectElement>Builder for input elements.Provides methods and default implementations for modifying the attributes of an HTML input element.HTMLTextAreaElementBuilder<E extends HTMLTextAreaElement>Builder for input elements.Provides methods and default implementations for modifying the attributes of an HTML text area element.Helper methods for working with IDs.Known input types used inElements.input(InputType).Interface for custom components returning a single element.Selected key values according to https://www.w3.org/TR/DOM-Level-3-Events-key/.The ResizeObserver interface reports changes to the dimensions of an Element's content or border box.Roles as recommended by the MDN.Base interface for all typed builders.