Skip to main content

euv_core/vdom/attribute/
fn.rs

1use crate::*;
2
3/// Creates a reactive style `AttributeValue` that updates when signals change.
4///
5/// This function replaces the inline `Signal::new(...)` + `subscribe_attr_signal(...)`
6/// boilerplate that was previously generated by the `html!` macro for every
7/// `style:` attribute containing reactive `if` conditions.
8///
9/// # Arguments
10///
11/// - `Fn() -> String + 'static` - A closure that computes the current CSS string.
12///   Called on initial render and whenever any signal changes.
13///
14/// # Returns
15///
16/// - `AttributeValue` - A `AttributeValue::Signal` backed by a `Signal<String>`
17///   that reactively re-evaluates the CSS string on signal updates.
18pub fn create_reactive_style_attribute<F>(compute: F) -> AttributeValue
19where
20    F: Fn() -> String + 'static,
21{
22    let attr_signal: Signal<String> = Signal::new(compute());
23    subscribe_attr_signal(attr_signal, compute);
24    AttributeValue::Signal(attr_signal)
25}
26
27/// Creates a reactive attribute `AttributeValue` for conditional attribute values.
28///
29/// This function replaces the inline `Signal::new(...)` + `subscribe_attr_signal(...)`
30/// boilerplate that was previously generated by the `html!` macro for every
31/// attribute value containing an `if` condition.
32///
33/// # Arguments
34///
35/// - `Fn() -> String + 'static` - A closure that computes the current attribute value.
36///   Called on initial render and whenever any signal changes.
37///
38/// # Returns
39///
40/// - `AttributeValue` - A `AttributeValue::Signal` backed by a `Signal<String>`
41///   that reactively re-evaluates the attribute value on signal updates.
42pub fn create_reactive_attr_signal<F>(compute: F) -> AttributeValue
43where
44    F: Fn() -> String + 'static,
45{
46    let attr_signal: Signal<String> =
47        Signal::new(IntoReactiveString::into_reactive_string(compute()));
48    subscribe_attr_signal(attr_signal, move || {
49        IntoReactiveString::into_reactive_string(compute())
50    });
51    AttributeValue::Signal(attr_signal)
52}