Class ElementEffect

java.lang.Object
com.vaadin.flow.dom.ElementEffect

public final class ElementEffect extends Object
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.effect(Runnable), 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.

Since:
25.0
  • Constructor Details

    • ElementEffect

      public ElementEffect(Element owner, Runnable effectFunction)
  • Method Details

    • effect

      public static Registration effect(Element owner, Runnable 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.value());
       });
       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:
      • Signal.effect(Runnable)
    • bind

      public static <T> Registration bind(Element owner, com.vaadin.signals.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:
      • Signal.effect(Runnable)
    • close

      public void close()