Class ReplaceChildrenTemporarilyAction

java.lang.Object
com.vaadin.flow.component.trigger.internal.Action
com.vaadin.flow.component.trigger.internal.ReplaceChildrenTemporarilyAction
All Implemented Interfaces:
Serializable

public class ReplaceChildrenTemporarilyAction extends Action
Replaces a target component's children with a server-provided list of components when the bound trigger fires, then restores the original children after a configurable timeout. Pure client-side — no server round-trip after install.

The replacement components are attached as virtual children of the target at construction time, so their DOM is sent to the browser once and the swap on fire is a pure DOM operation. They live as virtual children for the lifetime of the target — fine for a handful of replacements per target, but avoid constructing many short-lived instances.

Slots are handled by the browser. Set slot="…" on the replacement component's root element if the target uses named slots; the browser's shadow-DOM slot routing distributes the children when they are appended:


 Icon check = new Icon(VaadinIcon.CHECK);
 check.getElement().setAttribute("slot", "prefix");
 new ClickTrigger(copyBtn).triggers(new ReplaceChildrenTemporarilyAction(
         copyBtn, check, new Text("Copied")));
 
The "original" children are read on the client at the first fire of a cycle, before the new children are inserted. Rapid re-fires within the timeout window are coalesced (same semantics as SetPropertyTemporarilyAction): the original captured on the first fire is preserved, the replacements are re-applied, and the revert timer is reset. All ReplaceChildrenTemporarilyActions on the same target share one stash entry (keyed on "children"), so two such actions in flight together still restore the truly-original children at the end.

The default timeout is one second (DEFAULT_TIMEOUT). Duration.ZERO is allowed — the reversion is queued onto the next event-loop turn, briefly showing the replacement before reverting.

Server-side state is not updated by this action; the changes (both the temporary swap and the reversion) live in the browser until the next sync from the client (if any). Plain-text replacements go through Text.

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

    • ReplaceChildrenTemporarilyAction

      public ReplaceChildrenTemporarilyAction(Component target, Component... replacementChildren)
      Creates an action that temporarily replaces the children of target with replacementChildren when the trigger fires, reverting after DEFAULT_TIMEOUT.
      Parameters:
      target - the component whose children to replace, not null
      replacementChildren - the replacement components; each must not be null and must not already have a parent. An empty array temporarily clears the children.
    • ReplaceChildrenTemporarilyAction

      public ReplaceChildrenTemporarilyAction(Component target, Duration timeout, Component... replacementChildren)
      Creates an action that temporarily replaces the children of target with replacementChildren when the trigger fires, reverting after timeout.
      Parameters:
      target - the component whose children to replace, not null
      timeout - how long to keep the replacement before reverting, not null and not negative; Duration.ZERO is allowed
      replacementChildren - the replacement components; each must not be null and must not already have a parent. An empty array temporarily clears the children.
  • 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