autumn_plugin_vanilla/lib.rs
1//! Declarative vanilla JS behaviors for Autumn apps. No npm, no bundler.
2//!
3//! Add [`VanillaPlugin`], put [`vanilla_script()`] in the page `<head>`, and
4//! write attributes with the typed builders:
5//!
6//! ```rust,no_run
7//! use autumn_plugin_vanilla::{CopyText, Toggle, VanillaPlugin, vanilla_script};
8//! use autumn_web::prelude::*;
9//!
10//! #[get("/")]
11//! async fn index() -> Markup {
12//! html! {
13//! html {
14//! head { (vanilla_script()) }
15//! body {
16//! input id="key" value="sk-123" readonly;
17//! (CopyText::selector("#key").button(html! { "Copy" }))
18//! (Toggle::target("#more").button(html! { "More" }))
19//! div id="more" hidden { "Details" }
20//! }
21//! }
22//! }
23//! }
24//!
25//! # async fn run() {
26//! autumn_web::app()
27//! .plugin(VanillaPlugin::new())
28//! .routes(routes![index])
29//! .run()
30//! .await;
31//! # }
32//! ```
33//!
34//! The runtime (`vanilla.js`) binds each `data-vanilla` element on page
35//! load and after each htmx swap. It runs cleanup before htmx removes an
36//! element. It uses no `eval` and no HTML strings, so it works under the
37//! default CSP (`script-src 'self'`).
38//!
39//! Built-in behaviors: see [`Behavior`]. Add your own in a same-origin
40//! script: `Vanilla.register("name", (el) => { …; return cleanup; })`.
41//!
42//! # Security
43//!
44//! The runtime binds any `data-vanilla` element in the page. If your app
45//! shows user HTML, the sanitizer must remove `data-vanilla*` attributes,
46//! or you must put that HTML inside `[data-vanilla-ignore]`. Selectors that
47//! you give to the builders are trusted input.
48
49mod assets;
50mod behaviors;
51mod markup;
52mod plugin;
53mod script;
54
55pub use assets::{ASSETS_NAMESPACE, VANILLA_ASSETS, VANILLA_JS};
56pub use behaviors::{
57 AutoSubmit, Behavior, Confirm, CopyText, Count, CountMode, Dismiss, LocalTime, OutputSlot,
58 TimeFormat, Toggle,
59};
60pub use markup::Attributes;
61pub use plugin::{PLUGIN_NAME, VanillaPlugin};
62pub use script::vanilla_script;