Add home liveview

This commit is contained in:
Paul-Henri Froidmont 2025-09-13 01:36:30 +02:00
parent a036e3cbb3
commit 0b067aa7e1
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
3 changed files with 43 additions and 4 deletions

View file

@ -35,7 +35,13 @@ class HtmlElement(val tag: HtmlTag, val mods: Vector[Mod]):
diff diff
class HtmlTag(val name: String, val void: Boolean = false): class HtmlTag(val name: String, val void: Boolean = false):
def apply(mods: Mod*): HtmlElement = HtmlElement(this, mods.toVector) def apply(mods: (Mod | IterableOnce[Mod])*): HtmlElement = HtmlElement(
this,
mods.toVector.flatMap {
case m: Mod => Some(m)
case ms: IterableOnce[Mod] => ms
}
)
class HtmlAttr[V](val name: String, val codec: Codec[V, String]): class HtmlAttr[V](val name: String, val codec: Codec[V, String]):
private inline def isBooleanAsAttrPresence = codec == BooleanAsAttrPresenceCodec private inline def isBooleanAsAttrPresence = codec == BooleanAsAttrPresenceCodec
@ -83,9 +89,9 @@ object Mod:
case DynText(dyn: Dyn[String]) extends Content with DynamicMod case DynText(dyn: Dyn[String]) extends Content with DynamicMod
case DynElement(dyn: Dyn[HtmlElement]) extends Content with DynamicMod case DynElement(dyn: Dyn[HtmlElement]) extends Content with DynamicMod
// TODO support arbitrary collection // TODO support arbitrary collection
case DynOptionElement(dyn: Dyn[Option[HtmlElement]]) extends Content with DynamicMod case DynOptionElement(dyn: Dyn[Option[HtmlElement]]) extends Content with DynamicMod
case DynElementColl(dyn: Dyn[Iterable[HtmlElement]]) extends Content with DynamicMod case DynElementColl(dyn: Dyn[IterableOnce[HtmlElement]]) extends Content with DynamicMod
case DynSplit(v: SplitVar[?, HtmlElement, ?]) extends Content with DynamicMod case DynSplit(v: SplitVar[?, HtmlElement, ?]) extends Content with DynamicMod
extension (mod: Mod) extension (mod: Mod)
private[scalive] def setAllUnchanged(): Unit = private[scalive] def setAllUnchanged(): Unit =

View file

@ -25,6 +25,10 @@ object Example extends ZIOAppDefault:
LiveRouter( LiveRouter(
RootLayout(_), RootLayout(_),
List( List(
LiveRoute(
Root,
(_, _) => HomeLiveView()
),
LiveRoute( LiveRoute(
Root / "counter", Root / "counter",
(_, _) => CounterLiveView() (_, _) => CounterLiveView()

View file

@ -0,0 +1,29 @@
import scalive.*
import zio.*
import zio.stream.ZStream
class HomeLiveView() extends LiveView[String, Unit]:
val links = List(
"/counter" -> "Counter",
"/list" -> "List"
)
def init = ZIO.succeed(())
def update(model: Unit) = _ => ZIO.succeed(model)
def view(model: Dyn[Unit]) =
ul(
cls := "space-y-2",
links.map((path, name) =>
li(
a(
href := path,
cls := "block px-4 py-2 rounded-lg text-gray-700 hover:bg-gray-100 hover:text-gray-900 font-medium transition",
name
)
)
)
)
def subscriptions(model: Unit) = ZStream.empty