Skip to main content

tachys/
dom.rs

1use wasm_bindgen::JsCast;
2use web_sys::{Document, HtmlElement, Window};
3
4thread_local! {
5    pub(crate) static WINDOW: web_sys::Window = web_sys::window().unwrap();
6
7    pub(crate) static DOCUMENT: web_sys::Document = web_sys::window().unwrap().document().unwrap();
8}
9
10/// Returns the [`Window`](https://developer.mozilla.org/en-US/docs/Web/API/Window).
11///
12/// This is cached as a thread-local variable, so calling `window()` multiple times
13/// requires only one call out to JavaScript.
14pub fn window() -> Window {
15    WINDOW.with(Clone::clone)
16}
17
18/// Returns the [`Document`](https://developer.mozilla.org/en-US/docs/Web/API/Document).
19///
20/// This is cached as a thread-local variable, so calling `document()` multiple times
21/// requires only one call out to JavaScript.
22///
23/// ## Panics
24/// Panics if called outside a browser environment.
25pub fn document() -> Document {
26    DOCUMENT.with(Clone::clone)
27}
28
29/// The `<body>` element.
30///
31/// ## Panics
32/// Panics if there is no `<body>` in the current document, or if it is called outside a browser
33/// environment.
34pub fn body() -> HtmlElement {
35    document().body().unwrap()
36}
37
38/// Helper function to extract [`Event.target`](https://developer.mozilla.org/en-US/docs/Web/API/Event/target)
39/// from any event.
40pub fn event_target<T>(event: &web_sys::Event) -> T
41where
42    T: JsCast,
43{
44    event.target().unwrap().unchecked_into::<T>()
45}
46
47/// Helper function to extract `event.target.value` from an event.
48///
49/// This is useful in the `on:input` or `on:change` listeners for an `<input>` element.
50pub fn event_target_value<T>(event: &T) -> String
51where
52    T: JsCast,
53{
54    event
55        .unchecked_ref::<web_sys::Event>()
56        .target()
57        .unwrap()
58        .unchecked_into::<web_sys::HtmlInputElement>()
59        .value()
60}
61
62/// Helper function to extract `event.target.checked` from an event.
63///
64/// This is useful in the `on:change` listeners for an `<input type="checkbox">` element.
65pub fn event_target_checked(ev: &web_sys::Event) -> bool {
66    ev.target()
67        .unwrap()
68        .unchecked_into::<web_sys::HtmlInputElement>()
69        .checked()
70}