java.lang.Object
com.vaadin.flow.component.trigger.internal.Trigger
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
DomEventTrigger

public abstract class Trigger extends Object implements Serializable
Something that fires on the client when some condition is met — a DOM event, a signal change, an observer firing, a timer elapsing, an idle timeout, a BroadcastChannel message, a media-query match, … — and, when it does, runs one or more actions.

Actions run synchronously inside the browser's handler when the trigger fires; for triggers that originate from a user gesture (click, keypress, …) this preserves the gesture context for downstream actions, letting them invoke browser APIs that require it (clipboard, fullscreen, file picker, share, …). Note that most such APIs are themselves asynchronous: the action is dispatched synchronously, but any server-observable effect — for example, a callback reporting whether navigator.clipboard.writeText resolved — may reach the server arbitrarily later than the gesture itself, after one or more event-loop turns.

Each call to triggers(Action...) produces one addJsInitializer registration on the host element; remove() removes all such registrations. Subclasses provide the JS that installs and tears down the listener by overriding installJs(); the action handler is exposed to that JS as a JsFunction captured at $0.

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

See Also:
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a new trigger bound to the given host component's root element.
  • Method Summary

    Modifier and Type
    Method
    Description
    final Element
    The host element this trigger fires on.
    protected abstract String
    Builds the JS expression that installs this trigger's listener and returns a cleanup function that removes it.
    final void
    Removes this trigger and all wirings created from it.
    final void
    triggers(Action... actions)
    Wires the given actions to this trigger.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Trigger

      protected Trigger(Component host)
      Creates a new trigger bound to the given host component's root element.
      Parameters:
      host - the component whose root element the trigger fires on, not null
  • Method Details

    • getHost

      public final Element getHost()
      The host element this trigger fires on.
      Returns:
      the host element, never null
    • triggers

      public final void triggers(Action... actions)
      Wires the given actions to this trigger. They run in the order given the next time this trigger fires. Each call adds another wiring; the existing ones are kept.
      Parameters:
      actions - the actions to run, not null or empty
    • installJs

      protected abstract String installJs()
      Builds the JS expression that installs this trigger's listener and returns a cleanup function that removes it.

      The expression runs with this bound to the host element. The handler JsFunction is available as $0; subclasses pass it to whatever client API the trigger wraps (e.g. this.addEventListener(name, $0)) and reference the same $0 in the cleanup callback to detach it.

      Returns:
      the JS install expression, not null
    • remove

      public final void remove()
      Removes this trigger and all wirings created from it. The corresponding client-side listeners are detached as part of the next synchronisation.