Class Clipboard

java.lang.Object
com.vaadin.flow.component.clipboard.Clipboard
All Implemented Interfaces:
Serializable

public final class Clipboard extends Object implements Serializable
Entry point for the browser clipboard API. Bind clipboard actions to a user gesture by chaining off onClick(Component):

 Button copyButton = new Button("Copy");
 Clipboard.onClick(copyButton).writeText(textField);

 Clipboard.onClick(copyButton)
         .write(ClipboardContent.create().text("Hello").html("<b>Hello</b>"));
 
The Clipboard API requires a fresh user gesture for each write, so actions only run during the DOM event that fires the underlying trigger.

Read-side support is exposed through onPaste, which forwards the browser's native paste event to a server-side listener as a PasteEvent. Unlike the write API, onPaste does not need a click binding — it attaches a DOM listener directly to the given component and fires on every paste gesture targeting it (or any of its descendants, since paste bubbles). Pass the UI as the component for UI-wide scope; the options overload lets the application skip pastes whose target is a form field — useful for page-wide listeners that should only react to pastes intended for the page as a whole.

See Also:
  • Method Details

    • onClick

      public static <T extends Component & ClickNotifier<?>> ClipboardBinding onClick(T component)
      Registers the given component as a clickable trigger for a clipboard action — the common shape for copy-to-clipboard buttons. Equivalent to new ClickTrigger(component), without making callers reach for the trigger framework's internal types.
      Type Parameters:
      T - the component type, must implement ClickNotifier
      Parameters:
      component - the component to listen for clicks on, not null
      Returns:
      a new binding that can chain actions to this trigger
    • onPaste

      public static Registration onPaste(Component component, SerializableConsumer<PasteEvent> listener)
      Registers a listener for browser paste events on the given component. The listener is invoked on the UI thread once per paste gesture targeting component (or any descendant, since paste bubbles) with a PasteEvent carrying the text/plain and text/html representations of the pasted content.

      The browser only fires paste when the target element is focused at the moment the user invokes paste. For non-editable elements such as a Div this means the element must be made focusable, typically via tabindex="0". See PasteEvent for the rest of the browser caveats.

      Example:

      
       Div pasteTarget = new Div();
       pasteTarget.getElement().setAttribute("tabindex", "0");
       add(pasteTarget);
      
       Clipboard.onPaste(pasteTarget, event -> {
           if (event.hasHtml()) {
               renderHtml(event.getHtml());
           } else if (event.hasText()) {
               renderText(event.getText());
           }
       });
       
      Parameters:
      component - the component to listen for paste events on, not null
      listener - the listener invoked for each paste, not null
      Returns:
      a Registration whose remove detaches the paste listener
    • onPaste

      public static Registration onPaste(Component component, PasteOptions options, SerializableConsumer<PasteEvent> listener)
      Registers a listener for browser paste events on the given component with the given PasteOptions. The listener is invoked on the UI thread for each paste gesture targeting component (or any descendant, since paste bubbles) whose target matches the options. For UI-wide scope, pass the UI as the component; the UI's root element is <body> so it receives every paste event that bubbles up from anywhere on the page.

      The component does not need to be attached at registration time — the underlying DOM listener is bound to the component's element and is applied when the element is attached to a UI.

      Pass PasteOptions.defaults() to skip pastes whose target is an input, textarea, or contenteditable element (typically what a page-wide listener wants). Pass PasteOptions.includingInputFields() to observe every paste regardless of focus.

      Parameters:
      component - the component to listen for paste events on, not null
      options - paste filtering options, not null
      listener - the listener invoked for each matching paste, not null
      Returns:
      a Registration whose remove detaches the paste listener