diff --git a/modules/ui/src/main/scala/lucuma/ui/components/CopyControl.scala b/modules/ui/src/main/scala/lucuma/ui/components/CopyControl.scala index 098f1f7fe..0dae8cea1 100644 --- a/modules/ui/src/main/scala/lucuma/ui/components/CopyControl.scala +++ b/modules/ui/src/main/scala/lucuma/ui/components/CopyControl.scala @@ -6,7 +6,6 @@ package lucuma.ui.components import japgolly.scalajs.react.* import japgolly.scalajs.react.vdom.html_<^.* import lucuma.core.util.NewType -import lucuma.react.clipboard.CopyToClipboard import lucuma.react.common.ReactFnProps import lucuma.ui.syntax.all.given @@ -29,7 +28,7 @@ object CopyControl: LoginStyles.Uncopied.unless(copied.value.value) )( props.label, - CopyToClipboard( + CopyTextToClipboard( text = props.textToCopy, onCopy = (_, copiedCallback) => copied.setState(Copied(copiedCallback)) *> diff --git a/modules/ui/src/main/scala/lucuma/ui/components/CopyTextToClipboard.scala b/modules/ui/src/main/scala/lucuma/ui/components/CopyTextToClipboard.scala new file mode 100644 index 000000000..b80eeec35 --- /dev/null +++ b/modules/ui/src/main/scala/lucuma/ui/components/CopyTextToClipboard.scala @@ -0,0 +1,32 @@ +// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) +// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause + +package lucuma.ui.components + +import japgolly.scalajs.react.* +import japgolly.scalajs.react.vdom.html_<^.* +import lucuma.react.common.* +import org.scalajs.dom.window.navigator + +type OnCopy = (String, Boolean) => Callback + +/** + * Text to be copied to clipboard You can pass an optional callback, will be called when text is + * copied + */ +case class CopyTextToClipboard( + text: String, + onCopy: OnCopy = (_, _) => Callback.empty +) extends ReactFnPropsWithChildren[CopyTextToClipboard](CopyTextToClipboard.component) + +object CopyTextToClipboard: + private type Props = CopyTextToClipboard + + private def copy(p: CopyTextToClipboard): Callback = + AsyncCallback.fromJsPromise(navigator.clipboard.writeText(p.text)).toCallback.attempt.flatMap { + case Right(_) => p.onCopy(p.text, true) + case Left(_) => p.onCopy(p.text, false) + } + + private val component = ScalaFnComponent + .withChildren[Props]((p, c) => <.div(^.onClick --> copy(p), c))