Skip to content

Commit cfa19e4

Browse files
committed
feat: React.createElement
1 parent c87934d commit cfa19e4

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/element.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
crate::macro_import::wasm_bindgen_react! {
4+
pub type Element;
5+
#[wasm_bindgen(structural, method, getter = r#type)]
6+
pub fn kind(this: &Element) -> JsValue;
7+
8+
#[wasm_bindgen(js_namespace = React, js_name = createElement)]
9+
pub fn create_element_no_props(element_type: &JsValue) -> Element;
10+
11+
#[wasm_bindgen(js_namespace = React, js_name = createElement)]
12+
pub fn create_element_with_props(element_type: &JsValue, props: JsValue) -> Element;
13+
14+
#[wasm_bindgen(variadic, js_namespace = React, js_name = createElement)]
15+
pub fn create_element(
16+
element_type: &JsValue,
17+
props: JsValue,
18+
children: js_sys::Array,
19+
) -> Element;
20+
}

src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#[cfg(test)]
2-
mod tests {
3-
#[test]
4-
fn it_works() {
5-
assert_eq!(2 + 2, 4);
6-
}
7-
}
1+
mod element;
2+
mod macro_import;
3+
pub use element::*;

src/macro_import.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[cfg(feature = "import-react")]
2+
macro_rules! wasm_bindgen_react {
3+
($($b:item)+) => {
4+
#[wasm_bindgen]
5+
extern "C" {
6+
$($b)+
7+
}
8+
};
9+
}
10+
11+
#[cfg(not(feature = "import-react"))]
12+
macro_rules! wasm_bindgen_react {
13+
($($b:item)+) => {
14+
#[wasm_bindgen(inline_js = r#"export * as React from "react";"#)]
15+
extern "C" {
16+
$($b)+
17+
}
18+
};
19+
}
20+
21+
pub(crate) use wasm_bindgen_react;

0 commit comments

Comments
 (0)