refactor: window_event_listener

This commit is contained in:
luoxiao 2023-08-29 09:23:53 +08:00
parent 8a78de2e2d
commit 3a1cb72937
4 changed files with 5 additions and 43 deletions

View file

@ -1,9 +1,4 @@
use crate::{
teleport::Teleport,
theme::use_theme,
utils::{dom::window_event_listener, mount_style::mount_style},
Theme,
};
use crate::{teleport::Teleport, theme::use_theme, utils::mount_style::mount_style, Theme};
use leptos::*;
use std::hash::Hash;
use stylers::style_sheet_str;
@ -73,7 +68,7 @@ where
}
is_show_popover.set(false);
});
on_cleanup(timer);
on_cleanup(move || timer.remove());
let temp_options = options.clone();
let select_option_label = create_memo(move |_| match value.get() {

View file

@ -1,8 +1,4 @@
use crate::{
theme::use_theme,
utils::{dom::window_event_listener, mount_style::mount_style},
Theme,
};
use crate::{theme::use_theme, utils::mount_style::mount_style, Theme};
use leptos::*;
use stylers::style_sheet_str;
use wasm_bindgen::JsCast;
@ -47,7 +43,7 @@ pub fn Slider(
let on_mouse_up = window_event_listener(ev::mouseup, move |_| {
set_mouse_move.set(false);
});
on_cleanup(on_mouse_up);
on_cleanup(move || on_mouse_up.remove());
let on_mouse_move = window_event_listener(ev::mousemove, move |ev| {
if is_mouse_move.get_untracked() {
@ -68,7 +64,7 @@ pub fn Slider(
};
}
});
on_cleanup(on_mouse_move);
on_cleanup(move || on_mouse_move.remove());
view! { class=class_name,
<div class="melt-slider" style=move || css_vars.get()>

View file

@ -1,28 +0,0 @@
use leptos::window;
use leptos_dom::ev;
use std::borrow::Cow;
use wasm_bindgen::{prelude::Closure, JsCast};
pub fn window_event_listener<E: ev::EventDescriptor + 'static>(
event: E,
cb: impl Fn(E::EventType) + 'static,
) -> impl FnOnce() -> ()
where
E::EventType: JsCast,
{
fn wel(
cb: Box<dyn FnMut(web_sys::Event)>,
event_name: Cow<'static, str>,
) -> impl FnOnce() -> () + 'static {
let cb = Closure::wrap(cb).into_js_value();
_ = window().add_event_listener_with_callback(&event_name, cb.unchecked_ref());
move || {
_ = window().remove_event_listener_with_callback(&event_name, cb.unchecked_ref());
}
}
wel(
Box::new(move |e| cb(e.unchecked_into::<E::EventType>())),
event.name(),
)
}

View file

@ -1,2 +1 @@
pub mod dom;
pub(crate) mod mount_style;