euv_core/renderer/render/
fn.rs1use crate::*;
2
3pub fn mount_body<F>(render_fn: F)
13where
14 F: FnOnce() -> VirtualNode,
15{
16 mount("body", render_fn);
17}
18
19pub fn mount<F>(selector: &str, render_fn: F)
35where
36 F: FnOnce() -> VirtualNode,
37{
38 init_event_delegation();
39 let window: Window = web_sys::window().expect("no global window exists");
40 let document: Document = window.document().expect("should have a document");
41 let target: Element = if selector == "body" {
42 document.body().expect("document should have a body").into()
43 } else if let Some(id) = selector.strip_prefix('#') {
44 document
45 .get_element_by_id(id)
46 .unwrap_or_else(|| panic!("no element found with id '{}'", id))
47 } else if let Some(class) = selector.strip_prefix('.') {
48 document
49 .get_elements_by_class_name(class)
50 .item(0)
51 .unwrap_or_else(|| panic!("no element found with class '{}'", class))
52 } else {
53 document
54 .get_elements_by_tag_name(selector)
55 .item(0)
56 .unwrap_or_else(|| panic!("no element found with tag '{}'", selector))
57 };
58 let mut renderer: Renderer = Renderer::new(target);
59 let vnode: VirtualNode = render_fn();
60 renderer.render(vnode);
61}
62
63pub fn destroy(selector: &str) {
77 let window: Window = window().expect("no global window exists");
78 let document: Document = window.document().expect("should have a document");
79 let target: Option<Element> = if selector == "body" {
80 document.body().map(|body: HtmlElement| body.into())
81 } else if let Some(id) = selector.strip_prefix('#') {
82 document.get_element_by_id(id)
83 } else if let Some(class) = selector.strip_prefix('.') {
84 document.get_elements_by_class_name(class).item(0)
85 } else {
86 document.get_elements_by_tag_name(selector).item(0)
87 };
88 if let Some(element) = target {
89 while let Some(child) = element.first_child() {
90 let _ = element.remove_child(&child);
91 }
92 }
93 cleanup_all();
94 reset_schedule_state();
95 reset_injected_classes();
96}