Expand description
Declarative vanilla JS behaviors for Autumn apps. No npm, no bundler.
Add VanillaPlugin, put vanilla_script() in the page <head>, and
write attributes with the typed builders:
use autumn_plugin_vanilla::{CopyText, Toggle, VanillaPlugin, vanilla_script};
use autumn_web::prelude::*;
#[get("/")]
async fn index() -> Markup {
html! {
html {
head { (vanilla_script()) }
body {
input id="key" value="sk-123" readonly;
(CopyText::selector("#key").button(html! { "Copy" }))
(Toggle::target("#more").button(html! { "More" }))
div id="more" hidden { "Details" }
}
}
}
}
autumn_web::app()
.plugin(VanillaPlugin::new())
.routes(routes![index])
.run()
.await;The runtime (vanilla.js) binds each data-vanilla element on page
load and after each htmx swap. It runs cleanup before htmx removes an
element. It uses no eval and no HTML strings, so it works under the
default CSP (script-src 'self').
Built-in behaviors: see Behavior. Add your own in a same-origin
script: Vanilla.register("name", (el) => { …; return cleanup; }).
§Security
The runtime binds any data-vanilla element in the page. If your app
shows user HTML, the sanitizer must remove data-vanilla* attributes,
or you must put that HTML inside [data-vanilla-ignore]. Selectors that
you give to the builders are trusted input.
Structs§
- Auto
Submit - Submits the form when a field changes.
- Confirm
- Asks the user before a form submits, a link opens, or an htmx element sends a request.
- Copy
Text - Copies text to the clipboard on click.
- Count
- Shows the character count of a field.
- Dismiss
- Removes the element when the user clicks a close control, or after a time.
- Local
Time - Shows a
<time>in the browser locale and time zone. - Output
Slot - The output element of
Count. Render it with(slot.render())or directly in Maud:(Count::output_slot()). - Toggle
- Shows or hides target elements on click.
- Vanilla
Plugin - Installs the
VANILLA_ASSETSbundle. It reads no configuration.
Enums§
- Behavior
- A built-in behavior. Use it as the value of
data-vanillain raw markup. - Count
Mode - What
Countshows. - Time
Format - How
LocalTimeformats the time.
Constants§
- ASSETS_
NAMESPACE - URL namespace of the bundle. Files are under
/static/_plugins/vanilla/. - PLUGIN_
NAME - The plugin name in Autumn diagnostics and route listings.
- VANILLA_
JS - Logical path of the runtime inside
VANILLA_ASSETS.
Statics§
- VANILLA_
ASSETS - The plugin asset bundle. It holds one file,
VANILLA_JS.
Functions§
- vanilla_
script - Renders the deferred
<script>tag for the runtime.
Type Aliases§
- Attributes
- Attribute name and value pairs, in render order.