forked from fitzgen/dodrio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
42 lines (35 loc) · 1.23 KB
/
lib.rs
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
use dodrio::{builder::*, bumpalo};
use dodrio::{Node, Render, RenderContext, Vdom};
use wasm_bindgen::prelude::*;
/// A rendering component that displays a greeting.
struct Hello {
/// Who to greet.
who: String,
}
// The `Render` implementation describes how to render a `Hello` component into
// HTML.
impl<'a> Render<'a> for Hello {
fn render(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
let msg = bumpalo::format!(in cx.bump, "Hello, {}!", self.who);
let msg = msg.into_bump_str();
p(&cx).children([text(msg)]).finish()
}
}
#[wasm_bindgen(start)]
pub fn run() {
// Set up the panic hook for debugging when things go wrong.
console_error_panic_hook::set_once();
// Grab the document's `<body>`.
let window = web_sys::window().unwrap_throw();
let document = window.document().unwrap_throw();
let body = document.body().unwrap_throw();
// Create a new `Hello` render component.
let component = Hello {
who: String::from("World"),
};
// Create a virtual DOM and mount it and the `Hello` render component to the
// `<body>`.
let vdom = Vdom::new(body.as_ref(), component);
// Run the virtual DOM forever and don't unmount it.
vdom.forget();
}