travisstaloch/snap-wasm-vdom
Reactive immediate DOM rendering with Wasm in Zig
Reactive immediate DOM rendering with Wasm in Zig. Build interactive web applications in Zig for the browser which compile to a small WebAssembly binary (currently around 2.7K release small build).
See the included counter example in src/main.zig
for small application.
zig build --release=small
python -m http.server
open localhost:8000 in your browser
# Build WASM library
zig build
# Run tests
zig build test
# Build optimized for production
zig build --release=small
Node
Virtual DOM node representing elements, text, or empty nodes.
Attribute
HTML attribute with name-value pairs.
// Create attribute
const attr: Attribute = .a("class", "my-class");
EventHandler
Type-safe event handling with callbacks.
// Create event handler
const handler: EventHandler = .e("click", &myCallback, &context);
h()
- Element Nodepub fn h(
tag: []const u8,
attributes: []const Attribute,
events: []const EventHandler,
children: []const Node,
) Node
t()
- Text Nodepub fn t(text: []const u8) Node
tf()
- Formatted Text Nodepub fn tf(buf: []u8, comptime fmt: []const u8, args: anytype) Node
hi()
- Immediate Element Nodepub fn hi(
tag: []const u8,
attributes: []const Attribute,
events: []const EventHandler,
children: []const Node,
) void
ti()
- Immediate Text Nodepub fn ti(text: []const u8) void
State(T, Subscriber)
Generic state container with automatic invalidation.
pub fn useState(
state: anytype,
subscriber: anytype,
) State(@TypeOf(state), @TypeOf(subscriber))
Include the generated WASM file and JavaScript bridge:
<!DOCTYPE html>
<html>
<head>
<title>My Zig App</title>
</head>
<body>
<div id="app"></div>
<script src="index.js" data-wasm-url="zig-out/bin/snap-demo.wasm" data-wasm-init-method="init"></script>
</body>
</html>
The JavaScript bridge (index.js
) provides:
The library is designed around these core concepts:
This project is in early development. Contributions welcome!
zig build test
passesMIT License - see LICENSE file for details.
Component system architecture
Server-side rendering support
Performance optimizations
More comprehensive examples
Note: This is version 0.x software. APIs may change.