Learn by making
Small examples.
Real foundations.
Follow the state. See how the pieces fit.
The same Rust views, running in two places.
Run commands from your Refract checkout. Browser examples require the WASM toolchain; desktop examples have been tested on macOS. Set up once →
Counter
A tiny application. A reactive foundation.
Declare a value, read it in a view, and update it from an action. This is the smallest useful loop in Refract.
use refract::prelude::*;
#[view]
fn counter(cx: &mut ViewContext<'_>) -> impl View {
#[state]
let count: u32 = 0;
#[action(count)]
let increment = |cx| {
count.update(cx.runtime(), |n| *n += 1);
};
button(
text(format!("Count: {}", cx.read(&count))),
increment,
)
.padding(16.0)
}React illustration of the Rust example.
Run the real thing with the quick start below.
./scripts/build-web.sh
python3 scripts/serve.py
# Open http://127.0.0.1:4173Clock
State that keeps time.
A shared clock model, a scoped timer, and two context consumers. The root owns the effect; the time and date views subscribe to the readings they need.
- ↳Environment objects
- ↳Effect cleanup
- ↳Scoped updates
Source in your checkoutexamples/clock/src/lib.rs
./scripts/build-web.sh clock
python3 scripts/serve.py --example clock
# Open http://127.0.0.1:4174Gallery
Give your content some room.
Three bundled photographs with a contain/cover toggle. Explore image sizing and shared layouts, with embedded assets that work offline in either adapter.
- ↳Embedded images
- ↳Image fitting
- ↳Shared layouts
Source in your checkoutexamples/gallery/src/lib.rs
./scripts/build-web.sh gallery
python3 scripts/serve.py --example gallery
# Open http://127.0.0.1:4175Chat
A few moving parts. One graph.
Searchable conversations, per-chat drafts, streaming local replies, and a multiline composer. A fuller application built from keyed views and scoped effects. Replies are scripted and session-only.
- ↳Keyed composition
- ↳Streaming updates
- ↳Cancellation
Source in your checkoutexamples/chat/src/lib.rs
./scripts/build-web.sh chat
python3 scripts/serve.py --example chat
# Open http://127.0.0.1:4176Make it your own
Compose a little further.
Stacks, spacing, and paint are just Rust method calls. Start from an example, change a view, and build up from there.
Explore layout & stylingv_stack()
.gap(16.0)
.child(text("Hello, Refract").font_size(28.0))
.child(text("A shared view. A different host."))
.padding(24.0)
.background(Color::rgb(15, 30, 52))
.foreground(Color::WHITE)
.radius(12.0)