use crate::{
html::{
attribute::Attribute,
class::IntoClass,
event::{on, EventDescriptor},
style::IntoStyle,
},
renderer::RemoveEventHandler,
};
use wasm_bindgen::JsValue;
use web_sys::Element;
pub trait ElementExt {
fn attr<At>(&self, attribute: At) -> At::State
where
At: Attribute;
fn class<C>(&self, class: C) -> C::State
where
C: IntoClass;
fn style<S>(&self, style: S) -> S::State
where
S: IntoStyle;
fn on<E>(
&self,
ev: E,
cb: impl FnMut(E::EventType) + 'static,
) -> RemoveEventHandler<Element>
where
E: EventDescriptor + Send + 'static,
E::EventType: 'static,
E::EventType: From<JsValue>;
}
impl<T> ElementExt for T
where
T: AsRef<Element>,
{
fn attr<At>(&self, attribute: At) -> At::State
where
At: Attribute,
{
attribute.build(self.as_ref())
}
fn class<C>(&self, class: C) -> C::State
where
C: IntoClass,
{
class.build(self.as_ref())
}
fn on<E>(
&self,
ev: E,
cb: impl FnMut(E::EventType) + 'static,
) -> RemoveEventHandler<Element>
where
E: EventDescriptor + Send + 'static,
E::EventType: 'static,
E::EventType: From<JsValue>,
{
on::<E, _>(ev, cb).attach(self.as_ref())
}
fn style<S>(&self, style: S) -> S::State
where
S: IntoStyle,
{
style.build(self.as_ref())
}
}