Class CopyTextToClipboardAction

All Implemented Interfaces:
Serializable

public class CopyTextToClipboardAction extends PromiseAction
Copies a value to the user's clipboard via navigator.clipboard.writeText when the bound trigger fires.

The Clipboard API requires the call to happen inside a short-lived user gesture (click, key press, …). Bind this action to a Trigger that fires during such a gesture, typically a ClickTrigger.

Outcome handling is inherited from PromiseAction: use the no-arg outcome constructor for fire-and-forget, or the overload taking onSuccess/onError consumers to react to the writeText promise on the UI thread.


 Action.Input<String> value = new PropertyInput<>(textField, "value",
         String.class);
 CopyTextToClipboardAction copy = new CopyTextToClipboardAction(value,
         () -> notification.show("Copied"),
         err -> notification.show("Copy failed: " + err));
 new ClickTrigger(button).triggers(copy);
 
For internal use only. May be renamed or removed in a future release.
See Also:
  • Constructor Details

    • CopyTextToClipboardAction

      public CopyTextToClipboardAction(Action.Input<String> textInput)
      Creates a fire-and-forget clipboard-copy action: the rendered JS just calls writeText and the server never sees the result.
      Parameters:
      textInput - input supplying the text to copy, not null
    • CopyTextToClipboardAction

      public CopyTextToClipboardAction(Action.Input<String> textInput, SerializableRunnable onSuccess, SerializableConsumer<String> onError)
      Creates a clipboard-copy action whose outcome is reported back to the server via onSuccess/onError. See PromiseAction for the contract on both consumers.
      Parameters:
      textInput - input supplying the text to copy, not null
      onSuccess - invoked on the UI thread after the client reports writeText resolved, not null
      onError - invoked on the UI thread with the browser's error message after the client reports writeText rejected, not null
  • Method Details

    • appendPromiseExpression

      protected void appendPromiseExpression(com.vaadin.flow.component.trigger.internal.JsBuilder builder, StringBuilder out)
      Description copied from class: PromiseAction
      Subclasses append a JS expression that evaluates to a Promise when the trigger fires. The result of that promise is what onSuccess/onError observe; the resolved value itself is not delivered to the server (only success vs. failure and the rejection message).
      Specified by:
      appendPromiseExpression in class PromiseAction
      Parameters:
      builder - collects element parameter references, not null
      out - buffer to append into, not null