Class WriteToClipboardAction
java.lang.Object
com.vaadin.flow.component.trigger.internal.Action
com.vaadin.flow.component.trigger.internal.PromiseAction<String>
com.vaadin.flow.component.trigger.internal.WriteToClipboardAction
- All Implemented Interfaces:
Serializable
Writes a
ClipboardItem to the user's clipboard via
navigator.clipboard.write. Supports two concurrent text MIME types in
one item: text/plain and text/html. Any combination is
allowed; at least one slot must be set.
The Clipboard API requires the write call to happen inside a
short-lived user gesture (click, key press, ...). Bind this action to a
trigger that fires during such a gesture.
Outcome handling extends PromiseAction: use the no-arg outcome
constructor for fire-and-forget, or the overload taking
onCopied/onError consumers. onCopied receives the
exact string that was copied — the text/plain value if present,
otherwise the text/html value — useful when the input was a
PropertyInput whose value is only known on the client.
onError receives a PromiseAction.Error record with the
browser's error name and message.
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.PromiseAction
PromiseAction.ErrorNested classes/interfaces inherited from class com.vaadin.flow.component.trigger.internal.Action
Action.Input<T> -
Constructor Summary
ConstructorsConstructorDescriptionWriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput) Creates a fire-and-forget clipboard-copy action.WriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates a clipboard-copy action whose outcome is reported back to the server. -
Method Summary
Modifier and TypeMethodDescriptionprotected JsFunctiontoPromiseJs(Trigger trigger) Subclasses return aJsFunctionthat, when invoked with the trigger's event, evaluates to aPromise.Methods inherited from class com.vaadin.flow.component.trigger.internal.PromiseAction
toJsMethods inherited from class com.vaadin.flow.component.trigger.internal.Action
warnIfNotVisible
-
Constructor Details
-
WriteToClipboardAction
public WriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput) Creates a fire-and-forget clipboard-copy action.- Parameters:
textInput- input producing thetext/plainpayload, ornullto omithtmlInput- input producing thetext/htmlpayload, ornullto omit- Throws:
IllegalArgumentException- if both inputs arenull
-
WriteToClipboardAction
public WriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates a clipboard-copy action whose outcome is reported back to the server.- Parameters:
textInput- input producing thetext/plainpayload, ornullto omithtmlInput- input producing thetext/htmlpayload, ornullto omitonCopied- invoked on the UI thread with the string that was copied after the client reports the write resolved (text/plainif present, otherwisetext/html), ornullif the JS resolved withundefined; notnullonError- invoked on the UI thread with the browser's error after the client reports the write rejected, notnull- Throws:
IllegalArgumentException- if both inputs arenull
-
-
Method Details
-
toPromiseJs
Description copied from class:PromiseActionSubclasses return aJsFunctionthat, when invoked with the trigger's event, evaluates to aPromise. The value the promise resolves with is decoded toTon the server and handed toonSuccess. To deliver a typed value, subclasses can wrap their API call in an IIFE inside the function body — for example, copying a string and resolving with it:return JsFunction.of( "return ((v) => navigator.clipboard.writeText(v).then(() => v))($0(event))", textInput.toJs(trigger)).withArguments("event");- Specified by:
toPromiseJsin classPromiseAction<String>- Parameters:
trigger- the surrounding trigger this render is for, notnull- Returns:
- the promise-yielding JS function, not
null
-