Skip to content

Commit

Permalink
xilem_web: Add event_target_value helper function (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse authored Aug 12, 2024
1 parent c9dbb21 commit 7d137a7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
36 changes: 36 additions & 0 deletions xilem_web/src/dom_helpers.rs
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()
}
60 changes: 23 additions & 37 deletions xilem_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
//! </style>
#![doc = include_str!("../README.md")]

use core::{
Adapt, AdaptThunk, AnyElement, AnyView, MapAction, MapState, MessageResult, SuperElement, View,
ViewElement,
};
use std::any::Any;

use wasm_bindgen::UnwrapThrowExt;
use web_sys::wasm_bindgen::JsCast;

Expand All @@ -33,6 +30,7 @@ mod attribute;
mod attribute_value;
mod class;
mod context;
mod dom_helpers;
mod element_props;
mod events;
mod message;
Expand All @@ -49,21 +47,29 @@ pub mod elements;
pub mod interfaces;
pub mod svg;

pub use after_update::{
after_build, after_rebuild, before_teardown, AfterBuild, AfterRebuild, BeforeTeardown,
pub use self::{
after_update::{
after_build, after_rebuild, before_teardown, AfterBuild, AfterRebuild, BeforeTeardown,
},
app::App,
attribute::{Attr, Attributes, ElementWithAttributes, WithAttributes},
attribute_value::{AttributeValue, IntoAttributeValue},
class::{AsClassIter, Class, Classes, ElementWithClasses, WithClasses},
context::{MessageThunk, ViewCtx},
dom_helpers::{document, document_body, get_element_by_id, input_event_target_value},
element_props::ElementProps,
message::{DynMessage, Message},
optional_action::{Action, OptionalAction},
pointer::{Pointer, PointerDetails, PointerMsg},
style::{style, ElementWithStyle, IntoStyles, Style, Styles, WithStyle},
};
pub use app::App;
pub use attribute::{Attr, Attributes, ElementWithAttributes, WithAttributes};
pub use attribute_value::{AttributeValue, IntoAttributeValue};
pub use class::{AsClassIter, Class, Classes, ElementWithClasses, WithClasses};
pub use context::{MessageThunk, ViewCtx};
pub use element_props::ElementProps;
pub use message::{DynMessage, Message};
pub use optional_action::{Action, OptionalAction};
pub use pointer::{Pointer, PointerDetails, PointerMsg};
pub use style::{style, ElementWithStyle, IntoStyles, Style, Styles, WithStyle};

pub use xilem_core as core;
use xilem_core::ViewSequence;

use xilem_core::{
Adapt, AdaptThunk, AnyElement, AnyView, MapAction, MapState, MessageResult, SuperElement, View,
ViewElement, ViewSequence,
};

/// A trait used for type erasure of [`DomNode`]s
/// It is e.g. used in [`AnyPod`]
Expand Down Expand Up @@ -381,26 +387,6 @@ impl DomNode<()> for web_sys::Text {
fn apply_props(&self, (): &mut ()) {}
}

/// 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()
}

// TODO specialize some of these elements, maybe via features?
macro_rules! impl_dom_node_for_elements {
($($ty:ident, )*) => {$(
Expand Down

0 comments on commit 7d137a7

Please sign in to comment.