added ssr to use_element_bounding

This commit is contained in:
Maccesch 2024-01-03 21:11:28 +00:00
parent d9ff05afea
commit 34c913caae
2 changed files with 100 additions and 77 deletions

View file

@ -1,6 +1,5 @@
use crate::{use_event_listener_with_options, UseEventListenerOptions};
use cfg_if::cfg_if;
use leptos::{ev::change, *};
use leptos::*;
/// Reactive [`window.devicePixelRatio`](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio)
///
@ -33,6 +32,9 @@ pub fn use_device_pixel_ratio() -> Signal<f64> {
cfg_if! { if #[cfg(feature = "ssr")] {
let pixel_ratio = Signal::derive(|| 1.0);
} else {
use crate::{use_event_listener_with_options, UseEventListenerOptions};
use leptos::ev::change;
let initial_pixel_ratio = window().device_pixel_ratio();
let (pixel_ratio, set_pixel_ratio) = create_signal(initial_pixel_ratio);

View file

@ -1,9 +1,6 @@
use crate::core::ElementMaybeSignal;
use crate::{
use_event_listener_with_options, use_resize_observer, use_window, UseEventListenerOptions,
};
use cfg_if::cfg_if;
use default_struct_builder::DefaultBuilder;
use leptos::ev::{resize, scroll};
use leptos::*;
/// Reactive [bounding box](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) of an HTML element
@ -29,6 +26,9 @@ use leptos::*;
/// view! { <div node_ref=el></div> }
/// # }
/// ```
/// ## Server-Side Rendering
///
/// On the server the returned signals always are `0.0` and `update` is a no-op.
pub fn use_element_bounding<El, T>(target: El) -> UseElementBoundingReturn<impl Fn() + Clone>
where
El: Into<ElementMaybeSignal<T, web_sys::Element>> + Clone,
@ -46,13 +46,6 @@ where
El: Into<ElementMaybeSignal<T, web_sys::Element>> + Clone,
T: Into<web_sys::Element> + Clone + 'static,
{
let UseElementBoundingOptions {
reset,
window_resize,
window_scroll,
immediate,
} = options;
let (height, set_height) = create_signal(0.0);
let (width, set_width) = create_signal(0.0);
let (left, set_left) = create_signal(0.0);
@ -62,6 +55,33 @@ where
let (x, set_x) = create_signal(0.0);
let (y, set_y) = create_signal(0.0);
cfg_if! { if #[cfg(feature = "ssr")] {
let _ = target;
let _ = options;
let _ = set_height;
let _ = set_width;
let _ = set_left;
let _ = set_right;
let _ = set_top;
let _ = set_bottom;
let _ = set_x;
let _ = set_y;
let update = move || ();
} else {
use leptos::ev::{resize, scroll};
use crate::{
use_event_listener_with_options, use_resize_observer, use_window, UseEventListenerOptions,
};
let UseElementBoundingOptions {
reset,
window_resize,
window_scroll,
immediate,
} = options;
let target = target.into();
let update = {
@ -142,6 +162,7 @@ where
if immediate {
update();
}
}}
UseElementBoundingReturn {
height: height.into(),