Class SetPropertyTemporarilyAction<T>

java.lang.Object
com.vaadin.flow.component.trigger.internal.Action
com.vaadin.flow.component.trigger.internal.SetPropertyTemporarilyAction<T>
Type Parameters:
T - the runtime type of the value to assign
All Implemented Interfaces:
Serializable

public class SetPropertyTemporarilyAction<T> extends Action
Assigns a value to a JavaScript property on a target element when the bound trigger fires, then reverts the property to its previous value after a configurable timeout. Pure client-side — no server round-trip.

The "previous" value is read on the client at the first fire of a cycle, before the new value is assigned. Rapid re-fires within the timeout window are coalesced: the original captured on the first fire is preserved, the new value is applied again, and the revert timer is reset. This means a second click on a flashing button extends the flash by one timeout, rather than capturing the already-flashed value as "original". Coalescing is keyed per element and per property name, so unrelated temporary modifications on the same element do not interfere.

The default timeout is 1000L ms (DEFAULT_TIMEOUT). Passing Duration.ZERO is allowed and queues the reversion onto the next event-loop turn — the new value is briefly visible before reverting, which is a legitimate "flash" / "pulse" pattern.

Symmetric with SetPropertyAction, which applies the value permanently. The value to assign can be either a literal (constant, serialised at build time) or an Action.Input that produces the value on the client when the trigger fires.

Common idioms:

  • Flash a button into a "copied" state for one second: new SetPropertyTemporarilyAction(button, "innerText", "Copied!")
  • Mark an input invalid for half a second: new SetPropertyTemporarilyAction(input, "invalid", true, Duration.ofMillis(500))
Server-side state is not updated by this action; the changes (both the temporary assignment and the reversion) live in the browser until the next sync from the client (if any).

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

See Also:
  • Field Details

    • DEFAULT_TIMEOUT

      public static final Duration DEFAULT_TIMEOUT
      Default timeout used by constructors that don't take an explicit Duration: one second.
  • Constructor Details

    • SetPropertyTemporarilyAction

      public SetPropertyTemporarilyAction(Component target, String propertyName, @Nullable T value)
      Creates an action that temporarily assigns the given literal value to the given JS property on target when the trigger fires, reverting after DEFAULT_TIMEOUT.
      Parameters:
      target - the component whose root element to modify, not null
      propertyName - the JS property name, not null
      value - the value to assign temporarily — String, Boolean, Number, or any Jackson-serialisable object; may be null to emit a JS null
    • SetPropertyTemporarilyAction

      public SetPropertyTemporarilyAction(Component target, String propertyName, @Nullable T value, Duration timeout)
      Creates an action that temporarily assigns the given literal value to the given JS property on target when the trigger fires, reverting after timeout.
      Parameters:
      target - the component whose root element to modify, not null
      propertyName - the JS property name, not null
      value - the value to assign temporarily — String, Boolean, Number, or any Jackson-serialisable object; may be null to emit a JS null
      timeout - how long to keep the value before reverting, not null and not negative; Duration.ZERO is allowed
    • SetPropertyTemporarilyAction

      public SetPropertyTemporarilyAction(Component target, String propertyName, Action.Input<? extends T> source)
      Creates an action that temporarily assigns the value produced by source to the given JS property on target when the trigger fires, reverting after DEFAULT_TIMEOUT.
      Parameters:
      target - the component whose root element to modify, not null
      propertyName - the JS property name, not null
      source - input that produces the value to assign, not null
    • SetPropertyTemporarilyAction

      public SetPropertyTemporarilyAction(Component target, String propertyName, Action.Input<? extends T> source, Duration timeout)
      Creates an action that temporarily assigns the value produced by source to the given JS property on target when the trigger fires, reverting after timeout.
      Parameters:
      target - the component whose root element to modify, not null
      propertyName - the JS property name, not null
      source - input that produces the value to assign, not null
      timeout - how long to keep the value before reverting, not null and not negative; Duration.ZERO is allowed
  • Method Details

    • toJs

      protected JsFunction toJs(Trigger trigger)
      Description copied from class: Action
      Builds the JsFunction that runs this action when the surrounding trigger fires. The returned function takes one runtime argument named event (declared by the framework when it composes the trigger handler); subclasses do not declare argument names themselves.

      The body is one statement. To embed a value produced on the client, capture an Action.Input's JsFunction as a capture and invoke it inside the body as $N(event).

      Specified by:
      toJs in class Action
      Parameters:
      trigger - the surrounding trigger this render is for, not null
      Returns:
      the action's JS function, not null