mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-23 09:09:21 -05:00
Merge pull request #30 from lpotthast/main
This commit is contained in:
commit
7f0ddc830f
5 changed files with 21 additions and 26 deletions
|
@ -1,11 +1,10 @@
|
||||||
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
||||||
|
|
||||||
use crate::use_event_listener_with_options;
|
use crate::{use_event_listener_with_options, UseEventListenerOptions};
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use leptos::ev::{blur, focus};
|
use leptos::ev::{blur, focus};
|
||||||
use leptos::html::{AnyElement, ToHtmlElement};
|
use leptos::html::{AnyElement, ToHtmlElement};
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use web_sys::AddEventListenerOptions;
|
|
||||||
|
|
||||||
/// Reactive `document.activeElement`
|
/// Reactive `document.activeElement`
|
||||||
///
|
///
|
||||||
|
@ -49,8 +48,8 @@ pub fn use_active_element() -> Signal<Option<HtmlElement<AnyElement>>> {
|
||||||
let (active_element, set_active_element) = create_signal(get_active_element());
|
let (active_element, set_active_element) = create_signal(get_active_element());
|
||||||
|
|
||||||
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
let mut listener_options = AddEventListenerOptions::new();
|
let listener_options = UseEventListenerOptions::default()
|
||||||
listener_options.capture(true);
|
.capture(true);
|
||||||
|
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
window(),
|
window(),
|
||||||
|
@ -62,7 +61,7 @@ pub fn use_active_element() -> Signal<Option<HtmlElement<AnyElement>>> {
|
||||||
|
|
||||||
set_active_element.update(|el| *el = get_active_element());
|
set_active_element.update(|el| *el = get_active_element());
|
||||||
},
|
},
|
||||||
listener_options.clone(),
|
listener_options,
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
|
|
|
@ -80,7 +80,7 @@ where
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut listener_options = UseEventListenerOptions::default().passive(true);
|
let listener_options = UseEventListenerOptions::default().passive(true);
|
||||||
|
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
el.clone(),
|
el.clone(),
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
||||||
|
|
||||||
use crate::core::{ElementMaybeSignal, Position};
|
use crate::core::{ElementMaybeSignal, Position};
|
||||||
use crate::use_event_listener_with_options;
|
use crate::{use_event_listener_with_options, UseEventListenerOptions};
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::ev::{dragover, mousemove, touchend, touchmove, touchstart};
|
use leptos::ev::{dragover, mousemove, touchend, touchmove, touchstart};
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use wasm_bindgen::{JsCast, JsValue};
|
use wasm_bindgen::{JsCast, JsValue};
|
||||||
use web_sys::AddEventListenerOptions;
|
|
||||||
|
|
||||||
/// Reactive mouse position
|
/// Reactive mouse position
|
||||||
///
|
///
|
||||||
|
@ -158,40 +157,40 @@ where
|
||||||
|
|
||||||
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
let target = options.target;
|
let target = options.target;
|
||||||
let mut event_listener_options = AddEventListenerOptions::new();
|
let event_listener_options = UseEventListenerOptions::default()
|
||||||
event_listener_options.passive(true);
|
.passive(true);
|
||||||
|
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
target.clone(),
|
target.clone(),
|
||||||
mousemove,
|
mousemove,
|
||||||
mouse_handler,
|
mouse_handler,
|
||||||
event_listener_options.clone(),
|
event_listener_options,
|
||||||
);
|
);
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
target.clone(),
|
target.clone(),
|
||||||
dragover,
|
dragover,
|
||||||
drag_handler,
|
drag_handler,
|
||||||
event_listener_options.clone(),
|
event_listener_options,
|
||||||
);
|
);
|
||||||
if options.touch && !matches!(options.coord_type, UseMouseCoordType::Movement) {
|
if options.touch && !matches!(options.coord_type, UseMouseCoordType::Movement) {
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
target.clone(),
|
target.clone(),
|
||||||
touchstart,
|
touchstart,
|
||||||
touch_handler.clone(),
|
touch_handler.clone(),
|
||||||
event_listener_options.clone(),
|
event_listener_options,
|
||||||
);
|
);
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
target.clone(),
|
target.clone(),
|
||||||
touchmove,
|
touchmove,
|
||||||
touch_handler,
|
touch_handler,
|
||||||
event_listener_options.clone(),
|
event_listener_options,
|
||||||
);
|
);
|
||||||
if options.reset_on_touch_ends {
|
if options.reset_on_touch_ends {
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
target,
|
target,
|
||||||
touchend,
|
touchend,
|
||||||
move |_| reset(),
|
move |_| reset(),
|
||||||
event_listener_options.clone(),
|
event_listener_options,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::core::ElementMaybeSignal;
|
use crate::core::ElementMaybeSignal;
|
||||||
use crate::use_event_listener::use_event_listener_with_options;
|
use crate::use_event_listener::use_event_listener_with_options;
|
||||||
use crate::{use_debounce_fn_with_arg, use_throttle_fn_with_arg_and_options, ThrottleOptions};
|
use crate::{use_debounce_fn_with_arg, use_throttle_fn_with_arg_and_options, ThrottleOptions, UseEventListenerOptions};
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::ev::scrollend;
|
use leptos::ev::scrollend;
|
||||||
|
@ -377,7 +377,7 @@ where
|
||||||
target,
|
target,
|
||||||
ev::scroll,
|
ev::scroll,
|
||||||
handler,
|
handler,
|
||||||
options.event_listener_options.clone().unwrap_or_default(),
|
options.event_listener_options.unwrap_or_default(),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let _ = use_event_listener_with_options::<
|
let _ = use_event_listener_with_options::<
|
||||||
|
@ -389,7 +389,7 @@ where
|
||||||
target,
|
target,
|
||||||
ev::scroll,
|
ev::scroll,
|
||||||
on_scroll_handler,
|
on_scroll_handler,
|
||||||
options.event_listener_options.clone().unwrap_or_default(),
|
options.event_listener_options.unwrap_or_default(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -454,7 +454,7 @@ pub struct UseScrollOptions {
|
||||||
|
|
||||||
/// Options passed to the `addEventListener("scroll", ...)` call
|
/// Options passed to the `addEventListener("scroll", ...)` call
|
||||||
#[builder(into)]
|
#[builder(into)]
|
||||||
event_listener_options: Option<web_sys::AddEventListenerOptions>,
|
event_listener_options: Option<UseEventListenerOptions>,
|
||||||
|
|
||||||
/// When changing the `x` or `y` signals this specifies the scroll behaviour.
|
/// When changing the `x` or `y` signals this specifies the scroll behaviour.
|
||||||
/// Can be `Auto` (= not smooth) or `Smooth`. Defaults to `Auto`.
|
/// Can be `Auto` (= not smooth) or `Smooth`. Defaults to `Auto`.
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
||||||
|
|
||||||
use crate::use_event_listener_with_options;
|
use crate::{use_event_listener_with_options, UseEventListenerOptions};
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use leptos::ev::scroll;
|
use leptos::ev::scroll;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use web_sys::AddEventListenerOptions;
|
|
||||||
|
|
||||||
/// Reactive window scroll.
|
/// Reactive window scroll.
|
||||||
///
|
///
|
||||||
|
@ -41,10 +40,6 @@ pub fn use_window_scroll() -> (Signal<f64>, Signal<f64>) {
|
||||||
let (y, set_y) = create_signal(initial_y);
|
let (y, set_y) = create_signal(initial_y);
|
||||||
|
|
||||||
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
let mut options = AddEventListenerOptions::new();
|
|
||||||
options.capture(false);
|
|
||||||
options.passive(true);
|
|
||||||
|
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
window(),
|
window(),
|
||||||
scroll,
|
scroll,
|
||||||
|
@ -52,7 +47,9 @@ pub fn use_window_scroll() -> (Signal<f64>, Signal<f64>) {
|
||||||
set_x.set(window().scroll_x().unwrap_or_default());
|
set_x.set(window().scroll_x().unwrap_or_default());
|
||||||
set_y.set(window().scroll_y().unwrap_or_default());
|
set_y.set(window().scroll_y().unwrap_or_default());
|
||||||
},
|
},
|
||||||
options,
|
UseEventListenerOptions::default()
|
||||||
|
.capture(false)
|
||||||
|
.passive(true),
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue