leptos README

The project's README from github.com/leptos-rs/leptos. As one markdown file: README.md.

Leptos Logo

crates.io docs.rs Crates.io MSRV Discord Matrix

Website | Book | Docs.rs | Playground | Discord

You can find a list of useful libraries and example projects at awesome-leptos.

Leptos

use leptos::*;

#[component]
pub fn SimpleCounter(initial_value: i32) -> impl IntoView {
    // create a reactive signal with the initial value
    let (value, set_value) = signal(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! {
        <div>
            <button on:click=clear>Clear</button>
            <button on:click=decrement>-1</button>
            // text nodes can be quoted or unquoted
            <span>"Value: " {value} "!"</span>
            <button on:click=increment>+1</button>
        </div>
    }
}

// we also support a builder syntax rather than the JSX-like `view` macro
#[component]
pub fn SimpleCounterWithBuilder(initial_value: i32) -> impl IntoView {
    use leptos::html::*;

    let (value, set_value) = signal(initial_value);
    let clear = move |_| set_value(0);
    let decrement = move |_| set_value.update(|value| *value -= 1);
    let increment = move |_| set_value.update(|value| *value += 1);

    // the `view` macro above expands to this builder syntax
    div().child((
        button().on(ev::click, clear).child("Clear"),
        button().on(ev::click, decrement).child("-1"),
        span().child(("Value: ", value, "!")),
        button().on(ev::click, increment).child("+1")
    ))
}

// Easy to use with Trunk (trunk-rs.github.io/trunk) or with a simple wasm-bindgen setup
pub fn main() {
    mount_to_body(|| view! {
        <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?

Learn more

Here are some resources for learning more about Leptos:

Random numbers on wasm (rand / getrandom)

When you compile a Leptos app to wasm32-unknown-unknown, rand and getrandom need a JavaScript-backed source of randomness. If that backend isn’t enabled, your build can fail or randomness just won’t work in the browser.

Leptos itself takes care of this for its own code, but that does not automatically configure your app’s own rand / getrandom dependencies. If you use them directly, you need to turn on the JS backend yourself.

A simple setup in your Cargo.toml might look like this:

[dependencies]
# Make sure getrandom works on wasm by enabling its JS backend
getrandom = { version = "0.2", features = ["js"] }
rand      = { version = "0.8", features = ["small_rng"] }

Some of the examples in this repo (for example js-framework-benchmark and hackernews_js_fetch) already do this, so you can use them as a reference if you’re unsure.

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 templates for Actix or Axum.

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

Open browser to http://localhost:3000/.

FAQs

What’s up with the name?

Leptos (λεπτός) is an ancient Greek word meaning “thin, light, refined, 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?

The APIs are basically settled. We’re adding new features, but we’re very happy with where the type system and patterns have landed. I would not expect major breaking changes to your code to adapt to future releases, in terms of architecture.

  1. Are there bugs?

Yes, I’m sure there are. You can see from the state of our issue tracker over time that there aren’t that many bugs and they’re 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) don’t 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 the community using Leptos right now for many websites at work, who have also become significant contributors. There may be missing features that you need, and you may end up building them! But, if you're willing to contribute a few missing pieces along the way, the framework is most definitely usable for production applications, especially given the ecosystem of libraries that have sprung up around it.

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 any native GUI toolkit that uses the same kind of object-oriented, event-callback-based framework as the DOM pretty easily. The principles are the same:

The 0.7 update originally set out to create a "generic rendering" approach that would allow us to reuse most of the same view logic to do all of the above. Unfortunately, this has had to be shelved for now due to difficulties encountered by the Rust compiler when building larger-scale applications with the number of generics spread throughout the codebase that this required. It's an approach I'm looking forward to exploring again in the future; feel free to reach out if you're interested in this kind of work.

How is this different from Yew?

Yew is the most-used library for Rust web UI development, but there are several differences between Yew and Leptos, in philosophy, approach, and performance.

How is this different from Dioxus?

Like Leptos, Dioxus is a framework for building UIs using web technologies. However, there are significant differences in approach and features.

How is this different from Sycamore?

Sycamore and Leptos are both heavily influenced by SolidJS. At this point, Leptos has a larger community and ecosystem and is more actively developed. Other differences:

leptos (leptos-rs/leptos)

Searching Database…

21.4K ·GitHub
这是什么?Leptos 是一个全栈同构的 Rust Web 框架,采用细粒度响应式机制来构建快速、声明式的网页界面。
用它开发的应用运行在哪里——浏览器、服务器,还是两者都有?三种都支持:纯客户端渲染、纯服务端渲染,或者服务端渲染加客户端水合(hydration)——还支持以 HTTP 流式传输数据(Resources)和 HTML。
用它开始一个正式项目,推荐的做法是什么?使用 cargo-leptos,这个构建工具会统一处理客户端和服务端两部分,然后从 Actix 或 Axum 的官方入门模板创建项目。
它适合上生产环境吗?下个版本发布后我的应用会不会出问题?API 已经基本稳定,预计不会出现架构层面的大规模破坏性变更,bug 也能很快修复。但要注意:你可能会遇到缺少的功能,最后只能自己动手实现并贡献回去。
Leptos 这个名字是什么意思?它来自古希腊语,意思是“纤细、轻巧、精致、细粒度”。作者是位古典学学者,因为这套响应式系统很轻量而选了这个名字,后来才知道它也是“钩端螺旋体病(leptospirosis)”的词根。没有狗狗受到伤害。
我也能用它开发原生桌面应用吗?不能直接用——view 宏生成的是 DOM 节点——不过它的响应式系统(信号、memo、副作用)可以驱动任何基于事件回调的原生 GUI 工具包。原计划在 0.7 中加入的通用渲染方案,由于 Rust 编译器难以处理所需的泛型,已被搁置。
What are similar projects?topcoat, solid, compose-multiplatform, yew, nuxt, marko, svelte, dioxus
Creator
leptos-rs
Size
18.3 MB
Stars
21.4K
Age
4y
Commits · 90 Days