All Downloads are FREE. Search and download functionalities are using the official Maven repository.

be.doeraene.webcomponents.ui5.Toast.scala Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package be.doeraene.webcomponents.ui5

import be.doeraene.webcomponents.ui5.configkeys.ToastPlacement
import com.raquo.laminar.codecs.{BooleanAsAttrPresenceCodec, StringAsIsCodec}
import com.raquo.laminar.api.L.*
import com.raquo.laminar.tags.CustomHtmlTag
import com.raquo.laminar.keys.HtmlAttr
import com.raquo.laminar.nodes.ReactiveHtmlElement
import org.scalajs.dom

import scala.concurrent.duration.FiniteDuration
import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport
import be.doeraene.webcomponents.WebComponent

/** Simple UI button
  *
  * @see
  *   the doc for more information.
  */
object Toast extends WebComponent with HasIcon {

  @js.native
  trait RawElement extends js.Object {
    def show(): Unit = js.native
  }

  @js.native
  @JSImport("@ui5/webcomponents/dist/Toast.js", JSImport.Default)
  object RawImport extends js.Object

  // object-s are lazy so you need to actually use them in your code to prevent dead code elimination
  used(RawImport)

  type Ref = dom.html.Element with RawElement

  protected val tag: CustomHtmlTag[Ref] = CustomHtmlTag("ui5-toast")

  lazy val placement: HtmlAttr[ToastPlacement] = htmlAttr("placement", ToastPlacement.AsStringCodec)

  lazy val duration: HtmlAttr[FiniteDuration] = htmlAttr("duration", FiniteDurationCodec)

  object slots {}

  object events {}

  def getToastById(id: String): Option[dom.HTMLElement with RawElement] =
    Option(dom.document.getElementById(id)).map(_.asInstanceOf[dom.HTMLElement with RawElement])

  /** [[Observer]] you can feed a toast to show it. */
  val showObserver: Observer[Ref] = Observer(_.show())

  /** [[Mod]] for [[Toast]]s opening them each time the stream emits. */
  def showFromEvents(openerEvents: EventStream[Unit]): Mod[ReactiveHtmlElement[Ref]] =
    inContext[ReactiveHtmlElement[Ref]](el => openerEvents.mapTo(el.ref) --> showObserver)

  /** [[Mod]] for [[Toast]]s opening them each time the stream emits, putting the given text. */
  def showFromTextEvents(openerEvents: EventStream[String]): Mod[ReactiveHtmlElement[Ref]] = List(
    showFromEvents(openerEvents.mapTo(())),
    child.text <-- openerEvents
  )

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy