Skip to main content

Crate autumn_plugin_vanilla

Crate autumn_plugin_vanilla 

Source
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§

AutoSubmit
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.
CopyText
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.
LocalTime
Shows a <time> in the browser locale and time zone.
OutputSlot
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.
VanillaPlugin
Installs the VANILLA_ASSETS bundle. It reads no configuration.

Enums§

Behavior
A built-in behavior. Use it as the value of data-vanilla in raw markup.
CountMode
What Count shows.
TimeFormat
How LocalTime formats 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.