Skip to content

Commit

Permalink
Update for scala-js-snabbdom-0.2.0-M3
Browse files Browse the repository at this point in the history
  • Loading branch information
sloshy authored and davesmith00000 committed Sep 5, 2024
1 parent dbf7ff2 commit 6842dad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion project/Dependancies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Dependencies {
val scalajsDomVersion = "2.8.0"
val munitCatsEffect3 = "2.0.0"
val disciplineMUnit = "2.0.0"
val scalajsSnabbdom = "0.1.0"
val scalajsSnabbdom = "0.2.0-M3"
val zio = "2.1.7"
val zioInteropCats = "23.1.0.3"
val scalaJavaTime = "2.6.0"
Expand Down
6 changes: 3 additions & 3 deletions tyrian/src/main/scala/tyrian/runtime/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import cats.effect.kernel.Ref
import cats.effect.std.Dispatcher
import cats.syntax.all.*
import org.scalajs.dom
import snabbdom.VNode
import snabbdom.PatchedVNode
import tyrian.Html
import tyrian.Location

final case class Renderer(vnode: VNode, state: RendererState):
final case class Renderer(vnode: PatchedVNode, state: RendererState):

def runningAt(t: Long): Renderer =
this.copy(state = RendererState.Running(t))

object Renderer:

def init[F[_]](vnode: VNode)(using F: Async[F]): F[Ref[F, Renderer]] =
def init[F[_]](vnode: PatchedVNode)(using F: Async[F]): F[Ref[F, Renderer]] =
F.ref(
Renderer(vnode, RendererState.Idle)
)
Expand Down
34 changes: 19 additions & 15 deletions tyrian/src/main/scala/tyrian/runtime/Rendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import scala.scalajs.js

object Rendering:

private def buildNodeData[Msg](attrs: List[Attr[Msg]], onMsg: Msg => Unit, key: Option[String]): VNodeData =
private def buildNodeData[Msg](
attrs: List[Attr[Msg]],
onMsg: Msg => Unit,
key: Option[String]
): VNodeData =
val as: List[(String, String)] =
attrs.collect {
case Attribute(n, v) => (n, v)
Expand Down Expand Up @@ -39,7 +43,7 @@ object Rendering:

VNodeData.empty.copy(
props = props.toMap,
attrs = as.toMap,
attrs = as.map((k, v) => k -> AttrValue(v)).toMap,
on = events.toMap,
key = key
)
Expand Down Expand Up @@ -97,17 +101,17 @@ object Rendering:
val data = buildNodeData(attrs, onMsg, key)
val elm = dom.document.createElement(name)
elm.innerHTML = html
val vNode = snabbdom.toVNode(elm)
vNode.data = data
vNode
snabbdom.toVNode(elm) match
case e: snabbdom.PatchedVNode.Element => e.copy(data = data).toVNode
case other => other.toVNode

// Intercept a tags with an href and no onClick attribute to stop the
// browser following links by default.
case Tag("a", attrs, children, key) if interceptHref(attrs) =>
val data = buildNodeData(attrs, onMsg, key)
val childrenElem: Array[VNode] =
children.toArray.map {
case _: Empty.type => VNode.empty()
val childrenElem: List[VNode] =
children.map {
case _: Empty.type => VNode.empty
case t: Text => VNode.text(t.value)
case subHtml: Html[Msg] => toVNode(subHtml, onMsg, router)
}
Expand All @@ -120,9 +124,9 @@ object Rendering:

case Tag(name, attrs, children, key) =>
val data = buildNodeData(attrs, onMsg, key)
val childrenElem: Array[VNode] =
children.toArray.map {
case _: Empty.type => VNode.empty()
val childrenElem: List[VNode] =
children.map {
case _: Empty.type => VNode.empty
case t: Text => VNode.text(t.value)
case subHtml: Html[Msg] => toVNode(subHtml, onMsg, router)
}
Expand All @@ -142,12 +146,12 @@ object Rendering:
)

def render[Model, Msg](
oldNode: Element | VNode,
oldNode: Element | PatchedVNode,
model: Model,
view: Model => Html[Msg],
onMsg: Msg => Unit,
router: Location => Msg
): VNode =
): PatchedVNode =
oldNode match
case em: Element => patch(em, Rendering.toVNode(view(model), onMsg, router))
case vn: VNode => patch(vn, Rendering.toVNode(view(model), onMsg, router))
case em: Element => patch(em, Rendering.toVNode(view(model), onMsg, router))
case vn: PatchedVNode => patch(vn, Rendering.toVNode(view(model), onMsg, router))

0 comments on commit 6842dad

Please sign in to comment.