No description
Find a file
Greg Johnston 89a4ab5fe7 cargo fmt
2023-04-07 08:16:10 -04:00
.github docs: publish book during CI (#817) 2023-04-06 14:09:54 -04:00
benchmarks feat: new reactive system implementation (#637) 2023-03-13 17:58:00 -04:00
docs docs: add sandbox links and max height (#824) 2023-04-07 07:38:12 -04:00
examples feat: add the ability for server fns to be submitted via GET requests (#789) 2023-04-05 06:47:17 -04:00
integrations feat: add the ability for server fns to be submitted via GET requests (#789) 2023-04-05 06:47:17 -04:00
leptos fix: fixes #802 as a temporary measure without resorting to #803 yet (#804) 2023-04-04 20:50:50 -04:00
leptos_config chore: remove unused fs dependency from leptos_config (#787) 2023-04-02 12:29:30 -04:00
leptos_dom fix: correctly escape HTML special characters in text nodes during SSR (#812) 2023-04-06 06:52:59 -04:00
leptos_hot_reload feat: support diffing inside component children in hot-reload (#690) 2023-03-17 13:53:53 -04:00
leptos_macro feat: add the ability for server fns to be submitted via GET requests (#789) 2023-04-05 06:47:17 -04:00
leptos_reactive perf: optimize memory usage of update methods (#809) 2023-04-05 20:16:53 -04:00
leptos_server feat: add the ability for server fns to be submitted via GET requests (#789) 2023-04-05 06:47:17 -04:00
logos Add simple icon logo (#468) 2023-02-04 10:19:33 -05:00
meta docs: warn if you are using leptos_meta without features (#797) 2023-04-03 21:07:43 -04:00
router fix: prevent router panic on root-level <Redirect/> during route list generation (#801) 2023-04-04 21:36:03 -04:00
server_fn feat: add the ability for server fns to be submitted via GET requests (#789) 2023-04-05 06:47:17 -04:00
server_fn_macro cargo fmt 2023-04-07 08:16:10 -04:00
.gitignore modify component attribute macro to allow snake_case fn names 2023-01-22 14:04:36 +00:00
Cargo.toml fix: warning in Cargo.toml (#800) 2023-04-04 19:53:05 -04:00
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md 2023-01-14 12:04:11 -05:00
LICENSE Initial commit 2022-07-31 16:40:31 -04:00
Makefile.toml examples: added example using Tailwind, CSR (only) and Trunk (#666) 2023-03-17 12:45:49 -04:00
README.md docs: remove Leptos guide link (same as book?) (#818) 2023-04-06 20:44:26 -04:00
rustfmt.toml apply new formatting everywhere (#502) 2023-02-11 14:30:06 -05:00

Leptos Logo

crates.io docs.rs Discord

Leptos

use leptos::*;

#[component]
pub fn SimpleCounter(cx: Scope, initial_value: i32) -> impl IntoView {
    // create a reactive signal with the initial value
    let (value, set_value) = create_signal(cx, initial_value);

    // create event handlers for our buttons
    // note that `value` and `set_value` are `Copy`, so it's super easy to move them into closures
    let clear = move |_| set_value(0);
    let decrement = move |_| set_value.update(|value| *value -= 1);
    let increment = move |_| set_value.update(|value| *value += 1);

    // create user interfaces with the declarative `view!` macro
    view! {
        cx,
        <div>
            <button on:click=clear>"Clear"</button>
            <button on:click=decrement>"-1"</button>
            <span>"Value: " {value} "!"</span>
            <button on:click=increment>"+1"</button>
        </div>
    }
}

// Easy to use with Trunk (trunkrs.dev) or with a simple wasm-bindgen setup
pub fn main() {
    mount_to_body(|cx| view! { cx,  <SimpleCounter initial_value=3 /> })
}

About the Framework

Leptos is a full-stack, isomorphic Rust web framework leveraging fine-grained reactivity to build declarative user interfaces.

What does that mean?

  • Full-stack: Leptos can be used to build apps that run in the browser (client-side rendering), on the server (server-side rendering), or by rendering HTML on the server and then adding interactivity in the browser (hydration). This includes support for HTTP streaming of both data (Resources) and HTML (out-of-order streaming of <Suspense/> components.)
  • Isomorphic: Leptos provides primitives to write isomorphic server functions, i.e., functions that can be called with the “same shape” on the client or server, but only run on the server. This means you can write your server-only logic (database requests, authentication etc.) alongside the client-side components that will consume it, and call server functions as if they were running in the browser.
  • Web: Leptos is built on the Web platform and Web standards. The router is designed to use Web fundamentals (like links and forms) and build on top of them rather than trying to replace them.
  • Framework: Leptos provides most of what you need to build a modern web app: a reactive system, templating library, and a router that works on both the server and client side.
  • Fine-grained reactivity: The entire framework is built from reactive primitives. This allows for extremely performant code with minimal overhead: when a reactive signals value changes, it can update a single text node, toggle a single class, or remove an element from the DOM without any other code running. (So, no virtual DOM!)
  • Declarative: Tell Leptos how you want the page to look, and let the framework tell the browser how to do it.

Learn more

Here are some resources for learning more about Leptos:

nightly Note

Most of the examples assume youre using nightly Rust.

To set up your Rust toolchain using nightly (and add the ability to compile Rust to WebAssembly, if you havent already)

rustup toolchain install nightly
rustup default nightly
rustup target add wasm32-unknown-unknown

If youre on stable, note the following:

  1. You need to enable the "stable" flag in Cargo.toml: leptos = { version = "0.2", features = ["stable"] }
  2. nightly enables the function call syntax for accessing and setting signals. If youre using stable, youll just call .get(), .set(), or .update() manually. Check out the counters_stable example for examples of the correct API.

cargo-leptos

cargo-leptos is a build tool that's designed to make it easy to build apps that run on both the client and the server, with seamless integration. The best way to get started with a real Leptos project right now is to use cargo-leptos and our starter template.

cargo install cargo-leptos
cargo leptos new --git https://github.com/leptos-rs/start
cd [your project name]
cargo leptos watch

Open browser on http://localhost:3000/

FAQs

Whats up with the name?

Leptos (λεπτός) is an ancient Greek word meaning “thin, light, refine, fine-grained.” To me, a classicist and not a dog owner, it evokes the lightweight reactive system that powers the framework. I've since learned the same word is at the root of the medical term “leptospirosis,” a blood infection that affects humans and animals... My bad. No dogs were harmed in the creation of this framework.

Is it production ready?

People usually mean one of three things by this question.

  1. Are the APIs stable? i.e., will I have to rewrite my whole app from Leptos 0.1 to 0.2 to 0.3 to 0.4, or can I write it now and benefit from new features and updates as new versions come?

With 0.1 the APIs are basically settled. Were adding new features, but were very happy with where the type system and patterns have landed. I would not expect major breaking changes to your code to adapt to, for example, a 0.2.0 release.

  1. Are there bugs?

Yes, Im sure there are. You can see from the state of our issue tracker over time that there arent that many bugs and theyre usually resolved pretty quickly. But for sure, there may be moments where you encounter something that requires a fix at the framework level, which may not be immediately resolved.

  1. Am I a consumer or a contributor?

This may be the big one: “production ready” implies a certain orientation to a library: that you can basically use it, without any special knowledge of its internals or ability to contribute. Everyone has this at some level in their stack: for example I (@gbj) dont have the capacity or knowledge to contribute to something like wasm-bindgen at this point: I simply rely on it to work.

There are several people in this community using Leptos right now for internal apps at work, who have also become significant contributors. I think this is the right level of production use for now. There may be missing features that you need, and you may end up building them! But for internal apps, if youre willing to build and contribute missing pieces along the way, the framework is definitely usable right now.

Can I use this for native GUI?

Sure! Obviously the view macro is for generating DOM nodes but you can use the reactive system to drive native any GUI toolkit that uses the same kind of object-oriented, event-callback-based framework as the DOM pretty easily. The principles are the same:

  • Use signals, derived signals, and memos to create your reactive system
  • Create GUI widgets
  • Use event listeners to update signals
  • Create effects to update the UI

I've put together a very simple GTK example so you can see what I mean.

How is this different from Yew/Dioxus?

On the surface level, these libraries may seem similar. Yew is, of course, the most mature Rust library for web UI development and has a huge ecosystem. Dioxus is similar in many ways, being heavily inspired by React. Here are some conceptual differences between Leptos and these frameworks:

  • VDOM vs. fine-grained: Yew is built on the virtual DOM (VDOM) model: state changes cause components to re-render, generating a new virtual DOM tree. Yew diffs this against the previous VDOM, and applies those patches to the actual DOM. Component functions rerun whenever state changes. Leptos takes an entirely different approach. Components run once, creating (and returning) actual DOM nodes and setting up a reactive system to update those DOM nodes.
  • Performance: This has huge performance implications: Leptos is simply much faster at both creating and updating the UI than Yew is.
  • Mental model: Adopting fine-grained reactivity also tends to simplify the mental model. There are no surprising component re-renders because there are no re-renders. Your app can be divided into components based on what makes sense for your app, because they have no performance implications.

How is this different from Sycamore?

Conceptually, these two frameworks are very similar: because both are built on fine-grained reactivity, most apps will end up looking very similar between the two, and Sycamore or Leptos apps will both look a lot like SolidJS apps, in the same way that Yew or Dioxus can look a lot like React.

There are some practical differences that make a significant difference:

  • Maturity: Sycamore is obviously a much more mature and stable library with a larger ecosystem.

  • Templating: Leptos uses a JSX-like template format (built on syn-rsx) for its view macro. Sycamore offers the choice of its own templating DSL or a builder syntax.

  • Read-write segregation: Leptos, like Solid, encourages read-write segregation between signal getters and setters, so you end up accessing signals with tuples like let (count, set_count) = create_signal(cx, 0); (If you prefer or if it's more convenient for your API, you can use create_rw_signal to give a unified read/write signal.)

  • Signals are functions: In Leptos, you can call a signal to access it rather than calling a specific method (so, count() instead of count.get()) This creates a more consistent mental model: accessing a reactive value is always a matter of calling a function. For example:

    let (count, set_count) = create_signal(cx, 0); // a signal
    let double_count = move || count() * 2; // a derived signal
    let memoized_count = create_memo(cx, move |_| count() * 3); // a memo
    // all are accessed by calling them
    assert_eq!(count(), 0);
    assert_eq!(double_count(), 0);
    assert_eq!(memoized_count(), 0);
    // this function can accept any of those signals
    fn do_work_on_signal(my_signal: impl Fn() -> i32) { ... }
    
  • Signals and scopes are 'static: Both Leptos and Sycamore ease the pain of moving signals in closures (in particular, event listeners) by making them Copy, to avoid the { let count = count.clone(); move |_| ... } that's very familiar in Rust UI code. Sycamore does this by using bump allocation to tie the lifetimes of its signals to its scopes: since references are Copy, &'a Signal<T> can be moved into a closure. Leptos does this by using arena allocation and passing around indices: types like ReadSignal<T>, WriteSignal<T>, and Memo<T> are actually wrappers for indices into an arena. This means that both scopes and signals are both Copy and 'static in Leptos, which means that they can be moved easily into closures without adding lifetime complexity.