From ada0c27d712a2a79dffafecb6b3a29a94bcf6144 Mon Sep 17 00:00:00 2001 From: Jens Krause Date: Thu, 13 Jul 2023 08:04:01 +0200 Subject: [PATCH 1/2] Fix callback handling of `use_scroll` by bumping `default-struct-builder` --- Cargo.toml | 2 +- examples/use_scroll/src/main.rs | 15 ++++++++++++++- src/use_scroll.rs | 8 +++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1e77af5..80cc532 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ homepage = "https://leptos-use.rs" leptos = "0.4" wasm-bindgen = "0.2" js-sys = "0.3" -default-struct-builder = "0.3" +default-struct-builder = "0.4" num = { version = "0.4", optional = true } serde = { version = "1", optional = true } serde_json = { version = "1", optional = true } diff --git a/examples/use_scroll/src/main.rs b/examples/use_scroll/src/main.rs index 23e7ac6..898e3a6 100644 --- a/examples/use_scroll/src/main.rs +++ b/examples/use_scroll/src/main.rs @@ -3,6 +3,8 @@ use leptos::*; use leptos_use::docs::{demo_or_body, BooleanDisplay}; use leptos_use::{use_scroll_with_options, ScrollBehavior, UseScrollOptions, UseScrollReturn}; +use web_sys::Event; + #[component] fn Demo(cx: Scope) -> impl IntoView { let el = create_node_ref::
(cx); @@ -24,7 +26,18 @@ fn Demo(cx: Scope) -> impl IntoView { arrived_state, directions, .. - } = use_scroll_with_options(cx, el, UseScrollOptions::default().behavior(behavior)); + } = use_scroll_with_options( + cx, + el, + UseScrollOptions::default() + .behavior(behavior) + .on_stop(move |_: Event| { + log!("scrolling stopped"); + }) + .on_scroll(move |_: Event| { + log!("scrolling"); + }), + ); view! { cx,
diff --git a/src/use_scroll.rs b/src/use_scroll.rs index e22bff1..3576218 100644 --- a/src/use_scroll.rs +++ b/src/use_scroll.rs @@ -1,6 +1,6 @@ use crate::core::ElementMaybeSignal; use crate::use_event_listener::use_event_listener_with_options; -use crate::utils::CloneableFnWithArg; +use crate::utils::CloneableFnMutWithArg; use crate::{use_debounce_fn_with_arg, use_throttle_fn_with_arg_and_options, ThrottleOptions}; use default_struct_builder::DefaultBuilder; use leptos::ev::EventDescriptor; @@ -456,12 +456,10 @@ pub struct UseScrollOptions { offset: ScrollOffset, /// Callback when scrolling is happening. - #[builder(into)] - on_scroll: Box>, + on_scroll: Box + 'static>, /// Callback when scrolling stops (after `idle` + `throttle` milliseconds have passed). - #[builder(into)] - on_stop: Box>, + on_stop: Box + 'static>, /// Options passed to the `addEventListener("scroll", ...)` call event_listener_options: web_sys::AddEventListenerOptions, From e50fa080c1215ec2d9c0814d30f8451888ec20df Mon Sep 17 00:00:00 2001 From: Jens Krause Date: Thu, 13 Jul 2023 08:21:54 +0200 Subject: [PATCH 2/2] CloneableFnMutWithArg -> CloneableFnWithArg --- examples/use_scroll/src/main.rs | 16 ++++++++++------ src/use_scroll.rs | 6 +++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/examples/use_scroll/src/main.rs b/examples/use_scroll/src/main.rs index 898e3a6..0c8ca35 100644 --- a/examples/use_scroll/src/main.rs +++ b/examples/use_scroll/src/main.rs @@ -17,6 +17,14 @@ fn Demo(cx: Scope) -> impl IntoView { } }); + let on_stop = |_: Event| { + log!("scrolling stopped"); + }; + + let on_scroll = |_: Event| { + log!("scrolling"); + }; + let UseScrollReturn { x, y, @@ -31,12 +39,8 @@ fn Demo(cx: Scope) -> impl IntoView { el, UseScrollOptions::default() .behavior(behavior) - .on_stop(move |_: Event| { - log!("scrolling stopped"); - }) - .on_scroll(move |_: Event| { - log!("scrolling"); - }), + .on_stop(on_stop.clone()) + .on_scroll(on_scroll.clone()), ); view! { cx, diff --git a/src/use_scroll.rs b/src/use_scroll.rs index 3576218..87b9346 100644 --- a/src/use_scroll.rs +++ b/src/use_scroll.rs @@ -1,6 +1,6 @@ use crate::core::ElementMaybeSignal; use crate::use_event_listener::use_event_listener_with_options; -use crate::utils::CloneableFnMutWithArg; +use crate::utils::CloneableFnWithArg; use crate::{use_debounce_fn_with_arg, use_throttle_fn_with_arg_and_options, ThrottleOptions}; use default_struct_builder::DefaultBuilder; use leptos::ev::EventDescriptor; @@ -456,10 +456,10 @@ pub struct UseScrollOptions { offset: ScrollOffset, /// Callback when scrolling is happening. - on_scroll: Box + 'static>, + on_scroll: Box + 'static>, /// Callback when scrolling stops (after `idle` + `throttle` milliseconds have passed). - on_stop: Box + 'static>, + on_stop: Box + 'static>, /// Options passed to the `addEventListener("scroll", ...)` call event_listener_options: web_sys::AddEventListenerOptions,