# axum-wire
Server-driven reactive UI components for [Axum](https://github.com/tokio-rs/axum) — like [Laravel Livewire](https://livewire.laravel.com/), but for Rust.
Components are Rust structs. State lives on the server. User interactions (clicks, input, form submissions) trigger server actions via AJAX — the server re-renders and [morphdom](https://github.com/patrick-steele-idem/morphdom) patches the DOM in-place. No full page reloads, no client-side framework, no WASM.
## Quick start
```toml
[dependencies]
axum-wire = "0.0.2"
axum = "0.8"
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
```
```rust
use axum_wire::{html, wire_actions, Component, WireApp};
use serde::{Deserialize, Serialize};
#[derive(Default, Serialize, Deserialize, Component)]
struct Counter {
count: i64,
}
#[wire_actions]
impl Counter {
fn render(&self) -> String {
let count = self.count;
html! {
<div>
<p>{count}</p>
<button wire:click={Self::increment}>"+"</button>
</div>
}
}
fn increment(&mut self) {
self.count += 1;
}
}
#[tokio::main]
async fn main() {
let app = WireApp::new()
.wire("/", "counter")
.into_router();
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
.await.unwrap();
axum::serve(listener, app).await.unwrap();
}
```
## Features
- **`wire:click`** — call Rust methods from clicks, type-checked with `{Self::method}`
- **`wire:model`** — two-way bind inputs to struct fields (`.live` for instant sync)
- **`wire:submit`** — handle forms server-side
- **`wire:poll`** — auto-refresh on an interval
- **`wire:navigate`** — SPA-like page transitions with prefetch on hover
- **`wire:loading`** — show elements during server round-trips
- **`wire:ignore`** — preserve DOM subtrees across re-renders
- **`wire:init`** — call an action on mount
- **`html!`** — JSX-like macro with `<Counter />` sub-components and compile-time type checking
- **`self.js()`** — dispatch JavaScript from server actions (`$wire` available in scope)
- **`self.dispatch()` / `self.dispatch_with()`** — dispatch browser events from server actions
- **`#[on(...)]`** — react to dispatched events with optional typed payloads
- **`self.redirect()`** / **`self.redirect_navigate()`** — server-side redirects
- **`self.skip_render()`** / **`#[renderless]`** — skip re-rendering for lightweight actions
- **Async `render()` & `mount()`** — query databases or call APIs directly
- **Layouts** — named layouts via `.layout("name", ...)` and per-route overrides via `.wire_with()`
- **Route-scoped middleware** — Axum-style `.layer()` plus `.group(...)` for subsets of routes
- **Sub-components** — `<Counter count=42 />` with type-checked props
- **Alpine.js** — ships by default, `$wire` magic for client-side reactivity
- **Hot reload** — [subsecond](https://crates.io/crates/subsecond) integration, auto-detected under `dx serve`
## How it works
1. Server renders the component to HTML. State is serialized as a JSON snapshot in a `data-wire-snapshot` attribute.
2. User interacts — clicks, types, submits. A small JS runtime intercepts the event.
3. AJAX POST sends the snapshot + action name to `/_wire/update`.
4. Server deserializes the snapshot, calls the action method, re-renders, returns new HTML + snapshot.
5. morphdom diffs the new HTML in-place. No flicker, no full reload.
## Layouts
```rust
let app = WireApp::new()
.layout("default", my_layout) // default layout
.layout("admin", admin_layout)
.wire("/", "counter")
.wire_with("/admin", "dashboard", admin_layout) // per-route override
.group(|app| {
app.wire("/admin/posts", "admin-posts")
.wire("/admin/users", "admin-users")
.layer(auth_layer)
})
.into_router();
```
A layout is a `Fn(LayoutContext) -> String` that receives `body` (component HTML), `wire_scripts()`, `wire_styles()`, and `component` (the wire name).
## Events
```rust
#[derive(serde::Serialize)]
struct Toast {
message: String,
}
#[wire_actions]
impl Editor {
fn save(&mut self) {
self.dispatch("saved");
self.dispatch_with("toast", Toast {
message: "Saved".into(),
});
}
#[on("saved")]
fn refresh_after_save(&mut self) {}
}
```
## License
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or [MIT license](LICENSE-MIT) at your option.