Class DomEventTrigger

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

public class DomEventTrigger extends Trigger
Fires when the host component receives a DOM event with the given name. The bound actions run inside the browser's event handler, preserving the user-gesture context (so downstream actions may invoke APIs gated on a gesture, such as clipboard or fullscreen).

Examples:


 new DomEventTrigger(button, "click").triggers(action);
 new DomEventTrigger(input, "input").triggers(action);
 new DomEventTrigger(panel, "pointerdown").triggers(action);
 

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

See Also:
  • Constructor Details

    • DomEventTrigger

      public DomEventTrigger(Component host, String eventName)
      Creates a trigger that fires when the host receives a DOM event with the given name.
      Parameters:
      host - the component whose root element listens for the event, not null
      eventName - the DOM event name (e.g. "click", "input"), not null
  • Method Details

    • preventDefault

      public DomEventTrigger preventDefault()
      Configures the trigger to call event.preventDefault() when it fires, suppressing the browser's default action (e.g. submitting a form on Enter, scrolling on Space). Affects Trigger.triggers(Action...) calls made after this method; existing wirings are not retroactively changed.
      Returns:
      this trigger, for chaining
    • stopPropagation

      public DomEventTrigger stopPropagation()
      Configures the trigger to call event.stopPropagation() when it fires, preventing the event from bubbling to ancestor listeners. Affects Trigger.triggers(Action...) calls made after this method; existing wirings are not retroactively changed.
      Returns:
      this trigger, for chaining
    • install

      protected final 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
    • appendHandlerBody

      protected void appendHandlerBody(StringBuilder body, List<Object> extraCaptures)
      Hook for subclasses to contribute statements to the wrapper handler body and extra captures referenced by those statements. The base implementation appends e.preventDefault(); and e.stopPropagation(); when the corresponding flags are set.

      Subclasses overriding this method typically prepend their own guard statements (and call super.appendHandlerBody afterwards) so the guard runs before preventDefault — preventing the default action on events the guard would have rejected would be wrong. Each entry added to extraCaptures is appended after the event name capture, so the first extra is referenced as $2 in the body, the next as $3, and so on.

      Parameters:
      body - the wrapper body so far; statements appended here run inside the const h = e => { ... $0(e); } wrapper, in order, with the event available as e
      extraCaptures - mutable list of captures appended after the event-name capture ($1); the first capture added is $2, etc.
    • appendInstallPrelude

      protected void appendInstallPrelude(StringBuilder prelude)
      Hook for subclasses to inject JavaScript that runs once per install, before the const h = e => {...} wrapper is created. Use this to declare closure variables (e.g. let i = 0; for sequence tracking) that the wrapper body then captures by lexical scope.

      The prelude has access to the same captures as the rest of the install JS — $0 is the action, $1 is the event name, and any extras added via appendHandlerBody(java.lang.StringBuilder, java.util.List<java.lang.Object>) appear at $2+. The base implementation is empty.

      Parameters:
      prelude - the prelude buffer; statements appended here run before the wrapper is created and remain in scope inside it