bpholt / cats-js-clipboard   0.5.0

MIT License GitHub

Scala.js utility for copying text to the clipboard in a browser using cats-effect

Scala versions: 3.x 2.13 2.12
Scala.js versions: 1.x

cats-js-clipboard

Add the following to your build.sbt to bring cats-js-clipboard into your Scala.js project:

libraryDependencies += "dev.holt" %%% "cats-js-clipboard" % "0.5.0"

To use:

val x: IO[Boolean] = Clipboard.make[IO].copy("text")

When the returned effect is executed, the passed text will be added to the DOM in a <pre> element, selected, and copied. If the copy succeeds, the effect will return true and the added elements will be removed from the DOM; if not, the elements will remain in place, scrolled into view for manual copying, and the effect will return false.

If loaded, the copy can also be called from JavaScript:

Clipboard.copy("text") // returns a JS promise with the same semantics as above

Other Libraries

Arman Bilge's fs2-dom project has clipboard support and many other features; you may find it to be a better option than this project.