From ed3ee593a9609d873b9eda3a86da2260226900b6 Mon Sep 17 00:00:00 2001 From: Naden Date: Mon, 10 Nov 2025 20:41:50 +1100 Subject: [PATCH] Added support for DataStar --- README.md | 2 +- hypertext/Cargo.toml | 1 + hypertext/src/validation/attributes.rs | 99 ++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 062b2f9..bb3ff80 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A blazing fast type-checked HTML macro crate. ## Features -- Type checking for element names/attributes, including extensible support for custom frameworks like [htmx](https://htmx.org/) and [Alpine.js](https://alpinejs.dev/) +- Type checking for element names/attributes, including extensible support for custom frameworks like [htmx](https://htmx.org/), [Alpine.js](https://alpinejs.dev/), and [DataStar](https://data-star.dev/) - `#![no_std]` support - [Extremely fast](https://github.com/askama-rs/template-benchmark#benchmark-results), using lazy rendering to minimize allocation diff --git a/hypertext/Cargo.toml b/hypertext/Cargo.toml index c52918b..360ff62 100644 --- a/hypertext/Cargo.toml +++ b/hypertext/Cargo.toml @@ -34,6 +34,7 @@ actix-web = ["alloc", "dep:actix-web"] alloc = ["dep:html-escape", "dep:itoa", "dep:ryu"] alpine = [] axum = ["alloc", "dep:axum-core"] +datastar = [] default = ["alloc"] htmx = [] hyperscript = [] diff --git a/hypertext/src/validation/attributes.rs b/hypertext/src/validation/attributes.rs index d97621b..55b54dc 100644 --- a/hypertext/src/validation/attributes.rs +++ b/hypertext/src/validation/attributes.rs @@ -479,3 +479,102 @@ pub trait AlpineJsAttributes: GlobalAttributes { #[cfg(feature = "alpine")] impl AlpineJsAttributes for T {} + +/// Attributes for use with [DataStar](https://data-star.dev/). +#[cfg(feature = "datastar")] +pub trait DataStarAttributes: GlobalAttributes { + /// Sets the value of any HTML attribute to an expression + const data_attr: AttributeNamespace = AttributeNamespace; + + /// Creates a signal and sets up two-way data binding + const data_bind: AttributeNamespace = AttributeNamespace; + + /// Adds or removes a class based on an expression + const data_class: AttributeNamespace = AttributeNamespace; + + /// Creates a computed signal based on an expression + const data_computed: AttributeNamespace = AttributeNamespace; + + /// Executes an expression when any signals in it change + const data_effect: Attribute = Attribute; + + /// Tells Datastar to ignore an element and its descendants + const data_ignore: Attribute = Attribute; + + /// Tells PatchElements watcher to skip processing an element when morphing + const data_ignore_morph: Attribute = Attribute; + + /// Creates a signal that is `true` while a fetch request is in flight + const data_indicator: AttributeNamespace = AttributeNamespace; + + /// Runs an expression when the attribute is initialized + const data_init: Attribute = Attribute; + + /// Sets text content to a reactive JSON stringified version of signals + const data_json_signals: Attribute = Attribute; + + /// Attaches an event listener to an element + const data_on: AttributeNamespace = AttributeNamespace; + + /// Runs an expression when the element intersects with the viewport + const data_on_intersect: Attribute = Attribute; + + /// Runs an expression at a regular interval + const data_on_interval: Attribute = Attribute; + + /// Runs an expression whenever any signals are patched + const data_on_signal_patch: Attribute = Attribute; + + /// Filters which signals to watch when using data-on-signal-patch + const data_on_signal_patch_filter: Attribute = Attribute; + + /// Preserves the value of an attribute when morphing DOM elements + const data_preserve_attr: Attribute = Attribute; + + /// Creates a new signal that is a re ference to the element + const data_ref: AttributeNamespace = AttributeNamespace; + + /// Shows or hides an element based on an expression + const data_show: Attribute = Attribute; + + /// Patches one or more signals into the existing signals + const data_signals: AttributeNamespace = AttributeNamespace; + + /// Sets the value of inline CSS styles based on an expression + const data_style: AttributeNamespace = AttributeNamespace; + + /// Binds the text content of an element to an expression + const data_text: Attribute = Attribute; + + // Pro Attributes + + /// Allows you to animate element attributes over time + const data_animate: AttributeNamespace = AttributeNamespace; + + /// Adds custom validity to an element using an expression + const data_custom_validity: Attribute = Attribute; + + /// Runs an expression on every requestAnimationFrame event + const data_on_raf: Attribute = Attribute; + + /// Runs an expression whenever an element's dimensions change + const data_on_resize: Attribute = Attribute; + + /// Persists signals in local storage + const data_persist: AttributeNamespace = AttributeNamespace; + + /// Syncs query string params to signal values + const data_query_string: Attribute = Attribute; + + /// Replaces the URL in the browser without reloading the page + const data_replace_url: Attribute = Attribute; + + /// Scrolls the element into view + const data_scroll_into_view: Attribute = Attribute; + + /// Sets the view-transition-name style attribute explicitly + const data_view_transition: Attribute = Attribute; +} + +#[cfg(feature = "datastar")] +impl DataStarAttributes for T {}