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
use convert_js::ToJs;
use wasm_bindgen::JsValue;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CssProperties {
    custom: js_sys::Object,
}

impl CssProperties {
    pub fn new() -> Self {
        Self {
            custom: js_sys::Object::new(),
        }
    }

    pub fn with_key_value<V: ToJs>(self, key: &str, value: V) -> Self {
        let _ = js_sys::Reflect::set(
            self.custom.as_ref(),
            &JsValue::from_str(key),
            &value.to_js(),
        );

        self
    }
}

impl ToJs for CssProperties {
    fn to_js(&self) -> JsValue {
        JsValue::from(&self.custom)
    }
}

/// ```no_run
/// # use react_html::{ style, CssProperties };
/// let style: CssProperties = style! {
///     "margin": "10px auto",
///     "paddingTop": 8,
/// };
/// ```
///
/// Currently there is no static type checking
/// with this macro.
#[macro_export]
macro_rules! style {
    ($($key:literal : $value:expr),* $(,)? ) => {{
        $crate::CssProperties::new()
            $(.with_key_value($key, $value))*
    }};
}