use crate::{Direction, UseEventListenerOptions};
use default_struct_builder::DefaultBuilder;
use leptos::reactive::wrappers::read::Signal;
use std::rc::Rc;
#[derive(DefaultBuilder)]
#[cfg_attr(feature = "ssr", allow(dead_code))]
pub struct UseScrollOptions {
pub(crate) throttle: f64,
pub(crate) idle: f64,
pub(crate) offset: ScrollOffset,
pub(crate) on_scroll: Rc<dyn Fn(web_sys::Event)>,
pub(crate) on_stop: Rc<dyn Fn(web_sys::Event)>,
pub(crate) event_listener_options: UseEventListenerOptions,
#[builder(into)]
pub(crate) behavior: Signal<ScrollBehavior>,
}
impl Default for UseScrollOptions {
fn default() -> Self {
Self {
throttle: 0.0,
idle: 200.0,
offset: ScrollOffset::default(),
on_scroll: Rc::new(|_| {}),
on_stop: Rc::new(|_| {}),
event_listener_options: Default::default(),
behavior: Default::default(),
}
}
}
#[derive(Default, Copy, Clone)]
pub enum ScrollBehavior {
#[default]
Auto,
Smooth,
}
impl From<ScrollBehavior> for web_sys::ScrollBehavior {
fn from(val: ScrollBehavior) -> Self {
match val {
ScrollBehavior::Auto => web_sys::ScrollBehavior::Auto,
ScrollBehavior::Smooth => web_sys::ScrollBehavior::Smooth,
}
}
}
#[derive(Default, Copy, Clone, Debug)]
pub struct ScrollOffset {
pub left: f64,
pub top: f64,
pub right: f64,
pub bottom: f64,
}
impl ScrollOffset {
pub fn set_direction(mut self, direction: Direction, value: f64) -> Self {
match direction {
Direction::Top => self.top = value,
Direction::Bottom => self.bottom = value,
Direction::Left => self.left = value,
Direction::Right => self.right = value,
}
self
}
}