on abed2fe help sign in
dodrio/src/lib.rs
135 lines 3.8 kB history raw
1//! The `dodrio` virtual DOM.
2//!
3//! ## Example
4//!
5//! ```no_run
6//! use dodrio::{bumpalo, Attribute, Node, Render, RenderContext};
7//! use wasm_bindgen::UnwrapThrowExt;
8//!
9//! /// A component that greets someone.
10//! pub struct Hello<'who> {
11//! who: &'who str,
12//! }
13//!
14//! impl<'who> Hello<'who> {
15//! /// Construct a new `Hello` component that greets the given `who`.
16//! pub fn new(who: &str) -> Hello {
17//! Hello { who }
18//! }
19//! }
20//!
21//! impl<'who> Render for Hello<'who> {
22//! fn render<'a>(&self, cx: &mut RenderContext<'a>) -> Node<'a> {
23//! use dodrio::builder::*;
24//!
25//! let id = bumpalo::format!(in cx.bump, "hello-{}", self.who);
26//! let who = bumpalo::collections::String::from_str_in(self.who, cx.bump).into_bump_str();
27//!
28//! div(&cx)
29//! .attr("id", id.into_bump_str())
30//! .on("click", |root, _vdom, _event| {
31//! let hello = root.unwrap_mut::<Hello>();
32//! web_sys::window()
33//! .expect_throw("should have a `Window` on the Web")
34//! .alert_with_message(hello.who);
35//! })
36//! .children([
37//! text("Hello, "),
38//! strong(&cx)
39//! .children([
40//! text(who),
41//! text("!"),
42//! ])
43//! .finish(),
44//! ])
45//! .finish()
46//! }
47//! }
48//! ```
49#![deny(missing_docs, missing_debug_implementations)]
51// Re-export the `bumpalo` crate.
52pub use bumpalo;
54cfg_if::cfg_if! {
55 if #[cfg(feature = "log")] {
56 #[macro_use]
57 extern crate log;
58 } else {
59 #[macro_use]
60 mod logging;
61 }
64// A macro to expose items unstably, only for internal/testing usage.
65cfg_if::cfg_if! {
66 if #[cfg(feature = "xxx-unstable-internal-use-only")] {
67 macro_rules! pub_unstable_internal {
68 ( $(#[$attr:meta])* pub(crate) $( $thing:tt )* ) => {
69 #[doc(hidden)]
70 $( #[$attr] )*
71 pub $( $thing )*
72 }
73 }
74 } else {
75 macro_rules! pub_unstable_internal {
76 ( $(#[$attr:meta])* pub(crate) $( $thing:tt )* ) => {
77 $( #[$attr] )*
78 pub(crate) $( $thing )*
79 }
80 }
81 }
84// Only `pub` so that the wasm-bindgen bindings work.
85#[doc(hidden)]
86pub mod change_list;
88mod cached;
89mod cached_set;
90mod diff;
91mod events;
92mod node;
93mod render;
94mod render_context;
95mod strace;
96mod vdom;
98pub mod builder;
100// Re-export items at the top level.
101pub use self::cached::Cached;
102pub use self::node::{Attribute, Listener, Node, NodeKey};
103pub use self::render::{Render, RootRender};
104pub use self::render_context::RenderContext;
105pub use self::vdom::{Vdom, VdomWeak};
107cfg_if::cfg_if! {
108 if #[cfg(all(target_arch = "wasm32", not(feature = "xxx-unstable-internal-use-only")))] {
109 use wasm_bindgen::__rt::WasmRefCell as RefCell;
110 } else {
111 use std::cell::RefCell;
112 }
115// Polyfill some Web stuff for benchmarking...
116cfg_if::cfg_if! {
117 if #[cfg(all(feature = "xxx-unstable-internal-use-only", not(target_arch = "wasm32")))] {
118 /// An element node in the physical DOM.
119 pub type Element = ();
121 pub(crate) type EventsTrampoline = ();
122 } else {
123 /// An element node in the physical DOM.
124 pub type Element = web_sys::Element;
126 pub(crate) type EventsTrampoline = wasm_bindgen::closure::Closure<Fn(web_sys::Event, u32, u32)>;
127 }
130cfg_if::cfg_if! {
131 if #[cfg(feature = "xxx-unstable-internal-use-only")] {
132 pub use self::cached_set::{CachedSet};
133 pub use self::node::{ElementNode, NodeKind, TextNode};
134 }