1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#[cfg(not(feature = "nightly"))]
use leptos_reactive::{
    MaybeProp, MaybeSignal, Memo, ReadSignal, RwSignal, Signal, SignalGet,
};
use wasm_bindgen::JsValue;
#[cfg(all(target_arch = "wasm32", feature = "web"))]
use wasm_bindgen::UnwrapThrowExt;

/// Represents the different possible values an element property could have,
/// allowing you to do fine-grained updates to single fields.
///
/// This mostly exists for the [`view`](https://docs.rs/leptos_macro/latest/leptos_macro/macro.view.html)
/// macro’s use. You usually won't need to interact with it directly, but it can be useful for defining
/// permissive APIs for certain components.
pub enum Property {
    /// A static JavaScript value.
    Value(JsValue),
    /// A (presumably reactive) function, which will be run inside an effect to update the property.
    Fn(Box<dyn Fn() -> JsValue>),
}

/// Converts some type into a [`Property`].
///
/// This is implemented by default for Rust primitive types, [`String`] and friends, and [`JsValue`].
pub trait IntoProperty {
    /// Converts the object into a [`Property`].
    fn into_property(self) -> Property;

    /// Helper function for dealing with `Box<dyn IntoProperty>`.
    fn into_property_boxed(self: Box<Self>) -> Property;
}

impl<T, U> IntoProperty for T
where
    T: Fn() -> U + 'static,
    U: Into<JsValue>,
{
    fn into_property(self) -> Property {
        let modified_fn = Box::new(move || self().into());
        Property::Fn(modified_fn)
    }

    fn into_property_boxed(self: Box<Self>) -> Property {
        (*self).into_property()
    }
}

macro_rules! prop_type {
    ($prop_type:ty) => {
        impl IntoProperty for $prop_type {
            #[inline(always)]
            fn into_property(self) -> Property {
                Property::Value(self.into())
            }

            fn into_property_boxed(self: Box<Self>) -> Property {
                (*self).into_property()
            }
        }

        impl IntoProperty for Option<$prop_type> {
            #[inline(always)]
            fn into_property(self) -> Property {
                Property::Value(self.into())
            }

            fn into_property_boxed(self: Box<Self>) -> Property {
                (*self).into_property()
            }
        }
    };
}

macro_rules! prop_signal_type {
    ($signal_type:ty) => {
        #[cfg(not(feature = "nightly"))]
        impl<T> IntoProperty for $signal_type
        where
            T: Into<JsValue> + Clone,
        {
            fn into_property(self) -> Property {
                let modified_fn = Box::new(move || self.get().into());
                Property::Fn(modified_fn)
            }

            fn into_property_boxed(self: Box<Self>) -> Property {
                (*self).into_property()
            }
        }
    };
}

macro_rules! prop_signal_type_optional {
    ($signal_type:ty) => {
        #[cfg(not(feature = "nightly"))]
        impl<T> IntoProperty for $signal_type
        where
            T: Clone,
            Option<T>: Into<JsValue>,
        {
            fn into_property(self) -> Property {
                let modified_fn = Box::new(move || self.get().into());
                Property::Fn(modified_fn)
            }

            fn into_property_boxed(self: Box<Self>) -> Property {
                (*self).into_property()
            }
        }
    };
}

prop_type!(JsValue);
prop_type!(String);
prop_type!(&String);
prop_type!(&str);
prop_type!(usize);
prop_type!(u8);
prop_type!(u16);
prop_type!(u32);
prop_type!(u64);
prop_type!(u128);
prop_type!(isize);
prop_type!(i8);
prop_type!(i16);
prop_type!(i32);
prop_type!(i64);
prop_type!(i128);
prop_type!(f32);
prop_type!(f64);
prop_type!(bool);

prop_signal_type!(ReadSignal<T>);
prop_signal_type!(RwSignal<T>);
prop_signal_type!(Memo<T>);
prop_signal_type!(Signal<T>);
prop_signal_type!(MaybeSignal<T>);
prop_signal_type_optional!(MaybeProp<T>);

#[cfg(all(target_arch = "wasm32", feature = "web"))]
use leptos_reactive::Oco;

#[cfg(all(target_arch = "wasm32", feature = "web"))]
#[inline(never)]
pub(crate) fn property_helper(
    el: &web_sys::Element,
    name: Oco<'static, str>,
    value: Property,
) {
    use leptos_reactive::create_render_effect;

    match value {
        Property::Fn(f) => {
            let el = el.clone();
            create_render_effect(move |_| {
                let new = f();
                let prop_name = wasm_bindgen::intern(&name);
                property_expression(&el, prop_name, new.clone());
                new
            });
        }
        Property::Value(value) => {
            let prop_name = wasm_bindgen::intern(&name);
            property_expression(el, prop_name, value)
        }
    };
}

#[cfg(all(target_arch = "wasm32", feature = "web"))]
#[inline(never)]
pub(crate) fn property_expression(
    el: &web_sys::Element,
    prop_name: &str,
    value: JsValue,
) {
    js_sys::Reflect::set(el, &JsValue::from_str(prop_name), &value)
        .unwrap_throw();
}