diff --git a/leptos_dom/Cargo.toml b/leptos_dom/Cargo.toml index 4c2ad0988..a39a82f02 100644 --- a/leptos_dom/Cargo.toml +++ b/leptos_dom/Cargo.toml @@ -53,6 +53,20 @@ features = [ "Text", "TreeWalker", "Window", + + # Events we cast to in leptos_macro -- added here so we don't force users to import them + "MouseEvent", + "DragEvent", + "FocusEvent", + "KeyboardEvent", + "ProgressEvent", + "WheelEvent", + "InputEvent", + "SubmitEvent", + "AnimationEvent", + "PointerEvent", + "TouchEvent", + "TransitionEvent" ] [dev-dependencies] diff --git a/leptos_dom/src/operations.rs b/leptos_dom/src/operations.rs index f28f4c807..4ef0d2762 100644 --- a/leptos_dom/src/operations.rs +++ b/leptos_dom/src/operations.rs @@ -1,7 +1,7 @@ use std::time::Duration; -use wasm_bindgen::{prelude::Closure, JsCast, JsValue, UnwrapThrowExt}; use wasm_bindgen::convert::FromWasmAbi; +use wasm_bindgen::{prelude::Closure, JsCast, JsValue, UnwrapThrowExt}; use crate::{debug_warn, event_delegation, is_server}; @@ -182,8 +182,12 @@ where /// Helper function to extract `event.target.value` from an event. /// /// This is useful in the `on:input` or `on:change` listeners for an `` element. -pub fn event_target_value(event: &web_sys::Event) -> String { +pub fn event_target_value(event: &T) -> String +where + T: JsCast, +{ event + .unchecked_ref::() .target() .unwrap_throw() .unchecked_into::() @@ -255,7 +259,9 @@ pub fn add_event_listener( target: &web_sys::Element, event_name: &'static str, cb: impl FnMut(E) + 'static, -) where E: FromWasmAbi + 'static { +) where + E: FromWasmAbi + 'static, +{ let cb = Closure::wrap(Box::new(cb) as Box).into_js_value(); let key = event_delegation::event_delegation_key(event_name); _ = js_sys::Reflect::set(target, &JsValue::from_str(&key), &cb); diff --git a/leptos_macro/src/view.rs b/leptos_macro/src/view.rs index f6ab57306..acf40dc91 100644 --- a/leptos_macro/src/view.rs +++ b/leptos_macro/src/view.rs @@ -590,11 +590,11 @@ fn attr_to_tokens( }); } else if let Some(event_type) = EVENTS.get(&name.as_str()).map(|&e| e.parse::().unwrap_or_default()) { expressions.push(quote_spanned! { - span => ::leptos::add_event_listener::<#event_type>(#el_id.unchecked_ref(), #name, #handler); + span => ::leptos::add_event_listener::(#el_id.unchecked_ref(), #name, #handler); }); } else { expressions.push(quote_spanned! { - span => ::leptos::add_event_listener::(#el_id.unchecked_ref(), #name, #handler); + span => ::leptos::add_event_listener::(#el_id.unchecked_ref(), #name, #handler); }); } } else { @@ -1117,7 +1117,7 @@ fn create_component(cx: &Ident, node: &NodeElement, mode: Mode) -> TokenStream { }) } else { Some(quote_spanned! { - span => ::leptos::add_event_listener::(#component_name.unchecked_ref(), #event_name, #handler) + span => ::leptos::add_event_listener::(#component_name.unchecked_ref(), #event_name, #handler) }) } }