sauron_core/dom/
util.rs

1//! utility functions
2//!
3use crate::dom;
4pub use wasm_bindgen_futures::spawn_local;
5use web_sys::ScrollToOptions;
6
7//TODO: feature gate this with `use-cached-windows`
8thread_local!(static WINDOW: web_sys::Window = web_sys::window().expect("no global `window` exists"));
9thread_local!(static DOCUMENT: web_sys::Document = window().document().expect("should have a document on window"));
10
11/// utility function which returns the Window element
12pub fn window() -> web_sys::Window {
13    WINDOW.with(|window| window.clone())
14}
15
16/// provides access to the document element
17pub fn document() -> web_sys::Document {
18    DOCUMENT.with(|document| document.clone())
19}
20
21/// utility function which returns the history api of the browser
22pub fn history() -> web_sys::History {
23    window().history().expect("should have a history object")
24}
25
26/// inject style to document head
27pub fn inject_style(style: &str) {
28    let head = document().head().expect("must have a head");
29    let style_node = document()
30        .create_element("style")
31        .expect("create style element");
32    let style_css = document().create_text_node(style);
33    style_node
34        .append_child(&style_css)
35        .expect("append to style");
36    head.append_child(&style_node).expect("must append to head");
37}
38
39/// provides access to the document body
40pub fn body() -> web_sys::HtmlElement {
41    document().body().expect("document should have a body")
42}
43
44/// provides access to the window Performance api
45pub fn performance() -> web_sys::Performance {
46    window()
47        .performance()
48        .expect("should have performance on window")
49}
50
51/// return the instantaneous time
52pub fn now() -> f64 {
53    performance().now()
54}
55
56/// scroll the browser to the top of the document
57pub fn scroll_window_to_top() {
58    let options = ScrollToOptions::new();
59    options.set_top(0.0);
60    options.set_left(0.0);
61    window().scroll_to_with_scroll_to_options(&options);
62}
63
64/// set the browser location hash
65pub fn set_location_hash(hash: &str) {
66    let location = window().location();
67    location.set_hash(hash).expect("must set the location hash");
68}
69
70/// return the hash part of the browser current url location
71/// The hash part are the text right after the `#` sign
72pub fn get_location_hash() -> String {
73    window().location().hash().expect("must have a hash")
74}
75
76/// return the size of the browser at this moment
77pub fn get_window_size() -> (i32, i32) {
78    let window = dom::window();
79    let window_width = window
80        .inner_width()
81        .expect("unable to get window width")
82        .as_f64()
83        .expect("cant convert to f64");
84    let window_height = window
85        .inner_height()
86        .expect("unable to get height")
87        .as_f64()
88        .expect("cant convert to f64");
89    (window_width as i32, window_height as i32)
90}
91
92/// set the title of the document
93pub fn set_window_title(title: &str) {
94    document().set_title(title);
95}