Skip to content

Commit

Permalink
API: renderOnDomContentLoaded should check document.readyState. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
raquo committed Jan 15, 2025
1 parent 029dff7 commit 9c61be5
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/main/scala/com/raquo/laminar/api/Laminar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,24 @@ with Implicits {
}

/** Wait for `DOMContentLoaded` event to fire, then render a Laminar
* element into a container DOM node. This is probably what you want
* to initialize your Laminar application on page load.
* element into a container DOM node. Or, if this event has already
* fired, render immediately.
*
* This is probably what you want to initialize your Laminar application on page load.
*
* See https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event
*/
@inline def renderOnDomContentLoaded(
container: => dom.Element,
rootNode: => nodes.ReactiveElement.Base
): Unit = {
documentEvents(_.onDomContentLoaded).foreach { _ =>
if (dom.document.readyState == dom.DocumentReadyState.loading) {
documentEvents(_.onDomContentLoaded).foreach { _ =>
new RootNode(container, rootNode)
}(unsafeWindowOwner)
} else {
new RootNode(container, rootNode)
}(unsafeWindowOwner)
}
}

/** Wrap a Laminar element in [[DetachedRoot]], which allows you to
Expand Down

0 comments on commit 9c61be5

Please sign in to comment.