Giter Club home page Giter Club logo

tauri-htmx-extension's Introduction

Tauri + htmx

WIP

This is a work in progress htmx extension for tauri. Will later distribute properly, but feel free to look around.

Development

To start development run the tauri dev command I reccomend you install the cli via cargo.

Note this repo is using tauri 2.0 beta

cargo install tauri-cli
cargo tauri dev

Goals

The goal is to make it easy to template and write views on the rust side of the tauri app with no need for wasm and using htmx for the dynamic bits.

Usage

Setup

To get started load htmx and the extension (main.js for now) then register the extension using hx-ext

<head>
    ...
    <script type="module" src="/main.js" defer></script>
    <script type="module" src="/htmx.js" defer></script>
</head>
<body hx-ext="tauri"></body>

Invoke

To invoke a tauri::command use the tauri-invoke attribute

<button tauri-invoke="hello" hx-target="#my-div">Click me</button>
<div id="my-div"></div>
#[tauri::command]
fn hello() -> String {
    "Hello, world!".to_string()
}

This is the same as calling invoke('hello') then it will insert "Hello, world!" into #my-div.

It also works with forms.

<form tauri-invoke="save" hx-swap="outerHTML">
    <input type="text" name="name" />
    <button type="submit">Save</button>
</form>
#[tauri::command]
async fn save(name: &str) -> Result<String, String> {
    do_save(name).await;

    format!("Saved {name} to contacts")
}

This is the same as calling invoke('save', {name}), hx-swap will swap the form for our success message.

Listen

You can also listen to events. For example if you have a background task working and wish to update the frontend with a progress.

<div tauri-listen="progress"></div>
fn do_stuff(app: AppHandle) {
    thread::spawn(move || {
        for i in 0..=100 {
            app.emit("progress", format!("progress: {i}%")).unwrap();

            thread::sleep(Duration::from_millis(100));
        }
    });
}

This uses the event.listen() tauri function and will use htmx to swap the innerHTML of the div. You can still use hx-target.

Todos

  • emit events from htmx
  • emit to specific windows
  • finish todo list

Notes

When it comes to rust templating shtml is pretty nice, but there are other options.

Also checkout the SHAT STACK a full stack rust/htmx app template using shtml, htmx, axum, and tailwind. (also a wip)

tauri-htmx-extension's People

Contributors

christianpavilonis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.