localghost/dom/
mod.rs

1//! The browser's Document Object Model.
2//!
3//! # Examples
4//!
5//! ```no_run
6//! use localghost::prelude::*;
7//! use localghost::dom::{body, Element};
8//!
9//! #[localghost::main]
10//! async fn main() {
11//!     let el = Element::with_text("p", "Hello world");
12//!     body().append(el);
13//! }
14//! ```
15
16// re-exports, temporary only
17pub use element::Element;
18pub use query_selector::query_selector;
19pub use text::Text;
20pub use window::Window;
21
22mod element;
23mod query_selector;
24mod text;
25mod window;
26
27use crate::prelude::*;
28
29/// Get the document's body.
30pub fn body() -> Element {
31    let el = crate::utils::document()
32        .query_selector("body")
33        .expect_throw("Could not find `window.body`")
34        .expect_throw("Could not find `window.body`");
35    unsafe { Element::from_raw("body", el) }
36}
37
38/// Wait for the DOM to be loaded.
39///
40/// # Examples
41///
42/// ```no_run
43/// use wasm_bindgen::prelude::*;
44/// use localghost::dom;
45///
46/// #[localghost::main]
47/// async fn main() {
48///     println!("waiting on document to load");
49///     dom::ready().await;
50///     println!("document loaded!");
51/// }
52/// ```
53pub async fn ready() {
54    let doc = crate::utils::document();
55    match doc.ready_state().as_str() {
56        "complete" | "interactive" => return,
57        _ => doc.once("DOMContentLoaded").await,
58    };
59}