Class SizeTrigger

java.lang.Object
com.vaadin.flow.component.trigger.internal.Trigger
com.vaadin.flow.component.trigger.internal.SizeTrigger
All Implemented Interfaces:
Serializable

public class SizeTrigger extends Trigger
Fires when the host component's root element changes size, as reported by the browser's ResizeObserver API. The bound actions run inside the observer callback with a synthetic event whose width and height hold the rounded pixel size of the element's content box.

The size values are exposed as static Action.Input sources on SizeTrigger.EventData. The whole-event source SizeTrigger.EventData.size is shaped to match the Size record so it can be deserialised directly on the server when forwarded through an action that decodes its input on the server.

Example ? mirror the element's pixel size into two display fields:


 SizeTrigger resize = new SizeTrigger(panel);
 resize.triggers(
         new SetPropertyAction<>(widthSpan, "textContent",
                 SizeTrigger.EventData.width),
         new SetPropertyAction<>(heightSpan, "textContent",
                 SizeTrigger.EventData.height));
 

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

Since:
25.2
See Also:
  • Constructor Details

    • SizeTrigger

      public SizeTrigger(Component host)
      Creates a size trigger on the given host component's root element.
      Parameters:
      host - the component whose root element is observed, not null
    • SizeTrigger

      public SizeTrigger(Element host)
      Creates a size trigger on the given element.
      Parameters:
      host - the element that is observed, not null
      Since:
      25.3
  • Method Details

    • install

      protected Registration install(JsFunction action)
      Description copied from class: Trigger
      Installs the given rendered action as a client-side listener and returns the Registration that detaches it. Called once per action passed to Trigger.triggers(Action...).

      Implementations typically pass getHost().addJsInitializer an initializer that hands the action JsFunction to whatever client API the trigger wraps. The initializer is declared as a snippet, so that nothing is compiled from a string in the browser:

       @JsExpression("this.addEventListener($1, $0);"
               + "return () => this.removeEventListener($1, $0);")
       JsFunction listenToEvent(JsFunction action, String eventName);
      
       // In install, with LISTEN_JS = JsSnippets.of(ListenJs.class)
       return getHost().addJsInitializer(
               LISTEN_JS.listenToEvent(action, eventName));
       
      The initializer runs with this bound to the host element, and returns the function that uninstalls it. The arguments of the snippet are made available as $0, $1, ? in the order passed.
      Specified by:
      install in class Trigger
      Parameters:
      action - the rendered action JsFunction; takes one runtime argument named event (the trigger's event payload), not null
      Returns:
      the registration whose Registration.remove() detaches the listener, not null