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
use wasm_bindgen::{JsCast, JsValue};

#[derive(Debug, Clone)]
pub enum JsComponentType {
    Any(JsValue),
    StaticIntrinsic(&'static str),
}

#[derive(Debug, Clone)]
pub struct JsBridgeElement {
    pub js_component_type: JsComponentType,
    pub js_props_without_children: Option<js_sys::Object>,
    pub children: Option<crate::Children>,
    pub key: Option<crate::Key>,
    // pub to_persist: Option<Rc<dyn Any>>,
}

impl JsBridgeElement {
    pub(crate) fn unsafe_create_element_js(self) -> react_sys::Element {
        let mut props_without_children = self.js_props_without_children;

        if let Some(key) = self.key {
            // TODO: whether need to deep clone the props object?
            let obj = props_without_children.get_or_insert_with(|| js_sys::Object::new());
            let obj: &crate::JsProps = obj.unchecked_ref();
            obj.set_key(Some(key.as_ref()));
        }

        const NULL: &JsValue = &JsValue::NULL;

        let props_without_children = props_without_children.as_ref().map_or(NULL, AsRef::as_ref);

        let el = if let Some(children) = self.children {
            match self.js_component_type {
                JsComponentType::Any(js_component_type) => react_sys::create_element(
                    &js_component_type,
                    props_without_children,
                    &children.unsafe_into_js_array(),
                ),
                JsComponentType::StaticIntrinsic(tag) => react_sys::create_element_intrinsic(
                    tag,
                    props_without_children,
                    &children.unsafe_into_js_array(),
                ),
            }
        } else {
            match self.js_component_type {
                JsComponentType::Any(js_component_type) => react_sys::create_element_no_children(
                    &js_component_type,
                    props_without_children,
                ),
                JsComponentType::StaticIntrinsic(tag) => {
                    react_sys::create_element_intrinsic_no_children(tag, props_without_children)
                }
            }
        };

        el
    }
}

impl Into<crate::Element> for JsBridgeElement {
    #[inline]
    fn into(self) -> crate::Element {
        crate::Element::bridge_js(self)
    }
}

impl crate::IntoElement for JsBridgeElement {
    #[inline]
    fn into_element(self) -> crate::Element {
        self.into()
    }
}

impl crate::Node for JsBridgeElement {
    fn to_node(&self) -> crate::AnyNode {
        self.clone().to_node()
    }

    fn to_children(&self) -> Option<crate::Children> {
        self.clone().into_children()
    }

    #[inline]
    fn into_node(self) -> crate::AnyNode {
        crate::AnyNode::Element(self.into())
    }

    #[inline]
    fn into_children(self) -> Option<crate::Children> {
        Some(crate::Children::from_single(self.into_node()))
    }
}