Skip to content

Instantly share code, notes, and snippets.

@Koboo
Created January 12, 2022 12:59
Show Gist options
  • Save Koboo/5f1d101edff886a02cb475e4d0fc347b to your computer and use it in GitHub Desktop.
Save Koboo/5f1d101edff886a02cb475e4d0fc347b to your computer and use it in GitHub Desktop.
Viewless Clipboard utility for vaadin flow web-apps
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.NotificationVariant;
public class Clipboard {
public Clipboard() {
String javaScriptFunction = "window.copyToClipboard = (str) => {\n"
+ " const textarea = document.createElement(\"textarea\");\n"
+ " textarea.value = str;\n"
+ " textarea.style.position = \"absolute\";\n"
+ " textarea.style.opacity = \"0\";\n"
+ " document.body.appendChild(textarea);\n"
+ " textarea.select();\n"
+ " document.execCommand(\"copy\");\n"
+ " document.body.removeChild(textarea);\n"
+ "};";
UI.getCurrent().getPage().executeJs(javaScriptFunction);
}
public void copyTo(String value) {
UI.getCurrent().getPage().executeJs("window.copyToClipboard($0)", value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment