made use_scroll usable on the server

This commit is contained in:
Maccesch 2023-08-16 16:23:29 +01:00
parent 036fe98c34
commit cd34a9f3e6
3 changed files with 212 additions and 204 deletions

View file

@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `signal_debounced` - `signal_debounced`
- `signal_throttled` - `signal_throttled`
### Braking Changes 🛠 ### Breaking Changes 🛠
- Leptos version is now 0.5 - Leptos version is now 0.5
- No `cx: Scope` params are supported/needed anymore because of the changes in Leptos. - No `cx: Scope` params are supported/needed anymore because of the changes in Leptos.
@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Callbacks in options don't require to be cloneable anymore - Callbacks in options don't require to be cloneable anymore
- Callback in `use_raf_fn` doesn't require to be cloneable anymore - Callback in `use_raf_fn` doesn't require to be cloneable anymore
- `use_scroll` is now callable on the server
### Fixes 🍕 ### Fixes 🍕
@ -69,7 +70,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `is_some` - `is_some`
- `use_raf_fn` - `use_raf_fn`
### Braking Changes 🛠 ### Breaking Changes 🛠
- The following functions now accept a `MaybeRwSignal` as their initial/default value which means - The following functions now accept a `MaybeRwSignal` as their initial/default value which means
you can use a synchronized `RwSignal` in those places. you can use a synchronized `RwSignal` in those places.
@ -112,7 +113,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.4.0] - 2023-07-03 ## [0.4.0] - 2023-07-03
### Braking Changes 🛠 ### Breaking Changes 🛠
- Required `leptos` version is now 0.4 - Required `leptos` version is now 0.4
- Following the changes in `leptos` there is no longer a `stable` crate feature required in order to use this library - Following the changes in `leptos` there is no longer a `stable` crate feature required in order to use this library
@ -149,7 +150,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.3.0] - 2023-06-13 ## [0.3.0] - 2023-06-13
### Braking Changes 🛠 ### Breaking Changes 🛠
- `use_event_listener` no longer returns a `Box<dyn Fn()>` but a `impl Fn() + Clone` - `use_event_listener` no longer returns a `Box<dyn Fn()>` but a `impl Fn() + Clone`
@ -179,7 +180,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.2.0] - 2023-06-11 ## [0.2.0] - 2023-06-11
### Braking Changes ### Breaking Changes
- `watch` doesn't accept `immediate` as a direct argument anymore. This is only provided by the option variant. - `watch` doesn't accept `immediate` as a direct argument anymore. This is only provided by the option variant.
- `watch` has now variant `watch_with_options` which allows for debouncing and throttling. - `watch` has now variant `watch_with_options` which allows for debouncing and throttling.

View file

@ -76,7 +76,7 @@ pub fn use_raf_fn_with_options(
let loop_fn = { let loop_fn = {
let request_next_frame = request_next_frame.clone(); let request_next_frame = request_next_frame.clone();
let mut previous_frame_timestamp = Cell::new(0.0_f64); let previous_frame_timestamp = Cell::new(0.0_f64);
move |timestamp: f64| { move |timestamp: f64| {
if !is_active.get() { if !is_active.get() {

View file

@ -1,6 +1,7 @@
use crate::core::ElementMaybeSignal; use crate::core::ElementMaybeSignal;
use crate::use_event_listener::use_event_listener_with_options; use crate::use_event_listener::use_event_listener_with_options;
use crate::{use_debounce_fn_with_arg, use_throttle_fn_with_arg_and_options, ThrottleOptions}; use crate::{use_debounce_fn_with_arg, use_throttle_fn_with_arg_and_options, ThrottleOptions};
use cfg_if::cfg_if;
use default_struct_builder::DefaultBuilder; use default_struct_builder::DefaultBuilder;
use leptos::ev::EventDescriptor; use leptos::ev::EventDescriptor;
use leptos::*; use leptos::*;
@ -161,7 +162,7 @@ use wasm_bindgen::JsCast;
/// ///
/// ## Server-Side Rendering /// ## Server-Side Rendering
/// ///
/// Please refer to ["Functions with Target Elements"](https://leptos-use.rs/server_side_rendering.html#functions-with-target-elements) /// On the server this returns signals that don't change and setters that are noops.
pub fn use_scroll<El, T>(element: El) -> UseScrollReturn pub fn use_scroll<El, T>(element: El) -> UseScrollReturn
where where
El: Clone, El: Clone,
@ -182,7 +183,27 @@ where
let (internal_x, set_internal_x) = create_signal(0.0); let (internal_x, set_internal_x) = create_signal(0.0);
let (internal_y, set_internal_y) = create_signal(0.0); let (internal_y, set_internal_y) = create_signal(0.0);
let signal = (element).into(); let (is_scrolling, set_is_scrolling) = create_signal(false);
let arrived_state = create_rw_signal(Directions {
left: true,
right: false,
top: true,
bottom: false,
});
let directions = create_rw_signal(Directions {
left: false,
right: false,
top: false,
bottom: false,
});
cfg_if! { if #[cfg(feature = "ssr")] {
let set_x = Box::new(|_| {});
let set_y = Box::new(|_| {});
let measure = Box::new(|| {});
} else {
let signal = element.into();
let behavior = options.behavior; let behavior = options.behavior;
let scroll_to = { let scroll_to = {
@ -216,21 +237,6 @@ where
let set_y = Box::new(move |y| scroll_to(None, Some(y))); let set_y = Box::new(move |y| scroll_to(None, Some(y)));
let (is_scrolling, set_is_scrolling) = create_signal(false);
let arrived_state = create_rw_signal(Directions {
left: true,
right: false,
top: true,
bottom: false,
});
let directions = create_rw_signal(Directions {
left: false,
right: false,
top: false,
bottom: false,
});
let on_scroll_end = { let on_scroll_end = {
let on_stop = Rc::clone(&options.on_stop); let on_stop = Rc::clone(&options.on_stop);
@ -407,6 +413,7 @@ where
set_arrived_state(el); set_arrived_state(el);
} }
}); });
}}
UseScrollReturn { UseScrollReturn {
x: internal_x.into(), x: internal_x.into(),