rust_fel/app.rs
1use crate::component::Component;
2use crate::render::render;
3
4/// ```App``` has a member, ```component```, representing a [rust_fel::Element](../rust_fel/struct.Element.html).
5/// implementing [rust_fel::Component](../rust_fel/trait.Component.html)
6#[derive(Debug)]
7pub struct App<Component> {
8 component: Component,
9}
10
11impl<COMPONENT> App<COMPONENT>
12where
13 COMPONENT: Component,
14 COMPONENT::Properties: Default,
15 COMPONENT: std::fmt::Debug,
16{
17 pub fn new(component: COMPONENT) -> Self {
18 App { component }
19 }
20 /// ```App``` holds a [rust_fel::Element](../rust_fel/struct.Element.html) and ```mount``` invokes the [rust_fel::Element's](../rust_fel/struct.Element.html) [render](../rust_fel/trait.Component.html#tymethod.render) function.
21 /// After the [rust_fel::Element](../rust_fel/struct.Element.html) is created from the call to ```self.component.render();``` then ```rustfel::render::render``` is invoked.
22 /// This constructs a Virtual [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction) and then the real [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction) in the browser.
23 pub fn mount(&self, node_id: &str) {
24 let el = self.component.render();
25 let window = web_sys::window().expect("no global `window` exists");
26 let document = window.document().expect("should have a document on window");
27
28 let root_node = document
29 .get_element_by_id(node_id)
30 .expect("should have a root div");
31
32 render(el, &root_node, false);
33 }
34}