wvlet / airframe

Essential Building Blocks for Scala
https://wvlet.org/airframe
Apache License 2.0
631 stars 65 forks source link

airframe-rx-html: onMount may not be able to find the rendered DOM element #3453

Open xerial opened 5 months ago

xerial commented 5 months ago

If an RxElement is rendered in a nested sequence, onMount can be called a bit earlier than rendered element actually appears in the browser.

An example:

class HoverableTextLabel(txt: RxElement, hoverMessage: String) extends RxElement:

  private val elementId = ULID.newULIDString

  override def onMount: Unit =
    RxDOM.getHTMLElementById(elementId).foreach { el =>
      try Dynamic.newInstance(Dynamic.global.bootstrap.Tooltip)(el)
      catch case e: Throwable => warn(e)
    }

  override def render: RxElement = span(
    id                   -> elementId,
    data("bs-toggle")    -> "tooltip",
    data("bs-placement") -> "top",
    data("bs-title")     -> hoverMessage,
    txt
  )

div(
  Seq[RxElement](
    HoverableTextLabel("hello", "mouseover message")
  )
)