added `HtmlElement::prop` to the builder

This commit is contained in:
Jose Quesada 2022-12-04 21:30:48 -06:00
parent 6737413103
commit abed797027
1 changed files with 22 additions and 1 deletions

View File

@ -15,7 +15,7 @@ use std::{
fmt,
ops::Deref,
};
use wasm_bindgen::{convert::FromWasmAbi, intern, JsCast};
use wasm_bindgen::{convert::FromWasmAbi, intern, JsCast, JsValue};
/// Trait alias for the trait bounts on [`IntoElement`].
#[cfg(all(target_arch = "wasm32", feature = "web"))]
@ -370,6 +370,27 @@ impl<El: IntoElement> HtmlElement<El> {
self.dyn_attr("class", f)
}
/// Adds a prop to this element.
#[track_caller]
pub fn prop(
self,
name: impl Into<Cow<'static, str>>,
value: impl Into<JsValue>,
) -> Self {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
{
let name = name.into();
let value = value.into();
let name: &str = &name;
js_sys::Reflect::set(&self, &name.into(), &value)
.expect("set property to not err");
}
self
}
/// Adds an event listener to this element.
#[track_caller]
pub fn on<E: EventDescriptor + 'static>(