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
use crate::{document, Element};
use cfg_if::cfg_if;
use leptos_reactive::Scope;
use wasm_bindgen::UnwrapThrowExt;
pub trait Mountable {
fn mount(&self, parent: &web_sys::Element);
}
impl Mountable for Element {
fn mount(&self, parent: &web_sys::Element) {
cfg_if! {
if #[cfg(any(feature = "csr", feature = "hydrate"))] {
parent.append_child(self).unwrap_throw();
} else {
let _ = parent;
}
}
}
}
impl Mountable for Vec<Element> {
fn mount(&self, parent: &web_sys::Element) {
cfg_if! {
if #[cfg(any(feature = "csr", feature = "hydrate"))] {
for element in self {
parent.append_child(element).unwrap_throw();
}
}
}
}
}
pub fn mount_to_body<T, F>(f: F)
where
F: Fn(Scope) -> T + 'static,
T: Mountable,
{
mount(document().body().unwrap_throw(), f)
}
pub fn mount<T, F>(parent: web_sys::HtmlElement, f: F)
where
F: Fn(Scope) -> T + 'static,
T: Mountable,
{
use leptos_reactive::create_scope;
let _ = create_scope(move |cx| {
(f(cx)).mount(&parent);
});
}
#[cfg(feature = "hydrate")]
pub fn hydrate<T, F>(parent: web_sys::HtmlElement, f: F)
where
F: Fn(Scope) -> T + 'static,
T: Mountable,
{
let _ = leptos_reactive::create_scope(move |cx| {
cx.start_hydration(&parent);
(f(cx));
cx.end_hydration();
});
}