diff --git a/CHANGELOG.md b/CHANGELOG.md
index 65d75ca..c37f1ad 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,13 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+## [0.7.2] - 2023-10-21
+
+### Fixes 🍕
+
+- Some functions still used `window()` which could lead to panics in SSR. This is now fixed.
+ Specifically for `use_draggable`.
+
## [0.7.1] - 2023-10-02
### New Function 🚀
diff --git a/Cargo.toml b/Cargo.toml
index 2747c15..ee22cf8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "leptos-use"
-version = "0.7.1"
+version = "0.7.2"
edition = "2021"
authors = ["Marc-Stefan Cassola"]
categories = ["gui", "web-programming"]
diff --git a/examples/ssr/Cargo.toml b/examples/ssr/Cargo.toml
index 7b30d87..5010f37 100644
--- a/examples/ssr/Cargo.toml
+++ b/examples/ssr/Cargo.toml
@@ -1,5 +1,5 @@
[package]
-name = "start-axum"
+name = "leptos-use-ssr"
version = "0.1.0"
edition = "2021"
diff --git a/examples/ssr/src/main.rs b/examples/ssr/src/main.rs
index 6b92404..c08d00e 100644
--- a/examples/ssr/src/main.rs
+++ b/examples/ssr/src/main.rs
@@ -5,8 +5,8 @@ async fn main() {
use leptos::logging::log;
use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes};
- use start_axum::app::*;
- use start_axum::fileserv::file_and_error_handler;
+ use leptos_use_ssr::app::*;
+ use leptos_use_ssr::fileserv::file_and_error_handler;
simple_logger::init_with_level(log::Level::Debug).expect("couldn't initialize logging");
@@ -18,7 +18,7 @@ async fn main() {
let conf = get_configuration(None).await.unwrap();
let leptos_options = conf.leptos_options;
let addr = leptos_options.site_addr;
- let routes = generate_route_list(|| view! { }).await;
+ let routes = generate_route_list(|| view! { });
// build our application with a route
let app = Router::new()
diff --git a/examples/use_draggable/src/main.rs b/examples/use_draggable/src/main.rs
index cf92cdb..9ed1bfb 100644
--- a/examples/use_draggable/src/main.rs
+++ b/examples/use_draggable/src/main.rs
@@ -2,13 +2,16 @@ use leptos::html::Div;
use leptos::*;
use leptos_use::core::Position;
use leptos_use::docs::demo_or_body;
-use leptos_use::{use_draggable_with_options, UseDraggableOptions, UseDraggableReturn};
+use leptos_use::{use_draggable_with_options, use_window, UseDraggableOptions, UseDraggableReturn};
#[component]
fn Demo() -> impl IntoView {
let el = create_node_ref::
();
- let inner_width = window().inner_width().unwrap().as_f64().unwrap();
+ let inner_width = use_window()
+ .as_ref()
+ .map(|w| w.inner_width().unwrap().as_f64().unwrap())
+ .unwrap_or(0.0);
let UseDraggableReturn { x, y, style, .. } = use_draggable_with_options(
el,
diff --git a/examples/use_event_listener/src/main.rs b/examples/use_event_listener/src/main.rs
index eb2b22d..76c2f62 100644
--- a/examples/use_event_listener/src/main.rs
+++ b/examples/use_event_listener/src/main.rs
@@ -2,11 +2,11 @@ use leptos::ev::{click, keydown};
use leptos::html::A;
use leptos::logging::log;
use leptos::*;
-use leptos_use::use_event_listener;
+use leptos_use::{use_event_listener, use_window};
#[component]
fn Demo() -> impl IntoView {
- let _ = use_event_listener(window(), keydown, |evt| {
+ let _ = use_event_listener(use_window(), keydown, |evt| {
log!("window keydown: '{}'", evt.key());
});
diff --git a/src/core/mod.rs b/src/core/mod.rs
index 2a8c2e0..ea1f1b7 100644
--- a/src/core/mod.rs
+++ b/src/core/mod.rs
@@ -5,6 +5,7 @@ mod maybe_rw_signal;
mod pointer_type;
mod position;
mod size;
+mod ssr_safe_method;
mod storage;
pub use connection_ready_state::*;
@@ -14,4 +15,5 @@ pub use maybe_rw_signal::*;
pub use pointer_type::*;
pub use position::*;
pub use size::*;
+pub(crate) use ssr_safe_method::*;
pub use storage::*;
diff --git a/src/core/ssr_safe_method.rs b/src/core/ssr_safe_method.rs
new file mode 100644
index 0000000..9ffa513
--- /dev/null
+++ b/src/core/ssr_safe_method.rs
@@ -0,0 +1,19 @@
+macro_rules! impl_ssr_safe_method {
+ (
+ $(#[$attr:meta])*
+ $method:ident(&self$(, $p_name:ident: $p_ty:ty)*) -> $return_ty:ty
+ $(; $($post_fix:tt)+)?
+ ) => {
+ $(#[$attr])*
+ #[inline(always)]
+ pub fn $method(&self, $($p_name: $p_ty),*) -> $return_ty {
+ self.0.as_ref()
+ .map(
+ |w| w.$method($($p_name),*)
+ )
+ $($($post_fix)+)?
+ }
+ };
+}
+
+pub(crate) use impl_ssr_safe_method;
diff --git a/src/use_active_element.rs b/src/use_active_element.rs
index 4a671b0..76da95a 100644
--- a/src/use_active_element.rs
+++ b/src/use_active_element.rs
@@ -1,6 +1,6 @@
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
-use crate::{use_document, use_event_listener_with_options, UseEventListenerOptions};
+use crate::{use_document, use_event_listener_with_options, use_window, UseEventListenerOptions};
use leptos::ev::{blur, focus};
use leptos::html::{AnyElement, ToHtmlElement};
use leptos::*;
@@ -45,7 +45,7 @@ pub fn use_active_element() -> Signal