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 through width(), height() and size() so downstream actions can consume them. The shape of the value produced by size() matches 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", resize.width()),
         new SetPropertyAction<>(heightSpan, "textContent", resize.height()));
 

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

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
  • Method Details

    • width

      public Action.Input<Integer> width()
      event.width — the element's rounded content-box width in pixels.
    • height

      public Action.Input<Integer> height()
      event.height — the element's rounded content-box height in pixels.
    • size

      public Action.Input<Size> size()
      The synthetic event object as a whole, shaped as {width, height} — Jackson-deserialisable into the Size record when consumed by an action that decodes its input on the server.
    • 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 call getHost().addJsInitializer with an install expression that hands the action JsFunction to whatever client API the trigger wraps:

      
       return getHost().addJsInitializer(
               "this.addEventListener($1, $0);"
                       + "return () => this.removeEventListener($1, $0);",
               action, eventName);
       
      The expression runs with this bound to the host element. The captures 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