leptos_use_scroll/
options.rs1use crate::{Direction, UseEventListenerOptions};
2use default_struct_builder::DefaultBuilder;
3use leptos::reactive::wrappers::read::Signal;
4use std::rc::Rc;
5
6#[derive(DefaultBuilder)]
8#[cfg_attr(feature = "ssr", allow(dead_code))]
10pub struct UseScrollOptions {
11 pub(crate) throttle: f64,
13
14 pub(crate) idle: f64,
17
18 pub(crate) offset: ScrollOffset,
20
21 pub(crate) on_scroll: Rc<dyn Fn(web_sys::Event)>,
23
24 pub(crate) on_stop: Rc<dyn Fn(web_sys::Event)>,
26
27 pub(crate) event_listener_options: UseEventListenerOptions,
29
30 #[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#[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)]
69pub struct ScrollOffset {
71 pub left: f64,
72 pub top: f64,
73 pub right: f64,
74 pub bottom: f64,
75}
76
77impl ScrollOffset {
78 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}