Skip to main content

leptos_use/
use_scroll.rs

1use crate::UseEventListenerOptions;
2use crate::core::{Direction, Directions, IntoElementMaybeSignal};
3use cfg_if::cfg_if;
4use default_struct_builder::DefaultBuilder;
5use leptos::prelude::*;
6use leptos::reactive::wrappers::read::Signal;
7use std::rc::Rc;
8
9cfg_if! { if #[cfg(not(feature = "ssr"))] {
10use crate::use_event_listener::use_event_listener_with_options;
11use crate::{
12    sendwrap_fn, use_debounce_fn_with_arg, use_throttle_fn_with_arg_and_options, ThrottleOptions,
13};
14use leptos::ev;
15use leptos::ev::scrollend;
16use send_wrapper::SendWrapper;
17use wasm_bindgen::JsCast;
18
19
20/// We have to check if the scroll amount is close enough to some threshold in order to
21/// more accurately calculate arrivedState. This is because scrollTop/scrollLeft are non-rounded
22/// numbers, while scrollHeight/scrollWidth and clientHeight/clientWidth are rounded.
23/// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#determine_if_an_element_has_been_totally_scrolled
24const ARRIVED_STATE_THRESHOLD_PIXELS: f64 = 1.0;
25}}
26
27/// Reactive scroll position and state.
28///
29/// ## Demo
30///
31/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_scroll)
32///
33/// ## Usage
34///
35/// ```
36/// # use leptos::prelude::*;
37/// # use leptos::ev::resize;
38/// # use leptos::html::Div;
39/// # use leptos_use::{use_scroll, UseScrollReturn};
40/// #
41/// # #[component]
42/// # fn Demo() -> impl IntoView {
43/// let element = NodeRef::<Div>::new();
44///
45/// let UseScrollReturn {
46///     x, y, set_x, set_y, is_scrolling, arrived_state, directions, ..
47/// } = use_scroll(element);
48///
49/// view! {
50///     <div node_ref=element>"..."</div>
51/// }
52/// # }
53/// ```
54///
55/// ### With Offsets
56///
57/// You can provide offsets when you use [`use_scroll_with_options`].
58/// These offsets are thresholds in pixels when a side is considered to have arrived. This is reflected in the return field `arrived_state`.
59///
60/// ```
61/// # use leptos::prelude::*;
62/// # use leptos::html::Div;
63/// # use leptos::ev::resize;
64/// # use leptos_use::{use_scroll_with_options, UseScrollReturn, UseScrollOptions, ScrollOffset};
65/// #
66/// # #[component]
67/// # fn Demo() -> impl IntoView {
68/// # let element = NodeRef::<Div>::new();
69/// #
70/// let UseScrollReturn {
71///     x,
72///     y,
73///     set_x,
74///     set_y,
75///     is_scrolling,
76///     arrived_state,
77///     directions,
78///     ..
79/// } = use_scroll_with_options(
80///     element,
81///     UseScrollOptions::default().offset(ScrollOffset {
82///         top: 30.0,
83///         bottom: 30.0,
84///         right: 30.0,
85///         left: 30.0,
86///     }),
87/// );
88/// #
89/// #     view! { /// #         <div node_ref=element>"..."</div>
90/// #     }
91/// # }
92/// ```
93///
94/// ### Setting Scroll Position
95///
96/// Set the `x` and `y` values to make the element scroll to that position.
97///
98/// ```
99/// # use leptos::prelude::*;
100/// # use leptos::html::Div;
101/// # use leptos::ev::resize;
102/// # use leptos_use::{use_scroll, UseScrollReturn};
103/// #
104/// # #[component]
105/// # fn Demo() -> impl IntoView {
106/// let element = NodeRef::<Div>::new();
107///
108/// let UseScrollReturn {
109///     x, y, set_x, set_y, ..
110/// } = use_scroll(element);
111///
112/// view! {
113///     <div node_ref=element>"..."</div>
114///     <button on:click=move |_| set_x(x.get_untracked() + 10.0)>"Scroll right 10px"</button>
115///     <button on:click=move |_| set_y(y.get_untracked() + 10.0)>"Scroll down 10px"</button>
116/// }
117/// # }
118/// ```
119///
120/// ### Smooth Scrolling
121///
122/// Set `behavior: smooth` to enable smooth scrolling. The `behavior` option defaults to `auto`,
123/// which means no smooth scrolling. See the `behavior` option on
124/// [Element.scrollTo](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTo) for more information.
125///
126/// ```
127/// # use leptos::prelude::*;
128/// # use leptos::ev::resize;
129/// # use leptos::html::Div;
130/// # use leptos_use::{use_scroll_with_options, UseScrollReturn, UseScrollOptions, ScrollBehavior};
131/// #
132/// # #[component]
133/// # fn Demo() -> impl IntoView {
134/// # let element = NodeRef::<Div>::new();
135/// #
136/// let UseScrollReturn {
137///     x, y, set_x, set_y, ..
138/// } = use_scroll_with_options(
139///     element,
140///     UseScrollOptions::default().behavior(ScrollBehavior::Smooth),
141/// );
142/// #
143/// # view! { /// #     <div node_ref=element>"..."</div>
144/// # }
145/// # }
146/// ```
147///
148/// or as a `Signal`:
149///
150/// ```
151/// # use leptos::prelude::*;
152/// # use leptos::ev::resize;
153/// # use leptos::html::Div;
154/// # use leptos_use::{use_scroll_with_options, UseScrollReturn, UseScrollOptions, ScrollBehavior};
155/// #
156/// # #[component]
157/// # fn Demo() -> impl IntoView {
158/// # let element = NodeRef::<Div>::new();
159/// #
160/// let (smooth, set_smooth) = signal(false);
161///
162/// let behavior = Signal::derive(move || {
163///     if smooth.get() { ScrollBehavior::Smooth } else { ScrollBehavior::Auto }
164/// });
165///
166/// let UseScrollReturn {
167///     x, y, set_x, set_y, ..
168/// } = use_scroll_with_options(
169///     element,
170///     UseScrollOptions::default().behavior(behavior),
171/// );
172/// #
173/// # view! { /// #     <div node_ref=element>"..."</div>
174/// # }
175/// # }
176/// ```
177///
178/// ## SendWrapped Return
179///
180/// The returned closures `set_x`, `set_y` and `measure` are sendwrapped functions. They can
181/// only be called from the same thread that called `use_scroll`.
182///
183/// ## Server-Side Rendering
184///
185/// > Make sure you follow the [instructions in Server-Side Rendering](https://leptos-use.rs/server_side_rendering.html).
186///
187/// On the server this returns signals that don't change and setters that are noops.
188pub fn use_scroll<El, M>(
189    element: El,
190) -> UseScrollReturn<
191    impl Fn(f64) + Clone + Send + Sync,
192    impl Fn(f64) + Clone + Send + Sync,
193    impl Fn() + Clone + Send + Sync,
194>
195where
196    El: IntoElementMaybeSignal<web_sys::Element, M>,
197{
198    use_scroll_with_options(element, Default::default())
199}
200
201/// Version of [`use_scroll`] with options. See [`use_scroll`] for how to use.
202#[cfg_attr(feature = "ssr", allow(unused_variables))]
203pub fn use_scroll_with_options<El, M>(
204    element: El,
205    options: UseScrollOptions,
206) -> UseScrollReturn<
207    impl Fn(f64) + Clone + Send + Sync,
208    impl Fn(f64) + Clone + Send + Sync,
209    impl Fn() + Clone + Send + Sync,
210>
211where
212    El: IntoElementMaybeSignal<web_sys::Element, M>,
213{
214    let (internal_x, set_internal_x) = signal(0.0);
215    let (internal_y, set_internal_y) = signal(0.0);
216
217    let (is_scrolling, set_is_scrolling) = signal(false);
218
219    let arrived_state = RwSignal::new(Directions {
220        left: true,
221        right: false,
222        top: true,
223        bottom: false,
224    });
225    let directions = RwSignal::new(Directions {
226        left: false,
227        right: false,
228        top: false,
229        bottom: false,
230    });
231
232    let set_x;
233    let set_y;
234    let measure;
235
236    #[cfg(feature = "ssr")]
237    {
238        set_x = |_| {};
239        set_y = |_| {};
240        measure = || {};
241    }
242
243    #[cfg(not(feature = "ssr"))]
244    {
245        let signal = element.into_element_maybe_signal();
246        let behavior = options.behavior;
247
248        let scroll_to = move |x: Option<f64>, y: Option<f64>| {
249            let element = signal.get_untracked();
250
251            if let Some(element) = element {
252                let scroll_options = web_sys::ScrollToOptions::new();
253                scroll_options.set_behavior(behavior.get_untracked().into());
254
255                if let Some(x) = x {
256                    scroll_options.set_left(x);
257                }
258                if let Some(y) = y {
259                    scroll_options.set_top(y);
260                }
261
262                element.scroll_to_with_scroll_to_options(&scroll_options);
263            }
264        };
265
266        set_x = sendwrap_fn!(move |x| scroll_to(Some(x), None));
267
268        set_y = sendwrap_fn!(move |y| scroll_to(None, Some(y)));
269
270        let on_scroll_end = {
271            let on_stop = Rc::clone(&options.on_stop);
272
273            move |e| {
274                if !is_scrolling.try_get_untracked().unwrap_or_default() {
275                    return;
276                }
277
278                set_is_scrolling.set(false);
279                directions.update(|directions| {
280                    directions.left = false;
281                    directions.right = false;
282                    directions.top = false;
283                    directions.bottom = false;
284                    on_stop.clone()(e);
285                });
286            }
287        };
288
289        let throttle = options.throttle;
290
291        let on_scroll_end_debounced =
292            use_debounce_fn_with_arg(on_scroll_end.clone(), throttle + options.idle);
293
294        let offset = options.offset;
295
296        #[allow(clippy::unnecessary_cast)]
297        let set_arrived_state = move |target: web_sys::Element| {
298            let style = window()
299                .get_computed_style(&target)
300                .expect("failed to get computed style");
301
302            if let Some(style) = style {
303                let display = style
304                    .get_property_value("display")
305                    .expect("failed to get display");
306                let flex_direction = style
307                    .get_property_value("flex-direction")
308                    .expect("failed to get flex-direction");
309
310                let scroll_left = target.scroll_left() as f64;
311                let scroll_left_abs = scroll_left.abs();
312
313                directions.update(|directions| {
314                    directions.left = scroll_left < internal_x.get_untracked();
315                    directions.right = scroll_left > internal_x.get_untracked();
316                });
317
318                let left = scroll_left_abs <= offset.left;
319                let right = scroll_left_abs + target.client_width() as f64
320                    >= target.scroll_width() as f64 - offset.right - ARRIVED_STATE_THRESHOLD_PIXELS;
321
322                arrived_state.update(|arrived_state| {
323                    if display == "flex" && flex_direction == "row-reverse" {
324                        arrived_state.left = right;
325                        arrived_state.right = left;
326                    } else {
327                        arrived_state.left = left;
328                        arrived_state.right = right;
329                    }
330                });
331                set_internal_x.set(scroll_left);
332
333                let mut scroll_top = target.scroll_top() as f64;
334
335                // patch for mobile compatibility
336                if target == document().unchecked_into::<web_sys::Element>() && scroll_top == 0.0 {
337                    scroll_top = document().body().expect("failed to get body").scroll_top() as f64;
338                }
339
340                let scroll_top_abs = scroll_top.abs();
341
342                directions.update(|directions| {
343                    directions.top = scroll_top < internal_y.get_untracked();
344                    directions.bottom = scroll_top > internal_y.get_untracked();
345                });
346
347                let top = scroll_top_abs <= offset.top;
348                let bottom = scroll_top_abs + target.client_height() as f64
349                    >= target.scroll_height() as f64
350                        - offset.bottom
351                        - ARRIVED_STATE_THRESHOLD_PIXELS;
352
353                // reverse columns and rows behave exactly the other way around,
354                // bottom is treated as top and top is treated as the negative version of bottom
355                arrived_state.update(|arrived_state| {
356                    if display == "flex" && flex_direction == "column-reverse" {
357                        arrived_state.top = bottom;
358                        arrived_state.bottom = top;
359                    } else {
360                        arrived_state.top = top;
361                        arrived_state.bottom = bottom;
362                    }
363                });
364
365                set_internal_y.set(scroll_top);
366            }
367        };
368
369        let on_scroll_handler = {
370            let on_scroll = Rc::clone(&options.on_scroll);
371
372            move |e: web_sys::Event| {
373                let target: web_sys::Element = event_target(&e);
374
375                set_arrived_state(target);
376                set_is_scrolling.set(true);
377
378                on_scroll_end_debounced.clone()(e.clone());
379                on_scroll.clone()(e);
380            }
381        };
382
383        let target = Signal::derive(move || {
384            let element = signal.get();
385            element.map(|element| {
386                SendWrapper::new(element.take().unchecked_into::<web_sys::EventTarget>())
387            })
388        });
389
390        if throttle >= 0.0 {
391            use send_wrapper::SendWrapper;
392
393            let throttled_scroll_handler = use_throttle_fn_with_arg_and_options(
394                on_scroll_handler.clone(),
395                throttle,
396                ThrottleOptions {
397                    trailing: true,
398                    leading: false,
399                },
400            );
401
402            let handler = move |e: web_sys::Event| {
403                throttled_scroll_handler.clone()(e);
404            };
405
406            let _ = use_event_listener_with_options::<
407                _,
408                Signal<Option<SendWrapper<web_sys::EventTarget>>>,
409                _,
410                _,
411            >(target, ev::scroll, handler, options.event_listener_options);
412        } else {
413            let _ = use_event_listener_with_options::<
414                _,
415                Signal<Option<SendWrapper<web_sys::EventTarget>>>,
416                _,
417                _,
418            >(
419                target,
420                ev::scroll,
421                on_scroll_handler,
422                options.event_listener_options,
423            );
424        }
425
426        let _ = use_event_listener_with_options::<
427            _,
428            Signal<Option<SendWrapper<web_sys::EventTarget>>>,
429            _,
430            _,
431        >(
432            target,
433            scrollend,
434            on_scroll_end,
435            options.event_listener_options,
436        );
437
438        measure = sendwrap_fn!(move || {
439            if let Some(el) = signal.try_get_untracked().flatten() {
440                set_arrived_state(el.take());
441            }
442        });
443    }
444
445    UseScrollReturn {
446        x: internal_x.into(),
447        set_x,
448        y: internal_y.into(),
449        set_y,
450        is_scrolling: is_scrolling.into(),
451        arrived_state: arrived_state.into(),
452        directions: directions.into(),
453        measure,
454    }
455}
456
457/// Options for [`use_scroll`].
458#[derive(DefaultBuilder)]
459/// Options for [`use_scroll_with_options`].
460#[cfg_attr(feature = "ssr", allow(dead_code))]
461pub struct UseScrollOptions {
462    /// Throttle time in milliseconds for the scroll events. Defaults to 0 (disabled).
463    throttle: f64,
464
465    /// After scrolling ends we wait idle + throttle milliseconds before we consider scrolling to have stopped.
466    /// Defaults to 200.
467    idle: f64,
468
469    /// Threshold in pixels when we consider a side to have arrived (`UseScrollReturn::arrived_state`).
470    offset: ScrollOffset,
471
472    /// Callback when scrolling is happening.
473    on_scroll: Rc<dyn Fn(web_sys::Event)>,
474
475    /// Callback when scrolling stops (after `idle` + `throttle` milliseconds have passed).
476    on_stop: Rc<dyn Fn(web_sys::Event)>,
477
478    /// Options passed to the `addEventListener("scroll", ...)` call
479    event_listener_options: UseEventListenerOptions,
480
481    /// When changing the `x` or `y` signals this specifies the scroll behaviour.
482    /// Can be `Auto` (= not smooth) or `Smooth`. Defaults to `Auto`.
483    #[builder(into)]
484    behavior: Signal<ScrollBehavior>,
485}
486
487impl Default for UseScrollOptions {
488    fn default() -> Self {
489        Self {
490            throttle: 0.0,
491            idle: 200.0,
492            offset: ScrollOffset::default(),
493            on_scroll: Rc::new(|_| {}),
494            on_stop: Rc::new(|_| {}),
495            event_listener_options: Default::default(),
496            behavior: Default::default(),
497        }
498    }
499}
500
501/// The scroll behavior.
502/// Can be `Auto` (= not smooth) or `Smooth`. Defaults to `Auto`.
503#[derive(Default, Copy, Clone)]
504pub enum ScrollBehavior {
505    #[default]
506    Auto,
507    Smooth,
508}
509
510impl From<ScrollBehavior> for web_sys::ScrollBehavior {
511    fn from(val: ScrollBehavior) -> Self {
512        match val {
513            ScrollBehavior::Auto => web_sys::ScrollBehavior::Auto,
514            ScrollBehavior::Smooth => web_sys::ScrollBehavior::Smooth,
515        }
516    }
517}
518
519/// The return value of [`use_scroll`].
520pub struct UseScrollReturn<SetXFn, SetYFn, MFn>
521where
522    SetXFn: Fn(f64) + Clone + Send + Sync,
523    SetYFn: Fn(f64) + Clone + Send + Sync,
524    MFn: Fn() + Clone + Send + Sync,
525{
526    /// X coordinate of scroll position
527    pub x: Signal<f64>,
528
529    /// Sets the value of `x`. This does also scroll the element.
530    pub set_x: SetXFn,
531
532    /// Y coordinate of scroll position
533    pub y: Signal<f64>,
534
535    /// Sets the value of `y`. This does also scroll the element.
536    pub set_y: SetYFn,
537
538    /// Is true while the element is being scrolled.
539    pub is_scrolling: Signal<bool>,
540
541    /// Sets the field that represents a direction to true if the
542    /// element is scrolled all the way to that side.
543    pub arrived_state: Signal<Directions>,
544
545    /// The directions in which the element is being scrolled are set to true.
546    pub directions: Signal<Directions>,
547
548    /// Re-evaluates the `arrived_state`.
549    pub measure: MFn,
550}
551
552#[derive(Default, Copy, Clone, Debug)]
553/// Threshold in pixels when we consider a side to have arrived (`UseScrollReturn::arrived_state`).
554pub struct ScrollOffset {
555    pub left: f64,
556    pub top: f64,
557    pub right: f64,
558    pub bottom: f64,
559}
560
561impl ScrollOffset {
562    /// Sets the value of the provided direction
563    pub fn set_direction(mut self, direction: Direction, value: f64) -> Self {
564        match direction {
565            Direction::Top => self.top = value,
566            Direction::Bottom => self.bottom = value,
567            Direction::Left => self.left = value,
568            Direction::Right => self.right = value,
569        }
570
571        self
572    }
573}