Class ElementEffect

java.lang.Object
com.vaadin.flow.dom.ElementEffect
All Implemented Interfaces:
Serializable

public final class ElementEffect extends Object implements Serializable
The utility class that provides helper methods for using Signal effects in a context of a given element's life-cycle.

It ultimately creates a Signal effect, i.e. a call to Signal.unboundEffect(EffectAction), that is automatically enabled when an element is attached and disabled when the element is detached. Additionally, it provides methods to bind signals to element according to a given value setting function.

For internal use only. May be renamed or removed in a future release.

Since:
25.0
See Also:
  • Constructor Details

  • Method Details

    • effect

      public static Registration effect(Element owner, EffectAction effectFunction)
      Creates a Signal effect that is owned by a given element. The effect is enabled when the element is attached and automatically disabled when it is detached.

      Example of usage:

       Registration effect = ElementEffect.effect(myElement, () -> {
           Notification.show("Element is attached and signal value is "
                   + someSignal.get());
       });
       effect.remove(); // to remove the effect when no longer needed
       
      Parameters:
      owner - the owner element for which the effect is applied, must not be null
      effectFunction - the effect function to be executed when any dependency is changed, must not be null
      Returns:
      a Registration that can be used to remove the effect function
      See Also:
    • bind

      public static <T extends @Nullable Object> Registration bind(Element owner, Signal<T> signal, SerializableBiConsumer<Element,T> setter)
      Binds a signal's value to a given owner element in a way defined in setter function and creates a Signal effect function executing the setter whenever the signal value changes.

      Example of usage:

       Element mySpan = new Element("span");
       Registration effect = ElementEffect.bind(mySpan, stringSignal,
               Element::setText);
       effect.remove(); // to remove the effect when no longer needed
      
       ElementEffect.bind(mySpan, stringSignal.map(value -> !value.isEmpty()),
               Element::setVisible);
       
      Type Parameters:
      T - the type of the signal value
      Parameters:
      owner - the owner element for which the effect is applied, must not be null
      signal - the signal whose value is to be bound to the element, must not be null
      setter - the setter function that defines how the signal value is applied to the element, must not be null
      Returns:
      a Registration that can be used to remove the effect function
      See Also:
    • close

      public void close()
    • bindChildren

      public static <T extends @Nullable Object, S extends Signal<T>> Registration bindChildren(Element parentElement, Signal<List<S>> list, SerializableFunction<S,Element> childFactory)
      Binds a list signal containing child signals to a parent component using a child component factory. Each signal in the list corresponds to a child component within the parent.

      The parent component is automatically updated to reflect the structure of the list signal. Changes to the list, such as additions, removals, or reordering, will update the parent's children accordingly.

      The parent component must not contain any children that are not part of the list signal. If the parent has existing children when this method is called, or if it contains unrelated children after the list changes, an IllegalStateException will be thrown.

      New child components are created using the provided childFactory function. This function takes a signal from the list and returns a corresponding Component. It shouldn't return null. The signal can be further bound to the returned component as needed. Note that childFactory is run inside a Effect, and therefore Signal.get() calls makes effect re-run automatically on signal value change.

      Type Parameters:
      T - the value type of the Signals in the list
      S - the type of the Signals in the list
      Parameters:
      parentElement - target parent element, must not be null
      list - list signal to bind to the parent, must not be null
      childFactory - factory to create new element, must not be null
      Throws:
      IllegalStateException - thrown if parent element isn't empty