tauri-plugin-htmx
tauri-plugin-htmx copied to clipboard
htmx plugin for Tauri
tauri-plugin-htmx
htmx plugin for Tauri
How it works
tauri-plugin-htmx
works by creating a fake ajax response by patching XMLHttpRequest.send()
.
How to use
-
Start a Tauri project using
create-tauri-app
and choosing "Vanilla" Javascript/Typescript as frontend.$ cargo create-tauri-app ✔ Project name · tauri-htmx-ts ✔ Choose which language to use for your frontend · TypeScript / JavaScript - (pnpm, yarn, npm, bun) ✔ Choose your package manager · npm ✔ Choose your UI template · Vanilla ✔ Choose your UI flavor · TypeScript Template created! To get started run: cd tauri-htmx-ts npm install npm run tauri dev
-
Install htmx
npm i htmx.org
-
Install
tauri-plugin-htmx
npm i tauri-plugin-htmx
-
Modify the form in
index.html
to usehtmx
. Instead of using a URL inhx-post
(orhx-get
), use the command name prefixed withcommand:
(e.g.command:greet
).- <form class="row" id="greet-form"> - <input id="greet-input" placeholder="Enter a name..." /> - <button type="submit">Greet</button> - </form> + <div class="row"> + <input id="greet-input" name="name" placeholder="Enter a name..." /> + <button hx-post="command:greet" hx-include="[name='name']" hx-trigger="click" hx-target="#greet-msg" hx-swap="innerHTML">Greet</button> + </div>
-
Modify
main.ts
ormain.js
removing everything and replacing it with justhtmx
andtauri-plugin-htmx
imports.// main.ts import "htmx.org"; import "tauri-plugin-htmx";
If you are using JavaScript as frontend, you need to copy the JS files from
node_modules
.cp node_modules/htmx.org/dist/htmx.min.js src cp node_modules/tauri-plugin-htmx/tauri-plugin-htmx.js src
Importing will be a little different.
// main.js import "./htmx.min.js"; import "./tauri-plugin-htmx.js";
-
Run your Tauri app
cargo tauri dev
Author
Ronie Martinez