If you are not able to get all the interactivity your application needs with Htmx then we can deploy some rust in the browser.
To do this we'll follow the guidelines in Reasonable System for JavaScript Structure and apply those to Rust.
To follow the RSJS guidelines, we will use data attributes, that is, HTML attributes that begin with data-, a standard feature of HTML, to indicate that our HTML is a counter component. We will then write Rust to use an attribute selector that looks for the data-counter attribute as the root element in our counter component and wires in the appropriate event handlers and logic. Additionally, let’s rework the code to use query_selector() and add the counter functionality to all counter components found on the page. (You never know how many counters you might want!)
<1>
0 <2>
Increment
[]
= "web-csr"
= "0.1.0"
= "2021"
[]
= ["cdylib"]
[]
= ["console_error_panic_hook"]
[]
= "0.2.84"
= { = "0.3.76", = ['Document', 'Element', 'Event', 'HtmlElement', 'Node', 'Window', 'console'] }
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
= { = "0.1.7", = true }
[]
= "0.3.34"
[]
# Tell `rustc` to optimize for small code size.
= "s"
This component attaches to any element with the attribute data-alert
and shows an alert if the element is clicked.
use *;
use ;
// A simple helper function to get the document from the global window.
// Called by our JS entry point to run the example.
Add an entry wasm
wasm:
cd /workspace/crates/web-csr && wasm-pack build --target web --out-dir dist
Now run just wasm
and a wasm file will be created in the dist
folder.