Templates
axum-wire-macros
Proc macros for axum-wire — #[derive(Component)], #[wire_actions], html!
From crates.io
Version 0.0.6
Updated Apr 8, 2026
350 downloads
Install
Run this in your project folder. cargo add axum-wire-macros
axum-wire
Server-driven reactive UI components for Axum — like Laravel Livewire, 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 patches the DOM in-place. No full page reloads, no client-side framework, no WASM.
Quick start
[dependencies]
axum-wire = "0.0.5"
axum = "0.8"
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
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 (.livefor instant sync)wire:submit— handle forms server-sidewire:poll— auto-refresh on an intervalwire:navigate— SPA-like page transitions with prefetch on hoverwire:loading— show elements during server round-tripswire:ignore— preserve DOM subtrees across re-renderswire:init— call an action on mounthtml!— JSX-like macro with<Counter />sub-components and compile-time type checkingself.js()— dispatch JavaScript from server actions ($wireavailable in scope)self.dispatch()/self.dispatch_with()— dispatch browser events from server actions#[on(...)]— react to dispatched events with optional typed payloadsself.redirect()/self.redirect_navigate()— server-side redirectsself.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,
$wiremagic for client-side reactivity - Hot reload — subsecond integration, auto-detected under
dx serve
How it works
- Server renders the component to HTML. State is serialized as a JSON snapshot in a
data-wire-snapshotattribute. - User interacts — clicks, types, submits. A small JS runtime intercepts the event.
- AJAX POST sends the snapshot + action name to
/_wire/update. - Server deserializes the snapshot, calls the action method, re-renders, returns new HTML + snapshot.
- morphdom diffs the new HTML in-place. No flicker, no full reload.
Layouts
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
#[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 or MIT license at your option.