react/element/
js_bridge.rs1use wasm_bindgen::{JsCast, JsValue};
2
3#[derive(Debug, Clone)]
4pub enum JsComponentType {
5 Any(JsValue),
6 StaticIntrinsic(&'static str),
7}
8
9#[derive(Debug, Clone)]
10pub struct JsBridgeElement {
11 pub js_component_type: JsComponentType,
12 pub js_props_without_children: Option<js_sys::Object>,
13 pub children: Option<crate::Children>,
14 pub key: Option<crate::Key>,
15 }
17
18impl JsBridgeElement {
19 pub(crate) fn unsafe_create_element_js(self) -> react_sys::Element {
20 let mut props_without_children = self.js_props_without_children;
21
22 if let Some(key) = self.key {
23 let obj = props_without_children.get_or_insert_with(|| js_sys::Object::new());
25 let obj: &crate::JsProps = obj.unchecked_ref();
26 obj.set_key(Some(key.as_ref()));
27 }
28
29 const NULL: &JsValue = &JsValue::NULL;
30
31 let props_without_children = props_without_children.as_ref().map_or(NULL, AsRef::as_ref);
32
33 let el = if let Some(children) = self.children {
34 match self.js_component_type {
35 JsComponentType::Any(js_component_type) => react_sys::create_element(
36 &js_component_type,
37 props_without_children,
38 &children.unsafe_into_js_array(),
39 ),
40 JsComponentType::StaticIntrinsic(tag) => react_sys::create_element_intrinsic(
41 tag,
42 props_without_children,
43 &children.unsafe_into_js_array(),
44 ),
45 }
46 } else {
47 match self.js_component_type {
48 JsComponentType::Any(js_component_type) => react_sys::create_element_no_children(
49 &js_component_type,
50 props_without_children,
51 ),
52 JsComponentType::StaticIntrinsic(tag) => {
53 react_sys::create_element_intrinsic_no_children(tag, props_without_children)
54 }
55 }
56 };
57
58 el
59 }
60}
61
62impl Into<crate::Element> for JsBridgeElement {
63 #[inline]
64 fn into(self) -> crate::Element {
65 crate::Element::bridge_js(self)
66 }
67}
68
69impl crate::IntoElement for JsBridgeElement {
70 #[inline]
71 fn into_element(self) -> crate::Element {
72 self.into()
73 }
74}
75
76impl crate::Node for JsBridgeElement {
77 fn to_node(&self) -> crate::AnyNode {
78 self.clone().to_node()
79 }
80
81 fn to_children(&self) -> Option<crate::Children> {
82 self.clone().into_children()
83 }
84
85 #[inline]
86 fn into_node(self) -> crate::AnyNode {
87 crate::AnyNode::Element(self.into())
88 }
89
90 #[inline]
91 fn into_children(self) -> Option<crate::Children> {
92 Some(crate::Children::from_single(self.into_node()))
93 }
94}