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(@core.TriggerType::parse("load").to_string(), content="load")
inspect(@core.TriggerType::parse("idle").to_string(), content="idle")
inspect(@core.TriggerType::parse("visible").to_string(), content="visible")
inspect(@core.TriggerType::parse("none").to_string(), content="none")
inspect(
@core.TriggerType::parse("media:(min-width: 1024px)").to_string(),
content="media:(min-width: 1024px)",
)
}test {
inspect(@core.TriggerType::Load.to_string(), content="load")
inspect(@core.TriggerType::Idle.to_string(), content="idle")
inspect(@core.TriggerType::Visible.to_string(), content="visible")
inspect(
@core.TriggerType::Media("(max-width: 768px)").to_string(),
content="media:(max-width: 768px)",
)
inspect(@core.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 : @core.Attr[Unit, String] = @core.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 : @core.Attr[Unit, String] = @core.attr_static("my-class")
guard attr is VStatic(v) else { fail("expected VStatic") }
inspect(v, content="my-class")
}test {
let node : @core.Node[Unit, String] = @core.component(fn() {
@core.h("div", [], [@core.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")
}fn component_tag_name(url : String) -> Stringerror_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 : @core.Node[Unit, String] = @core.for_each(fn() {
items.map(fn(s) { @core.text(s) })
})
guard node is For(render~) else { fail("expected For") }
inspect(render().length(), content="3")
}test {
let node : @core.Node[Unit, String] = @core.fragment([
@core.text("Hello"),
@core.text(" "),
@core.text("World"),
])
guard node is Fragment(children) else { fail("expected Fragment") }
inspect(children.length(), content="3")
}test {
let node : @core.Node[Unit, String] = @core.h(
"div",
[("class", @core.attr_static("container"))],
[@core.text("Hello")],
)
guard node is Element(el) else { fail("expected Element") }
inspect(el.tag, content="div")
}test {
let h : @core.EventHandler[Int] = @core.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 : @core.Node[Unit, String] = @core.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 : @core.Node[Unit, String] = @core.show(fn() { visible }, fn() {
@core.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 : @core.Node[Unit, String] = @core.text("Hello World")
guard node is Text(s) else { fail("expected Text") }
inspect(s, content="Hello World")
}test {
let mut count = 5
let node : @core.Node[Unit, String] = @core.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]Install
Download zipFine-grained reactive UI library for Moonbit/JS
Dependencies