Skip to main content

leptos_use_scroll/
options.rs

1use crate::{Direction, UseEventListenerOptions};
2use default_struct_builder::DefaultBuilder;
3use leptos::reactive::wrappers::read::Signal;
4use std::rc::Rc;
5
6/// Options for [`use_scroll`](crate::use_scroll).
7#[derive(DefaultBuilder)]
8/// Options for [`use_scroll_with_options`](crate::use_scroll_with_options).
9#[cfg_attr(feature = "ssr", allow(dead_code))]
10pub struct UseScrollOptions {
11    /// Throttle time in milliseconds for the scroll events. Defaults to 0 (disabled).
12    pub(crate) throttle: f64,
13
14    /// After scrolling ends we wait idle + throttle milliseconds before we consider scrolling to have stopped.
15    /// Defaults to 200.
16    pub(crate) idle: f64,
17
18    /// Threshold in pixels when we consider a side to have arrived (`UseScrollReturn::arrived_state`).
19    pub(crate) offset: ScrollOffset,
20
21    /// Callback when scrolling is happening.
22    pub(crate) on_scroll: Rc<dyn Fn(web_sys::Event)>,
23
24    /// Callback when scrolling stops (after `idle` + `throttle` milliseconds have passed).
25    pub(crate) on_stop: Rc<dyn Fn(web_sys::Event)>,
26
27    /// Options passed to the `addEventListener("scroll", ...)` call
28    pub(crate) event_listener_options: UseEventListenerOptions,
29
30    /// When changing the `x` or `y` signals this specifies the scroll behaviour.
31    /// Can be `Auto` (= not smooth) or `Smooth`. Defaults to `Auto`.
32    #[builder(into)]
33    pub(crate) behavior: Signal<ScrollBehavior>,
34}
35
36impl Default for UseScrollOptions {
37    fn default() -> Self {
38        Self {
39            throttle: 0.0,
40            idle: 200.0,
41            offset: ScrollOffset::default(),
42            on_scroll: Rc::new(|_| {}),
43            on_stop: Rc::new(|_| {}),
44            event_listener_options: Default::default(),
45            behavior: Default::default(),
46        }
47    }
48}
49
50/// The scroll behavior.
51/// Can be `Auto` (= not smooth) or `Smooth`. Defaults to `Auto`.
52#[derive(Default, Copy, Clone)]
53pub enum ScrollBehavior {
54    #[default]
55    Auto,
56    Smooth,
57}
58
59impl From<ScrollBehavior> for web_sys::ScrollBehavior {
60    fn from(val: ScrollBehavior) -> Self {
61        match val {
62            ScrollBehavior::Auto => web_sys::ScrollBehavior::Auto,
63            ScrollBehavior::Smooth => web_sys::ScrollBehavior::Smooth,
64        }
65    }
66}
67
68#[derive(Default, Copy, Clone, Debug)]
69/// Threshold in pixels when we consider a side to have arrived (`UseScrollReturn::arrived_state`).
70pub struct ScrollOffset {
71    pub left: f64,
72    pub top: f64,
73    pub right: f64,
74    pub bottom: f64,
75}
76
77impl ScrollOffset {
78    /// Sets the value of the provided direction
79    pub fn set_direction(mut self, direction: Direction, value: f64) -> Self {
80        match direction {
81            Direction::Top => self.top = value,
82            Direction::Bottom => self.bottom = value,
83            Direction::Left => self.left = value,
84            Direction::Right => self.right = value,
85        }
86
87        self
88    }
89}