# Getting Started

Your first Refract view, from a few lines of Rust to a running application.

<a id="prerequisites"></a>

## A small starting point

Refract is a Rust UI framework with a portable reactive core, a browser DOM adapter, and a native adapter. You describe typed views in Rust; an attribute graph tracks the state and environment values they read.

Install Rust 1.93 or newer through `rustup`. The CLI handles the browser’s WebAssembly target and matching `wasm-bindgen` tool.

<a id="install"></a>

## Install the CLI

Install the CLI from crates.io. The `cli` feature of the `refract` package installs the `refract` executable. Library users can use `cargo add refract` without enabling CLI dependencies.

```bash
cargo install refract --features cli --locked
```

<a id="create-app"></a>

## Create your app

Create a new project, then enter its directory. The starter includes a counter with shared Rust views and separate browser and native entry points.

Edit `src/lib.rs` to change the shared interface. `public/index.html` loads the browser app, and `src/desktop.rs` opens the native window.

```bash
refract new my-app
cd my-app
```

> Choose a new directory; `refract new` never overwrites an existing one.

<a id="browser"></a>

## Run in your browser

From your new app’s directory, start the development server. The CLI builds the WebAssembly module, generates matching JavaScript bindings, and opens the browser.

The first run installs any missing WASM target or bindings tool. Later runs reuse them. The bindings tool is cached in the Cargo target directory without replacing your global installation.

```bash
refract dev --open
```

> The server runs at http://127.0.0.1:4173. Use `--port 4174` to choose another port, or omit `--open` to open the URL yourself.

<a id="native"></a>

## Open a native window

Run this command from the same app directory, in a separate terminal if the browser server is still running. The CLI builds and launches the native app with runtime diagnostics in your terminal.

Both platforms use the same view in `src/lib.rs`. In native apps, Winit hosts the window, Taffy handles layout, Parley shapes text, and Vello renders through wgpu.

```bash
refract dev --platform desktop
```

> The native implementation has been exercised on macOS. Other platforms are not yet validated, and native accessibility is deferred.

<a id="development-loop"></a>

## Edit, save, repeat

The CLI watches your app’s sources. A successful build refreshes the browser or restarts the native app. Failed builds keep the last successful app available; browser compilation errors appear in an overlay, and native errors appear in the terminal.

Reloads reset application state. Stop the development session with Ctrl+C. Closing a native window leaves the watcher running, and the next edit launches the app again.

> For offline development, add `--offline` after the required dependencies and tools are installed. Add `--locked` to require an unchanged Cargo.lock.

<a id="existing-examples"></a>

## Try the existing examples

To explore the counter already in the repository, run either command from the Refract checkout root. Use `-p refract-clock`, `-p refract-gallery`, or `-p refract-chat` to select another example.

```bash
refract dev -p refract-counter --open
# Or run the native app:
refract dev -p refract-counter --platform desktop
```

<a id="first-view"></a>

## Write your first view

Implement `View::body` to give each mounted counter its own state. Reading the binding with `cx.read` subscribes this view to updates; the action changes the value and the runtime schedules the affected UI work. Enable Macros to see the equivalent `#[view]` syntax.

**Without macros (View trait)**

```rust
use refract::prelude::*;

struct Counter {
    initial: u32,
}

impl View for Counter {
    fn body(&self, cx: &mut ViewContext<'_>) -> Result<impl View, ViewError> {
        let count = cx.state("count", || self.initial);
        let label = text(format!("Count: {}", cx.read(&count)));

        Ok(view()
            .child(label)
            .padding(16.0)
            .on_press(move |cx| {
                count.update(cx.runtime(), |n| *n += 1);
            }))
    }
}

fn counter() -> impl View {
    Counter { initial: 0 }
}
```

**With macros**

```rust
use refract::prelude::*;

#[view]
fn counter(cx: &mut ViewContext<'_>, initial: u32) -> impl View {
    #[state]
    let count: u32 = initial;

    #[action(count)]
    let increment = |cx| {
        count.update(cx.runtime(), |n| *n += 1);
    };

    view()
        .child(text(format!("Count: {}", cx.read(&count))))
        .padding(16.0)
        .on_press(increment)
}
```

<a id="ui-components"></a>

## Add editable UI components

Use `refract ui add` to copy components from a source collection into your app, then customize the Rust files directly. The installer copies required components and licenses, wires module declarations, merges Cargo dependencies and features, and records provenance in `refract-ui.lock`. Source normally lands in `src/ui/`; copying it does not add a runtime dependency on `refract-ui`.

A source collection provides a `refract-ui.toml` catalog alongside its component files and license. Refract 0.1.11 reserves `refract-ui` for the default collection, but it currently contains no components. Choose a local or Git collection with `--from`, or register a named source. The examples below assume your collection declares a `button` component; replace the sample paths and repository with your own.

```bash
# Inspect a local source, preview the changes, then install:
refract ui list --from ../my-components
refract ui add button --from ../my-components --dry-run
refract ui add button --from ../my-components

# Or register a Git source at a tag, branch, or commit:
refract ui source add acme github:acme/refract-ui --rev v0.2.0
refract ui list @acme
refract ui add @acme/button

# A direct GitHub source works without registration:
refract ui add github:acme/refract-ui/button --rev v0.2.0
```

> Append `-p my-app` to select a workspace package, or `--platform web` / `--platform desktop` to check platform support. `--offline` supports local collections and cached Cargo packages; Git sources require a fetch and use your existing Git authentication.

<a id="ui-source-packages"></a>

### Use and share the installed source

Use the copied module like any other application source, for example `use crate::ui::button::button;` when that is the collection's exported function. Run your usual Cargo check or build to resolve newly added dependencies. Commit the source files and `refract-ui.lock`; this provenance file records original file hashes and the resolved source revision, and does not replace `Cargo.lock` or download components during builds.

Repeating `add` preserves your edits when upstream bytes are unchanged. Changed upstream files that differ from local files report conflicts; inspect those changes before choosing `--overwrite`. The installer does not merge source or remove files. `--dry-run` prints the plan without changing your project.

For your own source collection, place `refract-ui.toml` at the root of a directory or Git repository. Declare each component's files, supported platforms, required components, and optional Cargo dependencies. Include a license and a compatible Refract version requirement. Bare component names use the published `refract-ui` crate matching the app's resolved Refract version unless a default source is configured; `--version` selects another compatible default-collection version. Arbitrary collections can be shared through local paths, `github:owner/repository`, or `git:https://host/repository.git`.

```toml
schema = 1
name = "acme"
license = "MIT"
license-file = "LICENSE"

[compatibility]
refract = ">=0.1.11, <0.2.0"

[items.button]
description = "Primary button"
platforms = ["web", "desktop"]
files = [{ source = "src/button.rs", target = "button.rs" }]
```

> The catalog maps source files to paths inside the app's UI directory. Include every referenced source and license file when distributing a collection. Use `requires = ["theme"]` to declare a dependency on another item in the same catalog.
