Class CopyTextToClipboardAction
java.lang.Object
com.vaadin.flow.component.trigger.internal.Action
com.vaadin.flow.component.trigger.internal.PromiseAction
com.vaadin.flow.component.trigger.internal.CopyTextToClipboardAction
- All Implemented Interfaces:
Serializable
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.component.trigger.internal.Action
Action.Input<T> -
Constructor Summary
ConstructorsConstructorDescriptionCopyTextToClipboardAction(Action.Input<String> textInput) Creates a fire-and-forget clipboard-copy action: the rendered JS just callswriteTextand the server never sees the result.CopyTextToClipboardAction(Action.Input<String> textInput, SerializableRunnable onSuccess, SerializableConsumer<String> onError) Creates a clipboard-copy action whose outcome is reported back to the server viaonSuccess/onError. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidappendPromiseExpression(com.vaadin.flow.component.trigger.internal.JsBuilder builder, StringBuilder out) Subclasses append a JS expression that evaluates to aPromisewhen the trigger fires.Methods inherited from class com.vaadin.flow.component.trigger.internal.PromiseAction
appendStatement
-
Constructor Details
-
CopyTextToClipboardAction
Creates a fire-and-forget clipboard-copy action: the rendered JS just callswriteTextand the server never sees the result.- Parameters:
textInput- input supplying the text to copy, notnull
-
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 viaonSuccess/onError. SeePromiseActionfor the contract on both consumers.- Parameters:
textInput- input supplying the text to copy, notnullonSuccess- invoked on the UI thread after the client reportswriteTextresolved, notnullonError- invoked on the UI thread with the browser's error message after the client reportswriteTextrejected, notnull
-
-
Method Details
-
appendPromiseExpression
protected void appendPromiseExpression(com.vaadin.flow.component.trigger.internal.JsBuilder builder, StringBuilder out) Description copied from class:PromiseActionSubclasses append a JS expression that evaluates to aPromisewhen the trigger fires. The result of that promise is whatonSuccess/onErrorobserve; the resolved value itself is not delivered to the server (only success vs. failure and the rejection message).- Specified by:
appendPromiseExpressionin classPromiseAction- Parameters:
builder- collects element parameter references, notnullout- buffer to append into, notnull
-