Fine-grained reactive UI library for Moonbit/JS
Warning: This is a Proof of Concept (PoC)This project is experimental and under active development. APIs may change without notice. Not recommended for production use.
// moon.mod.json
{
"deps": {
"mizchi/luna": "0.1.0"
}
}npm install @luna_ui/luna| Package | js | native | wasm | wasm-gc |
|---|---|---|---|---|
| mizchi/luna (core) | ✅ | ✅ | ✅ | ✅ |
| mizchi/luna/core/render | ✅ | ✅ | ✅ | ✅ |
| mizchi/luna/luna/dom | ✅ | - | - | - |
// Create a signal
let count = @signal.signal(0)
// Create a computed value (memo)
let doubled = @signal.memo(fn() { count.get() * 2 })
// Create an effect that auto-tracks dependencies
let _ = @signal.effect(fn() {
println("Count: " + count.get().to_string())
@signal.on_cleanup(fn() { println("Cleaning up") })
})
// Update triggers the effect
count.set(1) // prints: Count: 1
count.update(fn(n) { n + 1 }) // prints: Count: 2fn counter_component() -> @luna_dom.DomNode {
let count = @signal.signal(0)
let doubled = @signal.memo(fn() { count.get() * 2 })
@luna_dom.div(class="counter", [
@luna_dom.h2([@luna_dom.text("Counter")]),
// Dynamic text updates automatically
@luna_dom.p([@luna_dom.text_dyn(fn() { "Count: " + count.get().to_string() })]),
@luna_dom.p([@luna_dom.text_dyn(fn() { "Doubled: " + doubled().to_string() })]),
@luna_dom.div(class="buttons", [
@luna_dom.button(
on=@luna_dom.events().click(fn(_) { count.update(fn(n) { n - 1 }) }),
[@luna_dom.text("-")],
),
@luna_dom.button(
on=@luna_dom.events().click(fn(_) { count.update(fn(n) { n + 1 }) }),
[@luna_dom.text("+")],
),
@luna_dom.button(
on=@luna_dom.events().click(fn(_) { count.set(0) }),
[@luna_dom.text("Reset")],
),
]),
])
}
fn main {
let doc = @dom.document()
match doc.getElementById("app") {
Some(el) => {
let app = counter_component()
@luna_dom.render(el |> @luna_dom.DomElement::from_dom, app)
}
None => ()
}
}fn conditional_example() -> @luna_dom.DomNode {
let show = @signal.signal(true)
@luna_dom.div([
@luna_dom.button(
on=@luna_dom.events().click(fn(_) { show.update(fn(b) { not(b) }) }),
[@luna_dom.text_dyn(fn() { if show.get() { "Hide" } else { "Show" } })],
),
@luna_dom.show(fn() { show.get() }, fn() {
@luna_dom.div([@luna_dom.text("Conditionally rendered content")])
}),
])
}fn list_example() -> @luna_dom.DomNode {
let items : @signal.Signal[Array[String]] = @signal.signal(["A", "B", "C"])
@luna_dom.ul([
@luna_dom.for_each(fn() { items.get() }, fn(item, index) {
@luna_dom.li([@luna_dom.text(item)])
}),
])
}# Install dependencies
pnpm install
# Run all CI checks (recommended before PR)
just ci
# Type check
just check
# Run all tests
just test
# Run MoonBit tests only
just test-moonbit
# Run browser tests
just test-browser
# Run E2E tests
just test-e2e
# Build
just build
# Format code
just fmt
# Show bundle sizes
just size
# Watch mode
just watchsrc/
├── core/ # Target-independent
│ ├── signal/ # Signal primitives
│ ├── vnode.mbt # VNode definition
│ ├── render/ # HTML string rendering
│ ├── routes/ # Route matching
│ └── serialize/ # State serialization
├── platform/ # Platform-specific
│ ├── dom/ # Browser DOM API
│ │ ├── element/ # Low-level DOM operations (render, diff, reconcile)
│ │ └── router/ # Client-side router
│ ├── js/ # JS-specific
│ │ └── api/ # Public API for JS (@luna_ui/luna)
│ └── server_dom/ # Server-side SSR helpers
├── stella/ # Shard/Island embedding
├── sol/ # SSR framework (CLI + runtime)
├── examples/ # Example applications
└── tests/ # Test fixtures
js/
├── luna/ # npm package (@luna_ui/luna)
└── loader/ # Island hydration loader (@luna_ui/luna-loader)
e2e/ # Playwright E2E tests| Trigger | Description |
|---|---|
| load | Hydrate immediately on page load |
| idle | Hydrate during browser idle time (requestIdleCallback) |
| visible | Hydrate when element becomes visible (IntersectionObserver) |
| media | Hydrate when media query matches |
| none | Manual hydration via __LN_HYDRATE__ |
just sol new myapppub enum MyAction { Increment; Decrement } derive(Show)
@luna.h("button", [("onclick", action(Increment))], [...])fn[T] computed(compute : () -> T) -> (() -> T)fn[T] create_root(f : (() -> Unit) -> T) -> Tfn[T] create_root_with_dispose(f : () -> T) -> (T, () -> Unit)fn effect(fn_ : () -> Unit) -> (() -> Unit)fn effect_once(fn_ : () -> Unit) -> Unitfn effect_when(condition : () -> Bool, fn_ : () -> Unit) -> (() -> Unit)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")])
])
}
)fn[E] internal_ref(url : String, state : String, trigger? : TriggerType, wc? : Bool, styles? : String, children? : Array[Node[E]]) -> Node[E]fn[E] island(id : String, url : String, state : String, children : Array[Node[E]], trigger? : TriggerType) -> Node[E]fn[T] memo(compute : () -> T) -> (() -> T)fn on_cleanup(cleanup : () -> Unit) -> Unitfn on_mount(fn_ : () -> Unit) -> Unitlet theme_ctx = create_context("light")
provide(theme_ctx, "dark", fn() {
// All components here will see "dark"
my_component()
})fn register_disposer(disposer : () -> Unit) -> Unitfn register_owner_cleanup(cleanup : () -> Unit) -> Unitswitch_(
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") })
)fn[T] untracked(f : () -> T) -> Tlet theme = use_context(theme_ctx) // Returns "dark" or defaultfn[E] wc_island(name : String, url : String, styles : String, state : String, children : Array[Node[E]], trigger? : TriggerType) -> Node[E]