Class Clipboard
- All Implemented Interfaces:
Serializable
- Click-to-copy —
copyOnClick(Component, String)and its overloads install a click handler that performs the clipboard write directly in the browser event handler. This satisfies the user-gesture requirement enforced by all major browsers and works without a server round-trip. - Paste / copy / cut listeners —
addPasteListener(com.vaadin.flow.component.Component, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.clipboard.ClipboardEvent>),addCopyListener(com.vaadin.flow.component.Component, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.clipboard.ClipboardEvent>)andaddCutListener(com.vaadin.flow.component.Component, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.clipboard.ClipboardEvent>)forward DOM clipboard events from a target component to the server. Pasted files are uploaded via the standard Flow upload path.
availabilityHintSignal() to detect whether the API is usable in
the current page context — typically to hide clipboard controls on pages
served over plain HTTP.
Click-to-copy example:
Button copy = new Button("Copy link");
Clipboard.copyOnClick(copy, "https://example.com/share/abc123");
Paste-listener example:
Clipboard.addPasteListener(textArea, event -> {
if (event.hasHtml()) {
editor.setHtmlContent(sanitize(event.getHtml()));
} else if (event.hasText()) {
editor.setTextContent(event.getText());
}
for (ClipboardFile file : event.getFiles()) {
saveAttachment(file.getName(), file.getMimeType(), file.getData());
}
});
Availability example:
Signal.effect(this, () -> {
boolean usable = Clipboard.availabilityHintSignal()
.get() == ClipboardAvailability.AVAILABLE;
copyButton.setVisible(usable);
});
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic RegistrationaddCopyListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a copy listener totarget.static RegistrationaddCutListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a cut listener totarget.static RegistrationaddPasteFailedListener(Component target, SerializableConsumer<ClipboardPasteFailedEvent> listener) Adds a listener that fires when uploading a single pasted file fails on the client.static RegistrationaddPasteListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a paste listener totarget.static RegistrationaddPasteListener(Component target, UploadHandler uploadHandler, SerializableConsumer<ClipboardEvent> listener) Adds a paste listener totargetwith a customUploadHandler.static RegistrationaddPasteStartListener(Component target, SerializableConsumer<ClipboardPasteStartEvent> listener) Adds a listener that fires the moment the browser dispatches a paste event ontarget, before any pasted file has been uploaded.static Signal<ClipboardAvailability> Returns a read-only signal hinting at whether the Clipboard API is usable in the current UI's page context.static Signal<ClipboardAvailability> Returns a read-only signal hinting at whether the Clipboard API is usable in the given UI's page context.static ClipboardCopycopyImageOnClick(Component trigger, Component imageSource) Installs a click handler ontriggerthat copies the image referenced byimageSourceto the clipboard.static ClipboardCopycopyImageOnClick(Component trigger, Component imageSource, @Nullable Command onSuccess, @Nullable Command onError) LikecopyImageOnClick(Component, Component)but with server-side callbacks invoked when the browser reports success or failure of the clipboard write.static ClipboardCopycopyOnClick(Component trigger, Component source) Installs a click handler ontriggerthat copies the current value ofsourceon each click.static ClipboardCopycopyOnClick(Component trigger, Component source, @Nullable Command onSuccess, @Nullable Command onError) LikecopyOnClick(Component, Component)but with server-side callbacks invoked when the browser reports success or failure of the clipboard write.static ClipboardCopycopyOnClick(Component trigger, String text) Installs a client-side click handler ontriggerthat copiestextto the clipboard.static ClipboardCopycopyOnClick(Component trigger, String text, @Nullable Command onSuccess, @Nullable Command onError) LikecopyOnClick(Component, String)but with server-side callbacks invoked when the browser reports success or failure of the clipboard write.
-
Method Details
-
copyOnClick
Installs a client-side click handler ontriggerthat copiestextto the clipboard. The copy executes inside the click handler so the browser's user-gesture check is satisfied in all supported browsers.The text can be updated later via
ClipboardCopy.setValue(String)without a server round-trip on click. To remove the handler, callClipboardCopy.remove().- Parameters:
trigger- the component whose clicks trigger the copy, notnulltext- the text to copy;nullis treated as an empty string- Returns:
- a handle for updating the value or removing the handler
- Throws:
NullPointerException- iftriggerisnull
-
copyOnClick
public static ClipboardCopy copyOnClick(Component trigger, String text, @Nullable Command onSuccess, @Nullable Command onError) LikecopyOnClick(Component, String)but with server-side callbacks invoked when the browser reports success or failure of the clipboard write.The success callback fires after the browser resolves the
navigator.clipboard.writeTextpromise; the error callback fires if the promise rejects (e.g. the user denied permission or the document is not focused).- Parameters:
trigger- the component whose clicks trigger the copy, notnulltext- the text to copy;nullis treated as an empty stringonSuccess- invoked on the UI thread when the write succeeds, ornullfor no-oponError- invoked on the UI thread when the write fails, ornullfor no-op- Returns:
- a handle for updating the value or removing the handler
- Throws:
NullPointerException- iftriggerisnull
-
copyOnClick
Installs a click handler ontriggerthat copies the current value ofsourceon each click. The value is read live from the source element on the client side: thevalueproperty is used if present (input / textarea / Vaadin field components), otherwisetextContent.Use this to wire a "copy" button next to a text field — clicks always copy the field's current value without a server round-trip.
- Parameters:
trigger- the component whose clicks trigger the copy, notnullsource- the component whose value to copy, notnull- Returns:
- a handle for removing the handler
- Throws:
NullPointerException- iftriggerorsourceisnull
-
copyOnClick
public static ClipboardCopy copyOnClick(Component trigger, Component source, @Nullable Command onSuccess, @Nullable Command onError) LikecopyOnClick(Component, Component)but with server-side callbacks invoked when the browser reports success or failure of the clipboard write. The source's value is still read live on each click — only the success / error notification is added.- Parameters:
trigger- the component whose clicks trigger the copy, notnullsource- the component whose value to copy, notnullonSuccess- invoked on the UI thread when the write succeeds, ornullfor no-oponError- invoked on the UI thread when the write fails, ornullfor no-op- Returns:
- a handle for removing the handler
- Throws:
NullPointerException- iftriggerorsourceisnull
-
copyImageOnClick
Installs a click handler ontriggerthat copies the image referenced byimageSourceto the clipboard. The image'ssrcattribute is fetched on the client side and written to the clipboard as the original blob's MIME type.If the source's blob type is not one the browser accepts on the clipboard (currently only
image/pngon most browsers), the write may fail; subscribe to availability if you need to gate the control. The image must be reachable from the browser's same-origin policy or served with a CORS header that permits the page's origin — otherwise thefetchwill fail.- Parameters:
trigger- the component whose clicks trigger the copy, notnullimageSource- the component carrying the imagesrc(typically an<img>), notnull- Returns:
- a handle for removing the handler
- Throws:
NullPointerException- iftriggerorimageSourceisnull
-
copyImageOnClick
public static ClipboardCopy copyImageOnClick(Component trigger, Component imageSource, @Nullable Command onSuccess, @Nullable Command onError) LikecopyImageOnClick(Component, Component)but with server-side callbacks invoked when the browser reports success or failure of the clipboard write. The error callback fires for any step in the pipeline (image fetch, blob conversion, clipboard write) — the failure is not distinguished by stage.- Parameters:
trigger- the component whose clicks trigger the copy, notnullimageSource- the component carrying the imagesrc, notnullonSuccess- invoked on the UI thread when the write succeeds, ornullfor no-oponError- invoked on the UI thread when the write fails, ornullfor no-op- Returns:
- a handle for removing the handler
- Throws:
NullPointerException- iftriggerorimageSourceisnull
-
addPasteListener
public static Registration addPasteListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a paste listener totarget. Each pasted file is buffered in memory and exposed as aClipboardFileon the resultingClipboardEvent. Suitable for small pastes; for larger ones useaddPasteListener(Component, UploadHandler, SerializableConsumer)with a streaming handler.The listener fires once per paste, after every pasted file has been uploaded (or has failed). Failures are reported via
addPasteFailedListener(com.vaadin.flow.component.Component, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.clipboard.ClipboardPasteFailedEvent>); failed files do not appear inClipboardEvent.getFiles().- Parameters:
target- the component to attach the paste handler to, notnulllistener- invoked on the UI thread when the user pastes into the target, notnull- Returns:
- a registration for removing the listener
- Throws:
NullPointerException- iftargetorlistenerisnull
-
addPasteListener
public static Registration addPasteListener(Component target, UploadHandler uploadHandler, SerializableConsumer<ClipboardEvent> listener) Adds a paste listener totargetwith a customUploadHandler. The handler receives every pasted file's bytes directly — use this to stream large pastes to disk, enforce size limits, or wireTransferProgressListenerfor fine-grained progress reporting.Because the handler owns the file content, the
ClipboardEventdelivered tolistenercarries text and HTML only —ClipboardEvent.getFiles()is always empty. The listener still fires once per paste, after the handler has finished processing every file (successful or failed).- Parameters:
target- the component to attach the paste handler to, notnulluploadHandler- the upload handler that consumes pasted file bytes, notnulllistener- invoked on the UI thread when the user pastes into the target, notnull- Returns:
- a registration for removing the listener
- Throws:
NullPointerException- if any argument isnull
-
addPasteStartListener
public static Registration addPasteStartListener(Component target, SerializableConsumer<ClipboardPasteStartEvent> listener) Adds a listener that fires the moment the browser dispatches a paste event ontarget, before any pasted file has been uploaded. The event carries text, HTML, and metadata for each pasted file (name, MIME type, size) but no bytes.Use this hook to show a progress indicator at the start of a paste — pair it with
addPasteListener(com.vaadin.flow.component.Component, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.clipboard.ClipboardEvent>)(fires after uploads complete) andaddPasteFailedListener(com.vaadin.flow.component.Component, com.vaadin.flow.function.SerializableConsumer<com.vaadin.flow.component.clipboard.ClipboardPasteFailedEvent>)(fires per failed file) to dismiss the indicator on success or failure.- Parameters:
target- the component to attach the listener to, notnulllistener- invoked on the UI thread when a paste begins, notnull- Returns:
- a registration for removing the listener
- Throws:
NullPointerException- iftargetorlistenerisnull
-
addPasteFailedListener
public static Registration addPasteFailedListener(Component target, SerializableConsumer<ClipboardPasteFailedEvent> listener) Adds a listener that fires when uploading a single pasted file fails on the client. The remaining files in the same paste continue uploading; one event is fired per failed file.- Parameters:
target- the component to attach the listener to, notnulllistener- invoked on the UI thread for each failed file, notnull- Returns:
- a registration for removing the listener
- Throws:
NullPointerException- iftargetorlistenerisnull
-
addCopyListener
public static Registration addCopyListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a copy listener totarget. The browser's native copy event on the target is forwarded to the server with the text and HTML being copied. Useful for analytics or for triggering side effects (e.g. incrementing a "copies" counter).- Parameters:
target- the component to attach the copy handler to, notnulllistener- invoked on the UI thread when the user copies from the target, notnull- Returns:
- a registration for removing the listener
- Throws:
NullPointerException- iftargetorlistenerisnull
-
addCutListener
public static Registration addCutListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a cut listener totarget. The browser's native cut event on the target is forwarded to the server with the text and HTML being cut.- Parameters:
target- the component to attach the cut handler to, notnulllistener- invoked on the UI thread when the user cuts from the target, notnull- Returns:
- a registration for removing the listener
- Throws:
NullPointerException- iftargetorlistenerisnull
-
availabilityHintSignal
Returns a read-only signal hinting at whether the Clipboard API is usable in the current UI's page context.Subscribe with
Signal.effect(owner, ...)to react to the availability (e.g. to hide a copy button on insecure-context pages). For a snapshot read, callavailabilityHintSignal().peek().The signal starts as
UNKNOWNand transitions to the value reported during the initial client bootstrap. The value is best-effort and intended for pre-rendering decisions; for authoritative results, attempt the operation and inspect the success or error callback.- Returns:
- the availability hint signal
- Throws:
IllegalStateException- if there is no current UI
-
availabilityHintSignal
Returns a read-only signal hinting at whether the Clipboard API is usable in the given UI's page context. Same semantics asavailabilityHintSignal(); use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
ui- the UI to read the hint from, nevernull- Returns:
- the availability hint signal
- Throws:
NullPointerException- ifuiisnull
-