-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xilem_web: Add event_target_value helper function (#506)
- Loading branch information
Showing
2 changed files
with
59 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2024 the Xilem Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use web_sys::wasm_bindgen::JsCast; | ||
|
||
/// Helper to get the HTML document body element | ||
pub fn document_body() -> web_sys::HtmlElement { | ||
document().body().expect("HTML document missing body") | ||
} | ||
|
||
/// Helper to get the HTML document | ||
pub fn document() -> web_sys::Document { | ||
let window = web_sys::window().expect("no global `window` exists"); | ||
window.document().expect("should have a document on window") | ||
} | ||
|
||
/// Helper to get a DOM element by id | ||
pub fn get_element_by_id(id: &str) -> web_sys::HtmlElement { | ||
document() | ||
.get_element_by_id(id) | ||
.unwrap() | ||
.dyn_into() | ||
.unwrap() | ||
} | ||
|
||
/// Helper to get the value from an HTML input element from a given event. | ||
/// | ||
/// Returns `None` if the event isn't a valid input event or conversions fail. | ||
pub fn input_event_target_value(event: &web_sys::Event) -> Option<String> { | ||
event | ||
.target()? | ||
.dyn_into::<web_sys::HtmlInputElement>() | ||
.ok()? | ||
.value() | ||
.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters