mizchi/luna/core does not have a README file
pub struct EventHandler[E] {
callback : (E) -> Unit
}pub(all) enum Node[E, A] {
Element(VElement[E, A])
Text(String)
DynamicText(() -> String)
Fragment(Array[Node[E, A]])
Show(condition~ : () -> Bool, child~ : () -> Node[E, A])
For(render~ : () -> Array[Node[E, A]])
Component(render~ : () -> Node[E, A])
WcIsland(VWcIsland[E, A])
Async(VAsync[E, A])
ErrorBoundary(VErrorBoundary[E, A])
Switch(VSwitch[E, A])
InternalRef(VInternalRef[E, A])
RawHtml(String)
}#alias(Trigger)
pub(all) enum TriggerType {
Load
Idle
Visible
Media(String)
None
}test {
// Round-trip test: parse then to_string
inspect(TriggerType::parse("load").to_string(), content="load")
inspect(TriggerType::parse("idle").to_string(), content="idle")
inspect(TriggerType::parse("visible").to_string(), content="visible")
inspect(TriggerType::parse("none").to_string(), content="none")
inspect(
TriggerType::parse("media:(min-width: 1024px)").to_string(),
content="media:(min-width: 1024px)",
)
}test {
inspect(TriggerType::Load.to_string(), content="load")
inspect(TriggerType::Idle.to_string(), content="idle")
inspect(TriggerType::Visible.to_string(), content="visible")
inspect(
TriggerType::Media("(max-width: 768px)").to_string(),
content="media:(max-width: 768px)",
)
inspect(TriggerType::None.to_string(), content="none")
}pub(all) struct TrustedHtml {
content : String
}pub struct VInternalRef[E, A] {
url : String
state : String
trigger : TriggerType
styles : String
children : Array[Node[E, A]]
}pub struct VWcIsland[E, A] {
name : String
url : String
styles : String
state : String
trigger : TriggerType
children : Array[Node[E, A]]
}pub enum MyAction { Increment; Decrement } derive(Show)
h("button", [("onclick", action(Increment))], [...])test {
let mut count = 0
let attr : Attr[Unit, String] = attr_dynamic(fn() {
"count-" + count.to_string()
})
guard attr is VDynamic(getter) else { fail("expected VDynamic") }
inspect(getter(), content="count-0")
count = 5
inspect(getter(), content="count-5")
}test {
let attr : Attr[Unit, String] = attr_static("my-class")
guard attr is VStatic(v) else { fail("expected VStatic") }
inspect(v, content="my-class")
}test {
let node : Node[Unit, String] = component(fn() {
h("div", [], [text("Component")])
})
guard node is Component(render~) else { fail("expected Component") }
guard render() is Element(el) else { fail("expected Element") }
inspect(el.tag, content="div")
}error_boundary(
children=fn() { risky_component() },
fallback=fn(err, reset) {
h("div", [], [
text("Error: " + err.to_string()),
h("button", [("onclick", handler(fn(_) { reset() }))], [text("Retry")])
])
}
)test {
let items = ["a", "b", "c"]
let node : Node[Unit, String] = for_each(fn() { items.map(fn(s) { text(s) }) })
guard node is For(render~) else { fail("expected For") }
inspect(render().length(), content="3")
}test {
let node : Node[Unit, String] = fragment([
text("Hello"),
text(" "),
text("World"),
])
guard node is Fragment(children) else { fail("expected Fragment") }
inspect(children.length(), content="3")
}test {
let node : Node[Unit, String] = h(
"div",
[("class", attr_static("container"))],
[text("Hello")],
)
guard node is Element(el) else { fail("expected Element") }
inspect(el.tag, content="div")
}test {
let h : EventHandler[Int] = handler(fn(x) { let _ = x * 2 })
inspect(h.get_callback()(5), content="()")
}fn[E, A] internal_ref(url : String, state : String, trigger? : TriggerType, styles? : String, children? : Array[Node[E, A]]) -> Node[E, A]test {
let node : Node[Unit, String] = raw_html("<strong>Bold</strong>")
guard node is RawHtml(html) else { fail("expected RawHtml") }
inspect(html, content="<strong>Bold</strong>")
}test {
let visible = true
let node : Node[Unit, String] = show(fn() { visible }, fn() {
text("Visible!")
})
guard node is Show(condition~, ..) else { fail("expected Show") }
inspect(condition(), content="true")
}switch_(
cases=[
match_case(when=fn() { state.get() == 1 }, render=fn() { text("One") }),
match_case(when=fn() { state.get() == 2 }, render=fn() { text("Two") }),
],
fallback=Some(fn() { text("Other") })
)test {
let node : Node[Unit, String] = text("Hello World")
guard node is Text(s) else { fail("expected Text") }
inspect(s, content="Hello World")
}test {
let mut count = 5
let node : Node[Unit, String] = text_dyn(fn() { count.to_string() })
guard node is DynamicText(getter) else { fail("expected DynamicText") }
inspect(getter(), content="5")
count = 10
inspect(getter(), content="10")
}fn[E, A] wc_island(name : String, url : String, styles : String, state : String, children : Array[Node[E, A]], trigger? : TriggerType) -> Node[E, A]Fine-grained reactive UI library for Moonbit/JS
Dependencies