mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-22 16:49:22 -05:00
parent
0aff004250
commit
dfa30e4aef
23 changed files with 924 additions and 759 deletions
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="Python 3.9" />
|
||||||
|
</component>
|
||||||
|
</project>
|
48
CHANGELOG.md
48
CHANGELOG.md
|
@ -3,7 +3,7 @@
|
||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
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).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased] -
|
## [Unreleased] -
|
||||||
|
|
||||||
### New Functions 🚀
|
### New Functions 🚀
|
||||||
|
|
||||||
|
@ -20,17 +20,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
Please check the release notes of Leptos 0.5 for how to upgrade.
|
Please check the release notes of Leptos 0.5 for how to upgrade.
|
||||||
- `watch` is now deprecated in favor of `leptos::watch` and will be removed in a future release.
|
- `watch` is now deprecated in favor of `leptos::watch` and will be removed in a future release.
|
||||||
`watch_with_options` will continue to exist.
|
`watch_with_options` will continue to exist.
|
||||||
- `use_event_listener_with_options` now takes a `UseEventListenerOptions` instead of a `web_sys::AddEventListenerOptions`.
|
- `use_event_listener_with_options` now takes a `UseEventListenerOptions` instead of
|
||||||
|
a `web_sys::AddEventListenerOptions`.
|
||||||
|
- `use_mutation_observer_with_options` now takes a `UseMutationObserverOptions` instead of
|
||||||
|
a `web_sys::MutationObserverInit`.
|
||||||
- `use_websocket`:
|
- `use_websocket`:
|
||||||
- takes now a `&str` instead of a `String` as its `url` parameter.
|
- takes now a `&str` instead of a `String` as its `url` parameter.
|
||||||
- The `ready_state` return type is now renamed to `ConnectionReadyState` instead of `UseWebSocketReadyState`.
|
- The `ready_state` return type is now renamed to `ConnectionReadyState` instead of `UseWebSocketReadyState`.
|
||||||
- The returned signals `ready_state`, `message`, `message_bytes` have now the type
|
- The returned signals `ready_state`, `message`, `message_bytes` have now the type
|
||||||
`Signal<...>` instead of `ReadSignal<...>` to make them more consistent with other functions.
|
`Signal<...>` instead of `ReadSignal<...>` to make them more consistent with other functions.
|
||||||
- The options `reconnect_limit` and `reconnect_interval` now take a `u64` instead of `Option<u64>` to improve DX.
|
- The options `reconnect_limit` and `reconnect_interval` now take a `u64` instead of `Option<u64>` to improve DX.
|
||||||
- The option `manual` has been renamed to `immediate` to make it more consistent with other functions.
|
- The option `manual` has been renamed to `immediate` to make it more consistent with other functions.
|
||||||
To port please note that `immediate` is the inverse of `manual` (`immediate` = `!manual`).
|
To port please note that `immediate` is the inverse of `manual` (`immediate` = `!manual`).
|
||||||
- `use_color_mode`:
|
- `use_color_mode`:
|
||||||
- The optional `on_changed` handler parameters have changed slightly. Please refer to the docs for more details.
|
- The optional `on_changed` handler parameters have changed slightly. Please refer to the docs for more details.
|
||||||
- Throttled or debounced functions cannot be `FnOnce` anymore.
|
- Throttled or debounced functions cannot be `FnOnce` anymore.
|
||||||
- All traits `ClonableFn...` have been removed.
|
- All traits `ClonableFn...` have been removed.
|
||||||
|
|
||||||
|
@ -38,9 +41,18 @@ 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
|
- All (!) functions can now be safely called on the server. Specifically this includes the following that
|
||||||
- `use_event_listener` can now be called safely on the server.
|
- panicked on the server:
|
||||||
|
- `use_scroll`
|
||||||
|
- `use_event_listener`
|
||||||
|
- `use_element_hover`
|
||||||
|
- `on_click_outside`
|
||||||
|
- `use_drop_zone`
|
||||||
|
- `use_element_size`
|
||||||
|
- `use_element_visibility`
|
||||||
|
- `use_resize_observer`
|
||||||
|
- `use_intersection_observer`
|
||||||
|
- `use_mutation_observer`
|
||||||
|
|
||||||
### Fixes 🍕
|
### Fixes 🍕
|
||||||
|
|
||||||
|
@ -87,11 +99,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- `use_local_storage`
|
- `use_local_storage`
|
||||||
- `use_session_storage`
|
- `use_session_storage`
|
||||||
- Instead of returning `ReadSignal`, the following functions now return `Signal`.
|
- Instead of returning `ReadSignal`, the following functions now return `Signal`.
|
||||||
- `use_color_mode`
|
- `use_color_mode`
|
||||||
- `use_favicon`
|
- `use_favicon`
|
||||||
- `use_storage`
|
- `use_storage`
|
||||||
- `use_local_storage`
|
- `use_local_storage`
|
||||||
- `use_session_storage`
|
- `use_session_storage`
|
||||||
|
|
||||||
### Fixes 🍕
|
### Fixes 🍕
|
||||||
|
|
||||||
|
|
|
@ -32,18 +32,23 @@ and the server.
|
||||||
|
|
||||||
## Functions with Target Elements
|
## Functions with Target Elements
|
||||||
|
|
||||||
A lot of functions like `use_event_listener` and `use_element_size` are only useful when a target HTML/SVG element is
|
A lot of functions like `use_resize_observer` and `use_element_size` are only useful when a target HTML/SVG element is
|
||||||
available. This is not the case on the server. You can simply wrap them in `create_effect` which will cause them to
|
available. This is not always the case on the server. If you use them with `NodeRefs` they will just work in SSR.
|
||||||
be only called in the browser.
|
But what if you want to use them with `window()` or `document()`?
|
||||||
|
|
||||||
|
To enable that we provide the helper functions [`use_window()`](elements/use_window.md) and [`use_document()`](elements/use_document.md) which return
|
||||||
|
a new-type-wrapped `Option<web_sys::Window>` or `Option<web_sys::Document>` respectively. These can be
|
||||||
|
used safely on the server. The following code works on both the client and the server:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
create_effect(
|
use leptos::*;
|
||||||
cx,
|
use leptos::ev::keyup;
|
||||||
move |_| {
|
use leptos_use::{use_event_listener, use_window};
|
||||||
// window() doesn't work on the server
|
|
||||||
use_event_listener(window(), "resize", move |_| {
|
use_event_listener(use_window(), keyup, |evt| {
|
||||||
// ...
|
...
|
||||||
})
|
});
|
||||||
},
|
```
|
||||||
);
|
|
||||||
```
|
There are some convenience methods provided as well, like `use_document().body()` which
|
||||||
|
just propagate a `None` on the server.
|
|
@ -13,7 +13,6 @@ members = [
|
||||||
"use_css_var",
|
"use_css_var",
|
||||||
"use_cycle_list",
|
"use_cycle_list",
|
||||||
"use_debounce_fn",
|
"use_debounce_fn",
|
||||||
"use_document",
|
|
||||||
"use_document_visibility",
|
"use_document_visibility",
|
||||||
"use_draggable",
|
"use_draggable",
|
||||||
"use_drop_zone",
|
"use_drop_zone",
|
||||||
|
@ -38,7 +37,6 @@ members = [
|
||||||
"use_storage",
|
"use_storage",
|
||||||
"use_throttle_fn",
|
"use_throttle_fn",
|
||||||
"use_websocket",
|
"use_websocket",
|
||||||
"use_window",
|
|
||||||
"use_window_focus",
|
"use_window_focus",
|
||||||
"use_window_scroll",
|
"use_window_scroll",
|
||||||
"watch_debounced",
|
"watch_debounced",
|
||||||
|
|
|
@ -11,16 +11,16 @@ axum = { version = "0.6.4", optional = true }
|
||||||
console_error_panic_hook = "0.1"
|
console_error_panic_hook = "0.1"
|
||||||
console_log = "1"
|
console_log = "1"
|
||||||
cfg-if = "1"
|
cfg-if = "1"
|
||||||
leptos = { version = "0.5.0-beta2", features = ["nightly"] }
|
leptos = { version = "0.5.0-rc1", features = ["nightly"] }
|
||||||
leptos_axum = { version = "0.5.0-beta2", optional = true }
|
leptos_axum = { version = "0.5.0-rc1", optional = true }
|
||||||
leptos_meta = { version = "0.5.0-beta2", features = ["nightly"] }
|
leptos_meta = { version = "0.5.0-rc1", features = ["nightly"] }
|
||||||
leptos_router = { version = "0.5.0-beta2", features = ["nightly"] }
|
leptos_router = { version = "0.5.0-rc1", features = ["nightly"] }
|
||||||
leptos-use = { path = "../..", features = ["storage"] }
|
leptos-use = { path = "../..", features = ["storage"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
simple_logger = "4"
|
simple_logger = "4"
|
||||||
tokio = { version = "1.25.0", optional = true }
|
tokio = { version = "1.25.0", optional = true }
|
||||||
tower = { version = "0.4.13", optional = true }
|
tower = { version = "0.4.13", optional = true }
|
||||||
tower-http = { version = "0.5.0-beta2", features = ["fs"], optional = true }
|
tower-http = { version = "0.4.3", features = ["fs"], optional = true }
|
||||||
wasm-bindgen = "=0.2.87"
|
wasm-bindgen = "=0.2.87"
|
||||||
thiserror = "1.0.38"
|
thiserror = "1.0.38"
|
||||||
tracing = { version = "0.1.37", optional = true }
|
tracing = { version = "0.1.37", optional = true }
|
||||||
|
|
|
@ -5,7 +5,8 @@ use leptos_meta::*;
|
||||||
use leptos_router::*;
|
use leptos_router::*;
|
||||||
use leptos_use::storage::use_local_storage;
|
use leptos_use::storage::use_local_storage;
|
||||||
use leptos_use::{
|
use leptos_use::{
|
||||||
use_debounce_fn, use_event_listener, use_intl_number_format, UseIntlNumberFormatOptions,
|
use_debounce_fn, use_event_listener, use_intl_number_format, use_window,
|
||||||
|
UseIntlNumberFormatOptions,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
|
@ -47,11 +48,9 @@ fn HomePage() -> impl IntoView {
|
||||||
|
|
||||||
let (key, set_key) = create_signal("".to_string());
|
let (key, set_key) = create_signal("".to_string());
|
||||||
|
|
||||||
create_effect(move |_| {
|
// window() doesn't work on the server so we provide use_window()
|
||||||
// window() doesn't work on the server
|
let _ = use_event_listener(use_window(), keypress, move |evt: KeyboardEvent| {
|
||||||
let _ = use_event_listener(window(), keypress, move |evt: KeyboardEvent| {
|
set_key(evt.key())
|
||||||
set_key(evt.key())
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let (debounce_value, set_debounce_value) = create_signal("not called");
|
let (debounce_value, set_debounce_value) = create_signal("not called");
|
||||||
|
@ -65,10 +64,10 @@ fn HomePage() -> impl IntoView {
|
||||||
debounced_fn();
|
debounced_fn();
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<h1>"Leptos-Use SSR Example"</h1>
|
<h1>Leptos-Use SSR Example</h1>
|
||||||
<button on:click=on_click>"Click Me: " {count}</button>
|
<button on:click=on_click>Click Me: {count}</button>
|
||||||
<p>"Locale "zh-Hans-CN-u-nu-hanidec": " {zh_count}</p>
|
<p>Locale zh-Hans-CN-u-nu-hanidec: {zh_count}</p>
|
||||||
<p>"Press any key: " {key}</p>
|
<p>Press any key: {key}</p>
|
||||||
<p>"Debounced called: " {debounce_value}</p>
|
<p>Debounced called: {debounce_value}</p>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use leptos::html::Div;
|
use leptos::html::Div;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use leptos_use::docs::demo_or_body;
|
use leptos_use::docs::demo_or_body;
|
||||||
use leptos_use::use_mutation_observer_with_options;
|
use leptos_use::{use_mutation_observer_with_options, UseMutationObserverOptions};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
|
@ -11,9 +11,6 @@ fn Demo() -> impl IntoView {
|
||||||
let (class_name, set_class_name) = create_signal(String::new());
|
let (class_name, set_class_name) = create_signal(String::new());
|
||||||
let (style, set_style) = create_signal(String::new());
|
let (style, set_style) = create_signal(String::new());
|
||||||
|
|
||||||
let mut init = web_sys::MutationObserverInit::new();
|
|
||||||
init.attributes(true);
|
|
||||||
|
|
||||||
use_mutation_observer_with_options(
|
use_mutation_observer_with_options(
|
||||||
el,
|
el,
|
||||||
move |mutations, _| {
|
move |mutations, _| {
|
||||||
|
@ -23,7 +20,7 @@ fn Demo() -> impl IntoView {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
init,
|
UseMutationObserverOptions::default().attributes(true),
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = set_timeout_with_handle(
|
let _ = set_timeout_with_handle(
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
use crate::core::{ElementMaybeSignal, ElementsMaybeSignal};
|
use crate::core::{ElementMaybeSignal, ElementsMaybeSignal};
|
||||||
use crate::utils::IS_IOS;
|
use cfg_if::cfg_if;
|
||||||
use crate::{use_event_listener, use_event_listener_with_options, UseEventListenerOptions};
|
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::ev::{blur, click, pointerdown};
|
|
||||||
use leptos::*;
|
|
||||||
use std::cell::Cell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::sync::RwLock;
|
|
||||||
use std::time::Duration;
|
|
||||||
use wasm_bindgen::JsCast;
|
|
||||||
|
|
||||||
static IOS_WORKAROUND: RwLock<bool> = RwLock::new(false);
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
|
use leptos::*;
|
||||||
|
use crate::utils::IS_IOS;
|
||||||
|
use crate::{use_event_listener, use_event_listener_with_options, UseEventListenerOptions};
|
||||||
|
use leptos::ev::{blur, click, pointerdown};
|
||||||
|
use std::cell::Cell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::sync::RwLock;
|
||||||
|
use std::time::Duration;
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
|
||||||
|
static IOS_WORKAROUND: RwLock<bool> = RwLock::new(false);
|
||||||
|
}}
|
||||||
|
|
||||||
/// Listen for clicks outside of an element.
|
/// Listen for clicks outside of an element.
|
||||||
/// Useful for modals or dropdowns.
|
/// Useful for modals or dropdowns.
|
||||||
|
@ -48,7 +52,7 @@ static IOS_WORKAROUND: RwLock<bool> = RwLock::new(false);
|
||||||
///
|
///
|
||||||
/// ## 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 amounts to a no-op.
|
||||||
pub fn on_click_outside<El, T, F>(target: El, handler: F) -> impl FnOnce() + Clone
|
pub fn on_click_outside<El, T, F>(target: El, handler: F) -> impl FnOnce() + Clone
|
||||||
where
|
where
|
||||||
El: Clone,
|
El: Clone,
|
||||||
|
@ -64,6 +68,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Version of `on_click_outside` that takes an `OnClickOutsideOptions`. See `on_click_outside` for more details.
|
/// Version of `on_click_outside` that takes an `OnClickOutsideOptions`. See `on_click_outside` for more details.
|
||||||
|
#[cfg_attr(feature = "ssr", allow(unused_variables))]
|
||||||
pub fn on_click_outside_with_options<El, T, F, I>(
|
pub fn on_click_outside_with_options<El, T, F, I>(
|
||||||
target: El,
|
target: El,
|
||||||
handler: F,
|
handler: F,
|
||||||
|
@ -76,147 +81,151 @@ where
|
||||||
F: FnMut(web_sys::Event) + Clone + 'static,
|
F: FnMut(web_sys::Event) + Clone + 'static,
|
||||||
I: Into<web_sys::EventTarget> + Clone + 'static,
|
I: Into<web_sys::EventTarget> + Clone + 'static,
|
||||||
{
|
{
|
||||||
let OnClickOutsideOptions {
|
cfg_if! { if #[cfg(feature = "ssr")] {
|
||||||
ignore,
|
|| {}
|
||||||
capture,
|
} else {
|
||||||
detect_iframes,
|
let OnClickOutsideOptions {
|
||||||
} = options;
|
ignore,
|
||||||
|
capture,
|
||||||
|
detect_iframes,
|
||||||
|
} = options;
|
||||||
|
|
||||||
// Fixes: https://github.com/vueuse/vueuse/issues/1520
|
// Fixes: https://github.com/vueuse/vueuse/issues/1520
|
||||||
// How it works: https://stackoverflow.com/a/39712411
|
// How it works: https://stackoverflow.com/a/39712411
|
||||||
if *IS_IOS {
|
if *IS_IOS {
|
||||||
if let Ok(mut ios_workaround) = IOS_WORKAROUND.write() {
|
if let Ok(mut ios_workaround) = IOS_WORKAROUND.write() {
|
||||||
if !*ios_workaround {
|
if !*ios_workaround {
|
||||||
*ios_workaround = true;
|
*ios_workaround = true;
|
||||||
if let Some(body) = document().body() {
|
if let Some(body) = document().body() {
|
||||||
let children = body.children();
|
let children = body.children();
|
||||||
for i in 0..children.length() {
|
for i in 0..children.length() {
|
||||||
let _ = children
|
let _ = children
|
||||||
.get_with_index(i)
|
.get_with_index(i)
|
||||||
.expect("checked index")
|
.expect("checked index")
|
||||||
.add_event_listener_with_callback(
|
.add_event_listener_with_callback(
|
||||||
"click",
|
"click",
|
||||||
&js_sys::Function::default(),
|
&js_sys::Function::default(),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let should_listen = Rc::new(Cell::new(true));
|
let should_listen = Rc::new(Cell::new(true));
|
||||||
|
|
||||||
let should_ignore = move |event: &web_sys::UiEvent| {
|
let should_ignore = move |event: &web_sys::UiEvent| {
|
||||||
let ignore = ignore.get_untracked();
|
let ignore = ignore.get_untracked();
|
||||||
|
|
||||||
ignore.into_iter().flatten().any(|element| {
|
ignore.into_iter().flatten().any(|element| {
|
||||||
let element: web_sys::EventTarget = element.into();
|
let element: web_sys::EventTarget = element.into();
|
||||||
|
|
||||||
event_target::<web_sys::EventTarget>(event) == element
|
event_target::<web_sys::EventTarget>(event) == element
|
||||||
|| event.composed_path().includes(element.as_ref(), 0)
|
|| event.composed_path().includes(element.as_ref(), 0)
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
let target = (target).into();
|
let target = (target).into();
|
||||||
|
|
||||||
let listener = {
|
let listener = {
|
||||||
let should_listen = Rc::clone(&should_listen);
|
let should_listen = Rc::clone(&should_listen);
|
||||||
let mut handler = handler.clone();
|
let mut handler = handler.clone();
|
||||||
let target = target.clone();
|
let target = target.clone();
|
||||||
let should_ignore = should_ignore.clone();
|
let should_ignore = should_ignore.clone();
|
||||||
|
|
||||||
move |event: web_sys::UiEvent| {
|
move |event: web_sys::UiEvent| {
|
||||||
if let Some(el) = target.get_untracked() {
|
|
||||||
let el = el.into();
|
|
||||||
|
|
||||||
if el == event_target(&event) || event.composed_path().includes(el.as_ref(), 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if event.detail() == 0 {
|
|
||||||
should_listen.set(!should_ignore(&event));
|
|
||||||
}
|
|
||||||
|
|
||||||
if !should_listen.get() {
|
|
||||||
should_listen.set(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
handler(event.into());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let remove_click_listener = {
|
|
||||||
let mut listener = listener.clone();
|
|
||||||
|
|
||||||
use_event_listener_with_options::<_, web_sys::Window, _, _>(
|
|
||||||
window(),
|
|
||||||
click,
|
|
||||||
move |event| listener(event.into()),
|
|
||||||
UseEventListenerOptions::default()
|
|
||||||
.passive(true)
|
|
||||||
.capture(capture),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let remove_pointer_listener = {
|
|
||||||
let target = target.clone();
|
|
||||||
let should_listen = Rc::clone(&should_listen);
|
|
||||||
|
|
||||||
use_event_listener_with_options::<_, web_sys::Window, _, _>(
|
|
||||||
window(),
|
|
||||||
pointerdown,
|
|
||||||
move |event| {
|
|
||||||
if let Some(el) = target.get_untracked() {
|
if let Some(el) = target.get_untracked() {
|
||||||
should_listen.set(
|
let el = el.into();
|
||||||
!event.composed_path().includes(el.into().as_ref(), 0)
|
|
||||||
&& !should_ignore(&event),
|
if el == event_target(&event) || event.composed_path().includes(el.as_ref(), 0) {
|
||||||
);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.detail() == 0 {
|
||||||
|
should_listen.set(!should_ignore(&event));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !should_listen.get() {
|
||||||
|
should_listen.set(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
handler(event.into());
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
UseEventListenerOptions::default().passive(true),
|
};
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let remove_blur_listener = if detect_iframes {
|
let remove_click_listener = {
|
||||||
Some(use_event_listener::<_, web_sys::Window, _, _>(
|
let mut listener = listener.clone();
|
||||||
window(),
|
|
||||||
blur,
|
|
||||||
move |event| {
|
|
||||||
let target = target.clone();
|
|
||||||
let mut handler = handler.clone();
|
|
||||||
|
|
||||||
let _ = set_timeout_with_handle(
|
use_event_listener_with_options::<_, web_sys::Window, _, _>(
|
||||||
move || {
|
window(),
|
||||||
if let Some(el) = target.get_untracked() {
|
click,
|
||||||
if let Some(active_element) = document().active_element() {
|
move |event| listener(event.into()),
|
||||||
if active_element.tag_name() == "IFRAME"
|
UseEventListenerOptions::default()
|
||||||
&& !el
|
.passive(true)
|
||||||
.into()
|
.capture(capture),
|
||||||
.unchecked_into::<web_sys::Node>()
|
)
|
||||||
.contains(Some(&active_element.into()))
|
};
|
||||||
{
|
|
||||||
handler(event.into());
|
let remove_pointer_listener = {
|
||||||
|
let target = target.clone();
|
||||||
|
let should_listen = Rc::clone(&should_listen);
|
||||||
|
|
||||||
|
use_event_listener_with_options::<_, web_sys::Window, _, _>(
|
||||||
|
window(),
|
||||||
|
pointerdown,
|
||||||
|
move |event| {
|
||||||
|
if let Some(el) = target.get_untracked() {
|
||||||
|
should_listen.set(
|
||||||
|
!event.composed_path().includes(el.into().as_ref(), 0)
|
||||||
|
&& !should_ignore(&event),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
UseEventListenerOptions::default().passive(true),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let remove_blur_listener = if detect_iframes {
|
||||||
|
Some(use_event_listener::<_, web_sys::Window, _, _>(
|
||||||
|
window(),
|
||||||
|
blur,
|
||||||
|
move |event| {
|
||||||
|
let target = target.clone();
|
||||||
|
let mut handler = handler.clone();
|
||||||
|
|
||||||
|
let _ = set_timeout_with_handle(
|
||||||
|
move || {
|
||||||
|
if let Some(el) = target.get_untracked() {
|
||||||
|
if let Some(active_element) = document().active_element() {
|
||||||
|
if active_element.tag_name() == "IFRAME"
|
||||||
|
&& !el
|
||||||
|
.into()
|
||||||
|
.unchecked_into::<web_sys::Node>()
|
||||||
|
.contains(Some(&active_element.into()))
|
||||||
|
{
|
||||||
|
handler(event.into());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
Duration::ZERO,
|
||||||
Duration::ZERO,
|
);
|
||||||
);
|
},
|
||||||
},
|
))
|
||||||
))
|
} else {
|
||||||
} else {
|
None
|
||||||
None
|
};
|
||||||
};
|
|
||||||
|
|
||||||
move || {
|
move || {
|
||||||
remove_click_listener();
|
remove_click_listener();
|
||||||
remove_pointer_listener();
|
remove_pointer_listener();
|
||||||
if let Some(f) = remove_blur_listener {
|
if let Some(f) = remove_blur_listener {
|
||||||
f();
|
f();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Options for [`on_click_outside_with_options`].
|
/// Options for [`on_click_outside_with_options`].
|
||||||
|
@ -226,6 +235,7 @@ where
|
||||||
T: Into<web_sys::EventTarget> + Clone + 'static,
|
T: Into<web_sys::EventTarget> + Clone + 'static,
|
||||||
{
|
{
|
||||||
/// List of elementss that should not trigger the callback. Defaults to `[]`.
|
/// List of elementss that should not trigger the callback. Defaults to `[]`.
|
||||||
|
#[cfg_attr(feature = "ssr", allow(dead_code))]
|
||||||
ignore: ElementsMaybeSignal<T, web_sys::EventTarget>,
|
ignore: ElementsMaybeSignal<T, web_sys::EventTarget>,
|
||||||
|
|
||||||
/// Use capturing phase for internal event listener. Defaults to `true`.
|
/// Use capturing phase for internal event listener. Defaults to `true`.
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
||||||
|
|
||||||
use crate::use_event_listener_with_options;
|
use crate::{use_document, use_event_listener_with_options, UseEventListenerOptions};
|
||||||
use cfg_if::cfg_if;
|
|
||||||
use leptos::ev::{blur, focus};
|
use leptos::ev::{blur, focus};
|
||||||
use leptos::html::{AnyElement, ToHtmlElement};
|
use leptos::html::{AnyElement, ToHtmlElement};
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use web_sys::AddEventListenerOptions;
|
|
||||||
|
|
||||||
/// Reactive `document.activeElement`
|
/// Reactive `document.activeElement`
|
||||||
///
|
///
|
||||||
|
@ -36,44 +34,37 @@ use web_sys::AddEventListenerOptions;
|
||||||
///
|
///
|
||||||
/// On the server this returns a `Signal` that always contains the value `None`.
|
/// On the server this returns a `Signal` that always contains the value `None`.
|
||||||
pub fn use_active_element() -> Signal<Option<HtmlElement<AnyElement>>> {
|
pub fn use_active_element() -> Signal<Option<HtmlElement<AnyElement>>> {
|
||||||
cfg_if! { if #[cfg(feature = "ssr")] {
|
let get_active_element = move || {
|
||||||
let get_active_element = || { None };
|
use_document()
|
||||||
} else {
|
.active_element()
|
||||||
let get_active_element = move || {
|
.map(|el| el.to_leptos_element())
|
||||||
document()
|
};
|
||||||
.active_element()
|
|
||||||
.map(|el| el.to_leptos_element())
|
|
||||||
};
|
|
||||||
}}
|
|
||||||
|
|
||||||
let (active_element, set_active_element) = create_signal(get_active_element());
|
let (active_element, set_active_element) = create_signal(get_active_element());
|
||||||
|
|
||||||
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
let listener_options = UseEventListenerOptions::default().capture(true);
|
||||||
let mut listener_options = AddEventListenerOptions::new();
|
|
||||||
listener_options.capture(true);
|
|
||||||
|
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
window(),
|
window(),
|
||||||
blur,
|
blur,
|
||||||
move |event| {
|
move |event| {
|
||||||
if event.related_target().is_some() {
|
if event.related_target().is_some() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
set_active_element.update(|el| *el = get_active_element());
|
set_active_element.update(|el| *el = get_active_element());
|
||||||
},
|
},
|
||||||
listener_options.clone(),
|
listener_options,
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
window(),
|
window(),
|
||||||
focus,
|
focus,
|
||||||
move |_| {
|
move |_| {
|
||||||
set_active_element.update(|el| *el = get_active_element());
|
set_active_element.update(|el| *el = get_active_element());
|
||||||
},
|
},
|
||||||
listener_options,
|
listener_options,
|
||||||
);
|
);
|
||||||
}}
|
|
||||||
|
|
||||||
active_element.into()
|
active_element.into()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
||||||
|
|
||||||
use crate::core::ElementMaybeSignal;
|
use crate::core::ElementMaybeSignal;
|
||||||
use crate::{use_mutation_observer_with_options, watch_with_options, WatchOptions};
|
use crate::{
|
||||||
|
use_mutation_observer_with_options, watch_with_options, UseMutationObserverOptions,
|
||||||
|
WatchOptions,
|
||||||
|
};
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use wasm_bindgen::{JsCast, JsValue};
|
use wasm_bindgen::JsCast;
|
||||||
|
|
||||||
/// Manipulate CSS variables.
|
/// Manipulate CSS variables.
|
||||||
///
|
///
|
||||||
|
@ -127,17 +130,14 @@ where
|
||||||
};
|
};
|
||||||
|
|
||||||
if observe {
|
if observe {
|
||||||
let mut init = web_sys::MutationObserverInit::new();
|
|
||||||
let update_css_var = update_css_var.clone();
|
let update_css_var = update_css_var.clone();
|
||||||
let el_signal = el_signal.clone();
|
let el_signal = el_signal.clone();
|
||||||
|
|
||||||
init.attribute_filter(&js_sys::Array::from_iter(
|
|
||||||
vec![JsValue::from_str("style")],
|
|
||||||
));
|
|
||||||
use_mutation_observer_with_options::<ElementMaybeSignal<T, web_sys::Element>, T, _>(
|
use_mutation_observer_with_options::<ElementMaybeSignal<T, web_sys::Element>, T, _>(
|
||||||
el_signal,
|
el_signal,
|
||||||
move |_, _| update_css_var(),
|
move |_, _| update_css_var(),
|
||||||
init,
|
UseMutationObserverOptions::default()
|
||||||
|
.attribute_filter(vec!["style".to_string()]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use leptos::*;
|
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "ssr"))]
|
||||||
|
use leptos::*;
|
||||||
|
|
||||||
/// SSR safe `document()`.
|
/// SSR safe `document()`.
|
||||||
/// This returns just a new-type wrapper around `Option<Document>`.
|
/// This returns just a new-type wrapper around `Option<Document>`.
|
||||||
/// Calling this amounts to `None` on the server and `Some(Document)` on the client.
|
/// Calling this amounts to `None` on the server and `Some(Document)` on the client.
|
||||||
|
@ -44,8 +46,11 @@ impl Deref for UseDocument {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UseDocument {
|
impl UseDocument {
|
||||||
/// Returns `Some(HtmlElement)` in the Browser. `None` otherwise.
|
|
||||||
pub fn body(&self) -> Option<web_sys::HtmlElement> {
|
pub fn body(&self) -> Option<web_sys::HtmlElement> {
|
||||||
self.0.as_ref().and_then(|d| d.body())
|
self.0.as_ref().and_then(|d| d.body())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn active_element(&self) -> Option<web_sys::Element> {
|
||||||
|
self.0.as_ref().and_then(|d| d.active_element())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
use crate::core::ElementMaybeSignal;
|
use crate::core::ElementMaybeSignal;
|
||||||
use crate::use_event_listener;
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::ev::{dragenter, dragleave, dragover, drop};
|
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
|
use crate::use_event_listener;
|
||||||
|
use leptos::ev::{dragenter, dragleave, dragover, drop};
|
||||||
|
}}
|
||||||
|
|
||||||
/// Create a zone where files can be dropped.
|
/// Create a zone where files can be dropped.
|
||||||
///
|
///
|
||||||
/// ## Demo
|
/// ## Demo
|
||||||
|
@ -45,7 +49,8 @@ use std::rc::Rc;
|
||||||
///
|
///
|
||||||
/// ## 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 the returned `file` signal always contains an empty `Vec` and
|
||||||
|
/// `is_over_drop_zone` contains always `false`
|
||||||
pub fn use_drop_zone<El, T>(target: El) -> UseDropZoneReturn
|
pub fn use_drop_zone<El, T>(target: El) -> UseDropZoneReturn
|
||||||
where
|
where
|
||||||
El: Clone,
|
El: Clone,
|
||||||
|
@ -56,6 +61,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Version of [`use_drop_zone`] that takes a `UseDropZoneOptions`. See [`use_drop_zone`] for how to use.
|
/// Version of [`use_drop_zone`] that takes a `UseDropZoneOptions`. See [`use_drop_zone`] for how to use.
|
||||||
|
#[cfg_attr(feature = "ssr", allow(unused_variables))]
|
||||||
pub fn use_drop_zone_with_options<El, T>(
|
pub fn use_drop_zone_with_options<El, T>(
|
||||||
target: El,
|
target: El,
|
||||||
options: UseDropZoneOptions,
|
options: UseDropZoneOptions,
|
||||||
|
@ -65,81 +71,83 @@ where
|
||||||
El: Into<ElementMaybeSignal<T, web_sys::EventTarget>>,
|
El: Into<ElementMaybeSignal<T, web_sys::EventTarget>>,
|
||||||
T: Into<web_sys::EventTarget> + Clone + 'static,
|
T: Into<web_sys::EventTarget> + Clone + 'static,
|
||||||
{
|
{
|
||||||
let UseDropZoneOptions {
|
|
||||||
on_drop,
|
|
||||||
on_enter,
|
|
||||||
on_leave,
|
|
||||||
on_over,
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
let (is_over_drop_zone, set_over_drop_zone) = create_signal(false);
|
let (is_over_drop_zone, set_over_drop_zone) = create_signal(false);
|
||||||
let (files, set_files) = create_signal(Vec::<web_sys::File>::new());
|
let (files, set_files) = create_signal(Vec::<web_sys::File>::new());
|
||||||
|
|
||||||
let counter = store_value(0_usize);
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
|
let UseDropZoneOptions {
|
||||||
|
on_drop,
|
||||||
|
on_enter,
|
||||||
|
on_leave,
|
||||||
|
on_over,
|
||||||
|
} = options;
|
||||||
|
|
||||||
let update_files = move |event: &web_sys::DragEvent| {
|
let counter = store_value(0_usize);
|
||||||
if let Some(data_transfer) = event.data_transfer() {
|
|
||||||
let files: Vec<web_sys::File> = data_transfer
|
|
||||||
.files()
|
|
||||||
.map(|f| js_sys::Array::from(&f).to_vec())
|
|
||||||
.unwrap_or_default()
|
|
||||||
.into_iter()
|
|
||||||
.map(web_sys::File::from)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
set_files.update(move |f| *f = files);
|
let update_files = move |event: &web_sys::DragEvent| {
|
||||||
}
|
if let Some(data_transfer) = event.data_transfer() {
|
||||||
};
|
let files: Vec<web_sys::File> = data_transfer
|
||||||
|
.files()
|
||||||
|
.map(|f| js_sys::Array::from(&f).to_vec())
|
||||||
|
.unwrap_or_default()
|
||||||
|
.into_iter()
|
||||||
|
.map(web_sys::File::from)
|
||||||
|
.collect();
|
||||||
|
|
||||||
let _ = use_event_listener(target.clone(), dragenter, move |event| {
|
set_files.update(move |f| *f = files);
|
||||||
event.prevent_default();
|
}
|
||||||
counter.update_value(|counter| *counter += 1);
|
};
|
||||||
set_over_drop_zone.set(true);
|
|
||||||
|
|
||||||
update_files(&event);
|
let _ = use_event_listener(target.clone(), dragenter, move |event| {
|
||||||
|
event.prevent_default();
|
||||||
|
counter.update_value(|counter| *counter += 1);
|
||||||
|
set_over_drop_zone.set(true);
|
||||||
|
|
||||||
on_enter(UseDropZoneEvent {
|
update_files(&event);
|
||||||
files: files.get_untracked(),
|
|
||||||
event,
|
on_enter(UseDropZoneEvent {
|
||||||
|
files: files.get_untracked(),
|
||||||
|
event,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
let _ = use_event_listener(target.clone(), dragover, move |event| {
|
let _ = use_event_listener(target.clone(), dragover, move |event| {
|
||||||
event.prevent_default();
|
event.prevent_default();
|
||||||
update_files(&event);
|
update_files(&event);
|
||||||
on_over(UseDropZoneEvent {
|
on_over(UseDropZoneEvent {
|
||||||
files: files.get_untracked(),
|
files: files.get_untracked(),
|
||||||
event,
|
event,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
let _ = use_event_listener(target.clone(), dragleave, move |event| {
|
let _ = use_event_listener(target.clone(), dragleave, move |event| {
|
||||||
event.prevent_default();
|
event.prevent_default();
|
||||||
counter.update_value(|counter| *counter -= 1);
|
counter.update_value(|counter| *counter -= 1);
|
||||||
if counter.get_value() == 0 {
|
if counter.get_value() == 0 {
|
||||||
|
set_over_drop_zone.set(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
update_files(&event);
|
||||||
|
|
||||||
|
on_leave(UseDropZoneEvent {
|
||||||
|
files: files.get_untracked(),
|
||||||
|
event,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
let _ = use_event_listener(target, drop, move |event| {
|
||||||
|
event.prevent_default();
|
||||||
|
counter.update_value(|counter| *counter = 0);
|
||||||
set_over_drop_zone.set(false);
|
set_over_drop_zone.set(false);
|
||||||
}
|
|
||||||
|
|
||||||
update_files(&event);
|
update_files(&event);
|
||||||
|
|
||||||
on_leave(UseDropZoneEvent {
|
on_drop(UseDropZoneEvent {
|
||||||
files: files.get_untracked(),
|
files: files.get_untracked(),
|
||||||
event,
|
event,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}}
|
||||||
|
|
||||||
let _ = use_event_listener(target, drop, move |event| {
|
|
||||||
event.prevent_default();
|
|
||||||
counter.update_value(|counter| *counter = 0);
|
|
||||||
set_over_drop_zone.set(false);
|
|
||||||
|
|
||||||
update_files(&event);
|
|
||||||
|
|
||||||
on_drop(UseDropZoneEvent {
|
|
||||||
files: files.get_untracked(),
|
|
||||||
event,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
UseDropZoneReturn {
|
UseDropZoneReturn {
|
||||||
files: files.into(),
|
files: files.into(),
|
||||||
|
@ -149,6 +157,7 @@ where
|
||||||
|
|
||||||
/// Options for [`use_drop_zone_with_options`].
|
/// Options for [`use_drop_zone_with_options`].
|
||||||
#[derive(DefaultBuilder, Clone)]
|
#[derive(DefaultBuilder, Clone)]
|
||||||
|
#[cfg_attr(feature = "ssr", allow(dead_code))]
|
||||||
pub struct UseDropZoneOptions {
|
pub struct UseDropZoneOptions {
|
||||||
/// Event handler for the [`drop`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/drop_event) event
|
/// Event handler for the [`drop`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/drop_event) event
|
||||||
on_drop: Rc<dyn Fn(UseDropZoneEvent)>,
|
on_drop: Rc<dyn Fn(UseDropZoneEvent)>,
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
use crate::core::ElementMaybeSignal;
|
use crate::core::ElementMaybeSignal;
|
||||||
use crate::{use_event_listener_with_options, UseEventListenerOptions};
|
use crate::{use_event_listener_with_options, UseEventListenerOptions};
|
||||||
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::ev::{mouseenter, mouseleave};
|
use leptos::ev::{mouseenter, mouseleave};
|
||||||
use leptos::leptos_dom::helpers::TimeoutHandle;
|
use leptos::leptos_dom::helpers::TimeoutHandle;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::time::Duration;
|
|
||||||
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
|
use std::time::Duration;
|
||||||
|
}}
|
||||||
|
|
||||||
/// Reactive element's hover state.
|
/// Reactive element's hover state.
|
||||||
///
|
///
|
||||||
|
@ -32,7 +36,7 @@ use std::time::Duration;
|
||||||
///
|
///
|
||||||
/// ## 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 a `Signal` that always contains the value `false`.
|
||||||
pub fn use_element_hover<El, T>(el: El) -> Signal<bool>
|
pub fn use_element_hover<El, T>(el: El) -> Signal<bool>
|
||||||
where
|
where
|
||||||
El: Clone,
|
El: Clone,
|
||||||
|
@ -44,6 +48,7 @@ where
|
||||||
|
|
||||||
/// Version of [`use_element_hover`] that takes a `UseElementHoverOptions`. See [`use_element_hover`] for how to use.
|
/// Version of [`use_element_hover`] that takes a `UseElementHoverOptions`. See [`use_element_hover`] for how to use.
|
||||||
|
|
||||||
|
#[cfg_attr(feature = "ssr", allow(unused_variables, unused_mut))]
|
||||||
pub fn use_element_hover_with_options<El, T>(
|
pub fn use_element_hover_with_options<El, T>(
|
||||||
el: El,
|
el: El,
|
||||||
options: UseElementHoverOptions,
|
options: UseElementHoverOptions,
|
||||||
|
@ -63,24 +68,26 @@ where
|
||||||
let mut timer: Option<TimeoutHandle> = None;
|
let mut timer: Option<TimeoutHandle> = None;
|
||||||
|
|
||||||
let mut toggle = move |entering: bool| {
|
let mut toggle = move |entering: bool| {
|
||||||
let delay = if entering { delay_enter } else { delay_leave };
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
|
let delay = if entering { delay_enter } else { delay_leave };
|
||||||
|
|
||||||
if let Some(handle) = timer.take() {
|
if let Some(handle) = timer.take() {
|
||||||
handle.clear();
|
handle.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
if delay > 0 {
|
if delay > 0 {
|
||||||
timer = set_timeout_with_handle(
|
timer = set_timeout_with_handle(
|
||||||
move || set_hovered.set(entering),
|
move || set_hovered.set(entering),
|
||||||
Duration::from_millis(delay),
|
Duration::from_millis(delay),
|
||||||
)
|
)
|
||||||
.ok();
|
.ok();
|
||||||
} else {
|
} else {
|
||||||
set_hovered.set(entering);
|
set_hovered.set(entering);
|
||||||
}
|
}
|
||||||
|
}}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut listener_options = UseEventListenerOptions::default().passive(true);
|
let listener_options = UseEventListenerOptions::default().passive(true);
|
||||||
|
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
el.clone(),
|
el.clone(),
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
use crate::core::{ElementMaybeSignal, Size};
|
use crate::core::{ElementMaybeSignal, Size};
|
||||||
use crate::{use_resize_observer_with_options, UseResizeObserverOptions};
|
use cfg_if::cfg_if;
|
||||||
use crate::{watch_with_options, WatchOptions};
|
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use wasm_bindgen::prelude::wasm_bindgen;
|
use wasm_bindgen::prelude::wasm_bindgen;
|
||||||
use wasm_bindgen::JsCast;
|
|
||||||
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
|
use crate::{use_resize_observer_with_options, UseResizeObserverOptions};
|
||||||
|
use crate::{watch_with_options, WatchOptions};
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
}}
|
||||||
|
|
||||||
/// Reactive size of an HTML element.
|
/// Reactive size of an HTML element.
|
||||||
///
|
///
|
||||||
|
@ -41,7 +45,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 the returned signals always contain the value of the `initial_size` option.
|
||||||
///
|
///
|
||||||
/// ## See also
|
/// ## See also
|
||||||
///
|
///
|
||||||
|
@ -56,6 +60,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Version of [`use_element_size`] that takes a `UseElementSizeOptions`. See [`use_element_size`] for how to use.
|
/// Version of [`use_element_size`] that takes a `UseElementSizeOptions`. See [`use_element_size`] for how to use.
|
||||||
|
#[cfg_attr(feature = "ssr", allow(unused_variables))]
|
||||||
pub fn use_element_size_with_options<El, T>(
|
pub fn use_element_size_with_options<El, T>(
|
||||||
target: El,
|
target: El,
|
||||||
options: UseElementSizeOptions,
|
options: UseElementSizeOptions,
|
||||||
|
@ -65,101 +70,104 @@ where
|
||||||
El: Into<ElementMaybeSignal<T, web_sys::Element>>,
|
El: Into<ElementMaybeSignal<T, web_sys::Element>>,
|
||||||
T: Into<web_sys::Element> + Clone + 'static,
|
T: Into<web_sys::Element> + Clone + 'static,
|
||||||
{
|
{
|
||||||
let window = window();
|
let UseElementSizeOptions { box_, initial_size } = options;
|
||||||
let box_ = options.box_;
|
|
||||||
let initial_size = options.initial_size;
|
|
||||||
|
|
||||||
let target = (target).into();
|
let (width, set_width) = create_signal(initial_size.width);
|
||||||
|
let (height, set_height) = create_signal(initial_size.height);
|
||||||
|
|
||||||
let is_svg = {
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
let target = target.clone();
|
|
||||||
|
|
||||||
move || {
|
let box_ = box_.unwrap_or(web_sys::ResizeObserverBoxOptions::ContentBox);
|
||||||
if let Some(target) = target.get_untracked() {
|
|
||||||
target
|
let target = target.into();
|
||||||
.into()
|
|
||||||
.namespace_uri()
|
let is_svg = {
|
||||||
.map(|ns| ns.contains("svg"))
|
let target = target.clone();
|
||||||
.unwrap_or(false)
|
|
||||||
} else {
|
move || {
|
||||||
false
|
if let Some(target) = target.get_untracked() {
|
||||||
|
target
|
||||||
|
.into()
|
||||||
|
.namespace_uri()
|
||||||
|
.map(|ns| ns.contains("svg"))
|
||||||
|
.unwrap_or(false)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
let (width, set_width) = create_signal(options.initial_size.width);
|
{
|
||||||
let (height, set_height) = create_signal(options.initial_size.height);
|
let target = target.clone();
|
||||||
|
|
||||||
{
|
let _ = use_resize_observer_with_options::<ElementMaybeSignal<T, web_sys::Element>, _, _>(
|
||||||
let target = target.clone();
|
target.clone(),
|
||||||
|
move |entries, _| {
|
||||||
|
let entry = &entries[0];
|
||||||
|
|
||||||
let _ = use_resize_observer_with_options::<ElementMaybeSignal<T, web_sys::Element>, _, _>(
|
let box_size = match box_ {
|
||||||
target.clone(),
|
web_sys::ResizeObserverBoxOptions::ContentBox => entry.content_box_size(),
|
||||||
move |entries, _| {
|
web_sys::ResizeObserverBoxOptions::BorderBox => entry.border_box_size(),
|
||||||
let entry = &entries[0];
|
web_sys::ResizeObserverBoxOptions::DevicePixelContentBox => {
|
||||||
|
entry.device_pixel_content_box_size()
|
||||||
let box_size = match box_ {
|
|
||||||
web_sys::ResizeObserverBoxOptions::ContentBox => entry.content_box_size(),
|
|
||||||
web_sys::ResizeObserverBoxOptions::BorderBox => entry.border_box_size(),
|
|
||||||
web_sys::ResizeObserverBoxOptions::DevicePixelContentBox => {
|
|
||||||
entry.device_pixel_content_box_size()
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if is_svg() {
|
|
||||||
if let Some(target) = target.get() {
|
|
||||||
if let Ok(Some(styles)) = window.get_computed_style(&target.into()) {
|
|
||||||
set_height.set(
|
|
||||||
styles
|
|
||||||
.get_property_value("height")
|
|
||||||
.map(|v| v.parse().unwrap_or_default())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
);
|
|
||||||
set_width.set(
|
|
||||||
styles
|
|
||||||
.get_property_value("width")
|
|
||||||
.map(|v| v.parse().unwrap_or_default())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
_ => unreachable!(),
|
||||||
} else if !box_size.is_null() && !box_size.is_undefined() && box_size.length() > 0 {
|
|
||||||
let format_box_size = if box_size.is_array() {
|
|
||||||
box_size.to_vec()
|
|
||||||
} else {
|
|
||||||
vec![box_size.into()]
|
|
||||||
};
|
};
|
||||||
|
|
||||||
set_width.set(format_box_size.iter().fold(0.0, |acc, v| {
|
if is_svg() {
|
||||||
acc + v.as_ref().clone().unchecked_into::<BoxSize>().inline_size()
|
if let Some(target) = target.get() {
|
||||||
}));
|
if let Ok(Some(styles)) = window().get_computed_style(&target.into()) {
|
||||||
set_height.set(format_box_size.iter().fold(0.0, |acc, v| {
|
set_height.set(
|
||||||
acc + v.as_ref().clone().unchecked_into::<BoxSize>().block_size()
|
styles
|
||||||
}))
|
.get_property_value("height")
|
||||||
|
.map(|v| v.parse().unwrap_or_default())
|
||||||
|
.unwrap_or_default(),
|
||||||
|
);
|
||||||
|
set_width.set(
|
||||||
|
styles
|
||||||
|
.get_property_value("width")
|
||||||
|
.map(|v| v.parse().unwrap_or_default())
|
||||||
|
.unwrap_or_default(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if !box_size.is_null() && !box_size.is_undefined() && box_size.length() > 0 {
|
||||||
|
let format_box_size = if box_size.is_array() {
|
||||||
|
box_size.to_vec()
|
||||||
|
} else {
|
||||||
|
vec![box_size.into()]
|
||||||
|
};
|
||||||
|
|
||||||
|
set_width.set(format_box_size.iter().fold(0.0, |acc, v| {
|
||||||
|
acc + v.as_ref().clone().unchecked_into::<BoxSize>().inline_size()
|
||||||
|
}));
|
||||||
|
set_height.set(format_box_size.iter().fold(0.0, |acc, v| {
|
||||||
|
acc + v.as_ref().clone().unchecked_into::<BoxSize>().block_size()
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
// fallback
|
||||||
|
set_width.set(entry.content_rect().width());
|
||||||
|
set_height.set(entry.content_rect().height())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
UseResizeObserverOptions::default().box_(box_),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = watch_with_options(
|
||||||
|
move || target.get(),
|
||||||
|
move |ele, _, _| {
|
||||||
|
if ele.is_some() {
|
||||||
|
set_width.set(initial_size.width);
|
||||||
|
set_height.set(initial_size.height);
|
||||||
} else {
|
} else {
|
||||||
// fallback
|
set_width.set(0.0);
|
||||||
set_width.set(entry.content_rect().width());
|
set_height.set(0.0);
|
||||||
set_height.set(entry.content_rect().height())
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
options.into(),
|
WatchOptions::default().immediate(false),
|
||||||
);
|
);
|
||||||
}
|
}}
|
||||||
|
|
||||||
let _ = watch_with_options(
|
|
||||||
move || target.get(),
|
|
||||||
move |ele, _, _| {
|
|
||||||
if ele.is_some() {
|
|
||||||
set_width.set(initial_size.width);
|
|
||||||
set_height.set(initial_size.height);
|
|
||||||
} else {
|
|
||||||
set_width.set(0.0);
|
|
||||||
set_height.set(0.0);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
WatchOptions::default().immediate(false),
|
|
||||||
);
|
|
||||||
|
|
||||||
UseElementSizeReturn {
|
UseElementSizeReturn {
|
||||||
width: width.into(),
|
width: width.into(),
|
||||||
|
@ -175,24 +183,19 @@ pub struct UseElementSizeOptions {
|
||||||
initial_size: Size,
|
initial_size: Size,
|
||||||
|
|
||||||
/// The box that is used to determine the dimensions of the target. Defaults to `ContentBox`.
|
/// The box that is used to determine the dimensions of the target. Defaults to `ContentBox`.
|
||||||
pub box_: web_sys::ResizeObserverBoxOptions,
|
#[builder(into)]
|
||||||
|
pub box_: Option<web_sys::ResizeObserverBoxOptions>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for UseElementSizeOptions {
|
impl Default for UseElementSizeOptions {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
initial_size: Size::default(),
|
initial_size: Size::default(),
|
||||||
box_: web_sys::ResizeObserverBoxOptions::ContentBox,
|
box_: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<UseElementSizeOptions> for UseResizeObserverOptions {
|
|
||||||
fn from(options: UseElementSizeOptions) -> Self {
|
|
||||||
Self::default().box_(options.box_)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The return value of [`use_element_size`].
|
/// The return value of [`use_element_size`].
|
||||||
pub struct UseElementSizeReturn {
|
pub struct UseElementSizeReturn {
|
||||||
/// The width of the element.
|
/// The width of the element.
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
use crate::core::ElementMaybeSignal;
|
use crate::core::ElementMaybeSignal;
|
||||||
use crate::{use_intersection_observer_with_options, UseIntersectionObserverOptions};
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "ssr"))]
|
||||||
|
use crate::{use_intersection_observer_with_options, UseIntersectionObserverOptions};
|
||||||
|
|
||||||
/// Tracks the visibility of an element within the viewport.
|
/// Tracks the visibility of an element within the viewport.
|
||||||
///
|
///
|
||||||
/// ## Demo
|
/// ## Demo
|
||||||
|
@ -33,7 +36,7 @@ use std::marker::PhantomData;
|
||||||
///
|
///
|
||||||
/// ## 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 a `Signal` that always contains the value `false`.
|
||||||
///
|
///
|
||||||
/// ## See also
|
/// ## See also
|
||||||
///
|
///
|
||||||
|
@ -49,6 +52,8 @@ where
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Version of [`use_element_visibility`] with that takes a `UseElementVisibilityOptions`. See [`use_element_visibility`] for how to use.
|
||||||
|
#[cfg_attr(feature = "ssr", allow(unused_variables))]
|
||||||
pub fn use_element_visibility_with_options<El, T, ContainerEl, ContainerT>(
|
pub fn use_element_visibility_with_options<El, T, ContainerEl, ContainerT>(
|
||||||
target: El,
|
target: El,
|
||||||
options: UseElementVisibilityOptions<ContainerEl, ContainerT>,
|
options: UseElementVisibilityOptions<ContainerEl, ContainerT>,
|
||||||
|
@ -61,20 +66,22 @@ where
|
||||||
{
|
{
|
||||||
let (is_visible, set_visible) = create_signal(false);
|
let (is_visible, set_visible) = create_signal(false);
|
||||||
|
|
||||||
use_intersection_observer_with_options(
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
target.into(),
|
use_intersection_observer_with_options(
|
||||||
move |entries, _| {
|
target.into(),
|
||||||
// In some circumstances Chrome passes a first (or only) entry which has a zero bounding client rect
|
move |entries, _| {
|
||||||
// and returns `is_intersecting` erroneously as `false`.
|
// In some circumstances Chrome passes a first (or only) entry which has a zero bounding client rect
|
||||||
if let Some(entry) = entries.into_iter().find(|entry| {
|
// and returns `is_intersecting` erroneously as `false`.
|
||||||
let rect = entry.bounding_client_rect();
|
if let Some(entry) = entries.into_iter().find(|entry| {
|
||||||
rect.width() > 0.0 || rect.height() > 0.0
|
let rect = entry.bounding_client_rect();
|
||||||
}) {
|
rect.width() > 0.0 || rect.height() > 0.0
|
||||||
set_visible.set(entry.is_intersecting());
|
}) {
|
||||||
}
|
set_visible.set(entry.is_intersecting());
|
||||||
},
|
}
|
||||||
UseIntersectionObserverOptions::default().root(options.viewport),
|
},
|
||||||
);
|
UseIntersectionObserverOptions::default().root(options.viewport),
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
|
||||||
is_visible.into()
|
is_visible.into()
|
||||||
}
|
}
|
||||||
|
@ -92,6 +99,7 @@ where
|
||||||
/// Defaults to `None` (which means the root `document` will be used).
|
/// Defaults to `None` (which means the root `document` will be used).
|
||||||
/// Please note that setting this to a `Some(document)` may not be supported by all browsers.
|
/// Please note that setting this to a `Some(document)` may not be supported by all browsers.
|
||||||
/// See [Browser Compatibility](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#browser_compatibility)
|
/// See [Browser Compatibility](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#browser_compatibility)
|
||||||
|
#[cfg_attr(feature = "ssr", allow(dead_code))]
|
||||||
viewport: Option<El>,
|
viewport: Option<El>,
|
||||||
|
|
||||||
#[builder(skip)]
|
#[builder(skip)]
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
use crate::core::ElementMaybeSignal;
|
use crate::core::ElementMaybeSignal;
|
||||||
use crate::{watch_with_options, WatchOptions};
|
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 std::cell::RefCell;
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
use std::rc::Rc;
|
use crate::{watch_with_options, WatchOptions};
|
||||||
use wasm_bindgen::closure::Closure;
|
use leptos::*;
|
||||||
use wasm_bindgen::JsCast;
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use wasm_bindgen::closure::Closure;
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
}}
|
||||||
|
|
||||||
/// Use EventListener with ease.
|
/// Use EventListener with ease.
|
||||||
/// Register using [addEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) on mounted,
|
/// Register using [addEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) on mounted,
|
||||||
|
@ -94,6 +98,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Version of [`use_event_listener`] that takes `web_sys::AddEventListenerOptions`. See the docs for [`use_event_listener`] for how to use.
|
/// Version of [`use_event_listener`] that takes `web_sys::AddEventListenerOptions`. See the docs for [`use_event_listener`] for how to use.
|
||||||
|
#[cfg_attr(feature = "ssr", allow(unused_variables))]
|
||||||
pub fn use_event_listener_with_options<Ev, El, T, F>(
|
pub fn use_event_listener_with_options<Ev, El, T, F>(
|
||||||
target: El,
|
target: El,
|
||||||
event: Ev,
|
event: Ev,
|
||||||
|
@ -106,73 +111,78 @@ where
|
||||||
T: Into<web_sys::EventTarget> + Clone + 'static,
|
T: Into<web_sys::EventTarget> + Clone + 'static,
|
||||||
F: FnMut(<Ev as EventDescriptor>::EventType) + 'static,
|
F: FnMut(<Ev as EventDescriptor>::EventType) + 'static,
|
||||||
{
|
{
|
||||||
let event_name = event.name();
|
cfg_if! { if #[cfg(feature = "ssr")] {
|
||||||
let closure_js = Closure::wrap(Box::new(handler) as Box<dyn FnMut(_)>).into_js_value();
|
|| {}
|
||||||
|
} else {
|
||||||
|
let event_name = event.name();
|
||||||
|
let closure_js = Closure::wrap(Box::new(handler) as Box<dyn FnMut(_)>).into_js_value();
|
||||||
|
|
||||||
let cleanup_fn = {
|
let cleanup_fn = {
|
||||||
let closure_js = closure_js.clone();
|
let closure_js = closure_js.clone();
|
||||||
let options = options.as_add_event_listener_options();
|
let options = options.as_add_event_listener_options();
|
||||||
|
|
||||||
move |element: &web_sys::EventTarget| {
|
move |element: &web_sys::EventTarget| {
|
||||||
let _ = element.remove_event_listener_with_callback_and_event_listener_options(
|
let _ = element.remove_event_listener_with_callback_and_event_listener_options(
|
||||||
&event_name,
|
&event_name,
|
||||||
closure_js.as_ref().unchecked_ref(),
|
closure_js.as_ref().unchecked_ref(),
|
||||||
options.unchecked_ref(),
|
options.unchecked_ref(),
|
||||||
);
|
);
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let event_name = event.name();
|
|
||||||
|
|
||||||
let signal = (target).into();
|
|
||||||
|
|
||||||
let prev_element = Rc::new(RefCell::new(None::<web_sys::EventTarget>));
|
|
||||||
|
|
||||||
let cleanup_prev_element = {
|
|
||||||
let prev_element = prev_element.clone();
|
|
||||||
|
|
||||||
move || {
|
|
||||||
if let Some(element) = prev_element.take() {
|
|
||||||
cleanup_fn(&element);
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
let stop_watch = {
|
let event_name = event.name();
|
||||||
let cleanup_prev_element = cleanup_prev_element.clone();
|
|
||||||
|
|
||||||
watch_with_options(
|
let signal = (target).into();
|
||||||
move || signal.get().map(|e| e.into()),
|
|
||||||
move |element, _, _| {
|
|
||||||
cleanup_prev_element();
|
|
||||||
prev_element.replace(element.clone());
|
|
||||||
|
|
||||||
if let Some(element) = element {
|
let prev_element = Rc::new(RefCell::new(None::<web_sys::EventTarget>));
|
||||||
let options = options.as_add_event_listener_options();
|
|
||||||
|
|
||||||
_ = element.add_event_listener_with_callback_and_add_event_listener_options(
|
let cleanup_prev_element = {
|
||||||
&event_name,
|
let prev_element = prev_element.clone();
|
||||||
closure_js.as_ref().unchecked_ref(),
|
|
||||||
&options,
|
move || {
|
||||||
);
|
if let Some(element) = prev_element.take() {
|
||||||
|
cleanup_fn(&element);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
WatchOptions::default().immediate(true),
|
};
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let stop = move || {
|
let stop_watch = {
|
||||||
stop_watch();
|
let cleanup_prev_element = cleanup_prev_element.clone();
|
||||||
cleanup_prev_element();
|
|
||||||
};
|
|
||||||
|
|
||||||
on_cleanup(stop.clone());
|
watch_with_options(
|
||||||
|
move || signal.get().map(|e| e.into()),
|
||||||
|
move |element, _, _| {
|
||||||
|
cleanup_prev_element();
|
||||||
|
prev_element.replace(element.clone());
|
||||||
|
|
||||||
stop
|
if let Some(element) = element {
|
||||||
|
let options = options.as_add_event_listener_options();
|
||||||
|
|
||||||
|
_ = element.add_event_listener_with_callback_and_add_event_listener_options(
|
||||||
|
&event_name,
|
||||||
|
closure_js.as_ref().unchecked_ref(),
|
||||||
|
&options,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
WatchOptions::default().immediate(true),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let stop = move || {
|
||||||
|
stop_watch();
|
||||||
|
cleanup_prev_element();
|
||||||
|
};
|
||||||
|
|
||||||
|
on_cleanup(stop.clone());
|
||||||
|
|
||||||
|
stop
|
||||||
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Options for [`use_event_listener_with_options`].
|
/// Options for [`use_event_listener_with_options`].
|
||||||
#[derive(DefaultBuilder, Default, Copy, Clone)]
|
#[derive(DefaultBuilder, Default, Copy, Clone)]
|
||||||
|
#[cfg_attr(feature = "ssr", allow(dead_code))]
|
||||||
pub struct UseEventListenerOptions {
|
pub struct UseEventListenerOptions {
|
||||||
/// A boolean value indicating that events of this type will be dispatched to
|
/// A boolean value indicating that events of this type will be dispatched to
|
||||||
/// the registered `listener` before being dispatched to any `EventTarget`
|
/// the registered `listener` before being dispatched to any `EventTarget`
|
||||||
|
@ -202,6 +212,7 @@ pub struct UseEventListenerOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UseEventListenerOptions {
|
impl UseEventListenerOptions {
|
||||||
|
#[cfg_attr(feature = "ssr", allow(dead_code))]
|
||||||
fn as_add_event_listener_options(&self) -> web_sys::AddEventListenerOptions {
|
fn as_add_event_listener_options(&self) -> web_sys::AddEventListenerOptions {
|
||||||
let UseEventListenerOptions {
|
let UseEventListenerOptions {
|
||||||
capture,
|
capture,
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
use crate::core::{ElementMaybeSignal, ElementsMaybeSignal};
|
use crate::core::{ElementMaybeSignal, ElementsMaybeSignal};
|
||||||
use crate::{watch_with_options, WatchOptions};
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::rc::Rc;
|
|
||||||
use wasm_bindgen::prelude::*;
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
|
use crate::{watch_with_options, WatchOptions};
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
}}
|
||||||
|
|
||||||
/// Reactive [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver).
|
/// Reactive [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver).
|
||||||
///
|
///
|
||||||
|
@ -44,7 +48,7 @@ use wasm_bindgen::prelude::*;
|
||||||
///
|
///
|
||||||
/// ## 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 amounts to a no-op.
|
||||||
///
|
///
|
||||||
/// ## See also
|
/// ## See also
|
||||||
///
|
///
|
||||||
|
@ -66,6 +70,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Version of [`use_intersection_observer`] that takes a [`UseIntersectionObserverOptions`]. See [`use_intersection_observer`] for how to use.
|
/// Version of [`use_intersection_observer`] that takes a [`UseIntersectionObserverOptions`]. See [`use_intersection_observer`] for how to use.
|
||||||
|
#[cfg_attr(feature = "ssr", allow(unused_variables, unused_mut))]
|
||||||
pub fn use_intersection_observer_with_options<El, T, RootEl, RootT, F>(
|
pub fn use_intersection_observer_with_options<El, T, RootEl, RootT, F>(
|
||||||
target: El,
|
target: El,
|
||||||
mut callback: F,
|
mut callback: F,
|
||||||
|
@ -86,107 +91,113 @@ where
|
||||||
..
|
..
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
let closure_js = Closure::<dyn FnMut(js_sys::Array, web_sys::IntersectionObserver)>::new(
|
|
||||||
move |entries: js_sys::Array, observer| {
|
|
||||||
callback(
|
|
||||||
entries
|
|
||||||
.to_vec()
|
|
||||||
.into_iter()
|
|
||||||
.map(|v| v.unchecked_into::<web_sys::IntersectionObserverEntry>())
|
|
||||||
.collect(),
|
|
||||||
observer,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.into_js_value();
|
|
||||||
|
|
||||||
let (is_active, set_active) = create_signal(immediate);
|
let (is_active, set_active) = create_signal(immediate);
|
||||||
|
|
||||||
let observer: Rc<RefCell<Option<web_sys::IntersectionObserver>>> = Rc::new(RefCell::new(None));
|
cfg_if! { if #[cfg(feature = "ssr")] {
|
||||||
|
let pause = || {};
|
||||||
let cleanup = {
|
let cleanup = || {};
|
||||||
let obsserver = Rc::clone(&observer);
|
let stop = || {};
|
||||||
|
} else {
|
||||||
move || {
|
let closure_js = Closure::<dyn FnMut(js_sys::Array, web_sys::IntersectionObserver)>::new(
|
||||||
if let Some(o) = obsserver.take() {
|
move |entries: js_sys::Array, observer| {
|
||||||
o.disconnect();
|
callback(
|
||||||
}
|
entries
|
||||||
}
|
.to_vec()
|
||||||
};
|
.into_iter()
|
||||||
|
.map(|v| v.unchecked_into::<web_sys::IntersectionObserverEntry>())
|
||||||
let targets = (target).into();
|
.collect(),
|
||||||
let root = root.map(|root| (root).into());
|
observer,
|
||||||
|
|
||||||
let stop_watch = {
|
|
||||||
let cleanup = cleanup.clone();
|
|
||||||
|
|
||||||
watch_with_options(
|
|
||||||
move || {
|
|
||||||
(
|
|
||||||
targets.get(),
|
|
||||||
root.as_ref().map(|root| root.get()),
|
|
||||||
is_active.get(),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
move |values, _, _| {
|
|
||||||
let (targets, root, is_active) = values;
|
|
||||||
|
|
||||||
cleanup();
|
|
||||||
|
|
||||||
if !is_active {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut options = web_sys::IntersectionObserverInit::new();
|
|
||||||
options.root_margin(&root_margin).threshold(
|
|
||||||
&thresholds
|
|
||||||
.iter()
|
|
||||||
.copied()
|
|
||||||
.map(JsValue::from)
|
|
||||||
.collect::<js_sys::Array>(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(Some(root)) = root {
|
|
||||||
let root: web_sys::Element = root.clone().into();
|
|
||||||
options.root(Some(&root));
|
|
||||||
}
|
|
||||||
|
|
||||||
let obs = web_sys::IntersectionObserver::new_with_options(
|
|
||||||
closure_js.clone().as_ref().unchecked_ref(),
|
|
||||||
&options,
|
|
||||||
)
|
|
||||||
.expect("failed to create IntersectionObserver");
|
|
||||||
|
|
||||||
for target in targets.iter().flatten() {
|
|
||||||
let target: web_sys::Element = target.clone().into();
|
|
||||||
obs.observe(&target);
|
|
||||||
}
|
|
||||||
|
|
||||||
observer.replace(Some(obs));
|
|
||||||
},
|
},
|
||||||
WatchOptions::default().immediate(immediate),
|
|
||||||
)
|
)
|
||||||
};
|
.into_js_value();
|
||||||
|
|
||||||
let stop = {
|
let observer: Rc<RefCell<Option<web_sys::IntersectionObserver>>> = Rc::new(RefCell::new(None));
|
||||||
let cleanup = cleanup.clone();
|
|
||||||
|
|
||||||
move || {
|
let cleanup = {
|
||||||
cleanup();
|
let obsserver = Rc::clone(&observer);
|
||||||
stop_watch();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
on_cleanup(stop.clone());
|
move || {
|
||||||
|
if let Some(o) = obsserver.take() {
|
||||||
|
o.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let pause = {
|
let targets = (target).into();
|
||||||
let cleanup = cleanup.clone();
|
let root = root.map(|root| (root).into());
|
||||||
|
|
||||||
move || {
|
let stop_watch = {
|
||||||
cleanup();
|
let cleanup = cleanup.clone();
|
||||||
set_active.set(false);
|
|
||||||
}
|
watch_with_options(
|
||||||
};
|
move || {
|
||||||
|
(
|
||||||
|
targets.get(),
|
||||||
|
root.as_ref().map(|root| root.get()),
|
||||||
|
is_active.get(),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
move |values, _, _| {
|
||||||
|
let (targets, root, is_active) = values;
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
|
||||||
|
if !is_active {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut options = web_sys::IntersectionObserverInit::new();
|
||||||
|
options.root_margin(&root_margin).threshold(
|
||||||
|
&thresholds
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.map(JsValue::from)
|
||||||
|
.collect::<js_sys::Array>(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Some(Some(root)) = root {
|
||||||
|
let root: web_sys::Element = root.clone().into();
|
||||||
|
options.root(Some(&root));
|
||||||
|
}
|
||||||
|
|
||||||
|
let obs = web_sys::IntersectionObserver::new_with_options(
|
||||||
|
closure_js.clone().as_ref().unchecked_ref(),
|
||||||
|
&options,
|
||||||
|
)
|
||||||
|
.expect("failed to create IntersectionObserver");
|
||||||
|
|
||||||
|
for target in targets.iter().flatten() {
|
||||||
|
let target: web_sys::Element = target.clone().into();
|
||||||
|
obs.observe(&target);
|
||||||
|
}
|
||||||
|
|
||||||
|
observer.replace(Some(obs));
|
||||||
|
},
|
||||||
|
WatchOptions::default().immediate(immediate),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let stop = {
|
||||||
|
let cleanup = cleanup.clone();
|
||||||
|
|
||||||
|
move || {
|
||||||
|
cleanup();
|
||||||
|
stop_watch();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
on_cleanup(stop.clone());
|
||||||
|
|
||||||
|
let pause = {
|
||||||
|
let cleanup = cleanup.clone();
|
||||||
|
|
||||||
|
move || {
|
||||||
|
cleanup();
|
||||||
|
set_active.set(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}}
|
||||||
|
|
||||||
UseIntersectionObserverReturn {
|
UseIntersectionObserverReturn {
|
||||||
is_active: is_active.into(),
|
is_active: is_active.into(),
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
||||||
|
|
||||||
use crate::core::{ElementMaybeSignal, Position};
|
use crate::core::{ElementMaybeSignal, Position};
|
||||||
use crate::use_event_listener_with_options;
|
use crate::{use_event_listener_with_options, UseEventListenerOptions};
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::ev::{dragover, mousemove, touchend, touchmove, touchstart};
|
use leptos::ev::{dragover, mousemove, touchend, touchmove, touchstart};
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use wasm_bindgen::{JsCast, JsValue};
|
use wasm_bindgen::{JsCast, JsValue};
|
||||||
use web_sys::AddEventListenerOptions;
|
|
||||||
|
|
||||||
/// Reactive mouse position
|
/// Reactive mouse position
|
||||||
///
|
///
|
||||||
|
@ -158,40 +157,39 @@ where
|
||||||
|
|
||||||
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
let target = options.target;
|
let target = options.target;
|
||||||
let mut event_listener_options = AddEventListenerOptions::new();
|
let event_listener_options = UseEventListenerOptions::default().passive(true);
|
||||||
event_listener_options.passive(true);
|
|
||||||
|
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
target.clone(),
|
target.clone(),
|
||||||
mousemove,
|
mousemove,
|
||||||
mouse_handler,
|
mouse_handler,
|
||||||
event_listener_options.clone(),
|
event_listener_options,
|
||||||
);
|
);
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
target.clone(),
|
target.clone(),
|
||||||
dragover,
|
dragover,
|
||||||
drag_handler,
|
drag_handler,
|
||||||
event_listener_options.clone(),
|
event_listener_options,
|
||||||
);
|
);
|
||||||
if options.touch && !matches!(options.coord_type, UseMouseCoordType::Movement) {
|
if options.touch && !matches!(options.coord_type, UseMouseCoordType::Movement) {
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
target.clone(),
|
target.clone(),
|
||||||
touchstart,
|
touchstart,
|
||||||
touch_handler.clone(),
|
touch_handler.clone(),
|
||||||
event_listener_options.clone(),
|
event_listener_options,
|
||||||
);
|
);
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
target.clone(),
|
target.clone(),
|
||||||
touchmove,
|
touchmove,
|
||||||
touch_handler,
|
touch_handler,
|
||||||
event_listener_options.clone(),
|
event_listener_options,
|
||||||
);
|
);
|
||||||
if options.reset_on_touch_ends {
|
if options.reset_on_touch_ends {
|
||||||
let _ = use_event_listener_with_options(
|
let _ = use_event_listener_with_options(
|
||||||
target,
|
target,
|
||||||
touchend,
|
touchend,
|
||||||
move |_| reset(),
|
move |_| reset(),
|
||||||
event_listener_options.clone(),
|
event_listener_options,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
use crate::core::ElementsMaybeSignal;
|
use crate::core::ElementsMaybeSignal;
|
||||||
use crate::use_supported;
|
use cfg_if::cfg_if;
|
||||||
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
use web_sys::MutationObserverInit;
|
|
||||||
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
|
use crate::use_supported;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
}}
|
||||||
|
|
||||||
/// Reactive [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
|
/// Reactive [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
|
||||||
///
|
///
|
||||||
|
@ -19,16 +23,13 @@ use web_sys::MutationObserverInit;
|
||||||
/// ```
|
/// ```
|
||||||
/// # use leptos::*;
|
/// # use leptos::*;
|
||||||
/// # use leptos::html::Pre;
|
/// # use leptos::html::Pre;
|
||||||
/// # use leptos_use::use_mutation_observer_with_options;
|
/// # use leptos_use::{use_mutation_observer_with_options, UseMutationObserverOptions};
|
||||||
/// #
|
/// #
|
||||||
/// # #[component]
|
/// # #[component]
|
||||||
/// # fn Demo() -> impl IntoView {
|
/// # fn Demo() -> impl IntoView {
|
||||||
/// let el = create_node_ref::<Pre>();
|
/// let el = create_node_ref::<Pre>();
|
||||||
/// let (text, set_text) = create_signal("".to_string());
|
/// let (text, set_text) = create_signal("".to_string());
|
||||||
///
|
///
|
||||||
/// let mut init = web_sys::MutationObserverInit::new();
|
|
||||||
/// init.attributes(true);
|
|
||||||
///
|
|
||||||
/// use_mutation_observer_with_options(
|
/// use_mutation_observer_with_options(
|
||||||
/// el,
|
/// el,
|
||||||
/// move |mutations, _| {
|
/// move |mutations, _| {
|
||||||
|
@ -36,7 +37,7 @@ use web_sys::MutationObserverInit;
|
||||||
/// set_text.update(|text| *text = format!("{text}\n{:?}", mutation.attribute_name()));
|
/// set_text.update(|text| *text = format!("{text}\n{:?}", mutation.attribute_name()));
|
||||||
/// }
|
/// }
|
||||||
/// },
|
/// },
|
||||||
/// init,
|
/// UseMutationObserverOptions::default().attributes(true),
|
||||||
/// );
|
/// );
|
||||||
///
|
///
|
||||||
/// view! {
|
/// view! {
|
||||||
|
@ -47,7 +48,7 @@ use web_sys::MutationObserverInit;
|
||||||
///
|
///
|
||||||
/// ## 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 amounts to a no-op.
|
||||||
pub fn use_mutation_observer<El, T, F>(
|
pub fn use_mutation_observer<El, T, F>(
|
||||||
target: El,
|
target: El,
|
||||||
callback: F,
|
callback: F,
|
||||||
|
@ -57,84 +58,167 @@ where
|
||||||
T: Into<web_sys::Element> + Clone + 'static,
|
T: Into<web_sys::Element> + Clone + 'static,
|
||||||
F: FnMut(Vec<web_sys::MutationRecord>, web_sys::MutationObserver) + 'static,
|
F: FnMut(Vec<web_sys::MutationRecord>, web_sys::MutationObserver) + 'static,
|
||||||
{
|
{
|
||||||
use_mutation_observer_with_options(target, callback, MutationObserverInit::default())
|
use_mutation_observer_with_options(target, callback, UseMutationObserverOptions::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Version of [`use_mutation_observer`] that takes a `web_sys::MutationObserverInit`. See [`use_mutation_observer`] for how to use.
|
/// Version of [`use_mutation_observer`] that takes a `UseMutationObserverOptions`. See [`use_mutation_observer`] for how to use.
|
||||||
|
#[cfg_attr(feature = "ssr", allow(unused_variables, unused_mut))]
|
||||||
pub fn use_mutation_observer_with_options<El, T, F>(
|
pub fn use_mutation_observer_with_options<El, T, F>(
|
||||||
target: El,
|
target: El,
|
||||||
mut callback: F,
|
mut callback: F,
|
||||||
options: web_sys::MutationObserverInit,
|
options: UseMutationObserverOptions,
|
||||||
) -> UseMutationObserverReturn<impl Fn() + Clone>
|
) -> UseMutationObserverReturn<impl Fn() + Clone>
|
||||||
where
|
where
|
||||||
El: Into<ElementsMaybeSignal<T, web_sys::Element>>,
|
El: Into<ElementsMaybeSignal<T, web_sys::Element>>,
|
||||||
T: Into<web_sys::Element> + Clone + 'static,
|
T: Into<web_sys::Element> + Clone + 'static,
|
||||||
F: FnMut(Vec<web_sys::MutationRecord>, web_sys::MutationObserver) + 'static,
|
F: FnMut(Vec<web_sys::MutationRecord>, web_sys::MutationObserver) + 'static,
|
||||||
{
|
{
|
||||||
let closure_js = Closure::<dyn FnMut(js_sys::Array, web_sys::MutationObserver)>::new(
|
cfg_if! { if #[cfg(feature = "ssr")] {
|
||||||
move |entries: js_sys::Array, observer| {
|
UseMutationObserverReturn {
|
||||||
callback(
|
is_supported: Signal::derive(|| true),
|
||||||
entries
|
stop: || {},
|
||||||
.to_vec()
|
|
||||||
.into_iter()
|
|
||||||
.map(|v| v.unchecked_into::<web_sys::MutationRecord>())
|
|
||||||
.collect(),
|
|
||||||
observer,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.into_js_value();
|
|
||||||
|
|
||||||
let observer: Rc<RefCell<Option<web_sys::MutationObserver>>> = Rc::new(RefCell::new(None));
|
|
||||||
|
|
||||||
let is_supported = use_supported(|| JsValue::from("MutationObserver").js_in(&window()));
|
|
||||||
|
|
||||||
let cleanup = {
|
|
||||||
let observer = Rc::clone(&observer);
|
|
||||||
|
|
||||||
move || {
|
|
||||||
let mut observer = observer.borrow_mut();
|
|
||||||
if let Some(o) = observer.as_ref() {
|
|
||||||
o.disconnect();
|
|
||||||
*observer = None;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
} else {
|
||||||
|
let closure_js = Closure::<dyn FnMut(js_sys::Array, web_sys::MutationObserver)>::new(
|
||||||
let targets = (target).into();
|
move |entries: js_sys::Array, observer| {
|
||||||
|
callback(
|
||||||
let stop_watch = {
|
entries
|
||||||
let cleanup = cleanup.clone();
|
.to_vec()
|
||||||
|
.into_iter()
|
||||||
leptos::watch(
|
.map(|v| v.unchecked_into::<web_sys::MutationRecord>())
|
||||||
move || targets.get(),
|
.collect(),
|
||||||
move |targets, _, _| {
|
observer,
|
||||||
cleanup();
|
);
|
||||||
|
|
||||||
if is_supported.get() && !targets.is_empty() {
|
|
||||||
let obs = web_sys::MutationObserver::new(closure_js.as_ref().unchecked_ref())
|
|
||||||
.expect("failed to create MutationObserver");
|
|
||||||
|
|
||||||
for target in targets.iter().flatten() {
|
|
||||||
let target: web_sys::Element = target.clone().into();
|
|
||||||
let _ = obs.observe_with_options(&target, &options.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
observer.replace(Some(obs));
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
false,
|
|
||||||
)
|
)
|
||||||
};
|
.into_js_value();
|
||||||
|
|
||||||
let stop = move || {
|
let observer: Rc<RefCell<Option<web_sys::MutationObserver>>> = Rc::new(RefCell::new(None));
|
||||||
cleanup();
|
|
||||||
stop_watch();
|
|
||||||
};
|
|
||||||
|
|
||||||
on_cleanup(stop.clone());
|
let is_supported = use_supported(|| JsValue::from("MutationObserver").js_in(&window()));
|
||||||
|
|
||||||
UseMutationObserverReturn { is_supported, stop }
|
let cleanup = {
|
||||||
|
let observer = Rc::clone(&observer);
|
||||||
|
|
||||||
|
move || {
|
||||||
|
let mut observer = observer.borrow_mut();
|
||||||
|
if let Some(o) = observer.as_ref() {
|
||||||
|
o.disconnect();
|
||||||
|
*observer = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let targets = (target).into();
|
||||||
|
|
||||||
|
let stop_watch = {
|
||||||
|
let cleanup = cleanup.clone();
|
||||||
|
|
||||||
|
leptos::watch(
|
||||||
|
move || targets.get(),
|
||||||
|
move |targets, _, _| {
|
||||||
|
cleanup();
|
||||||
|
|
||||||
|
if is_supported.get() && !targets.is_empty() {
|
||||||
|
let obs = web_sys::MutationObserver::new(closure_js.as_ref().unchecked_ref())
|
||||||
|
.expect("failed to create MutationObserver");
|
||||||
|
|
||||||
|
for target in targets.iter().flatten() {
|
||||||
|
let target: web_sys::Element = target.clone().into();
|
||||||
|
let _ = obs.observe_with_options(&target, &options.clone().into());
|
||||||
|
}
|
||||||
|
|
||||||
|
observer.replace(Some(obs));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let stop = move || {
|
||||||
|
cleanup();
|
||||||
|
stop_watch();
|
||||||
|
};
|
||||||
|
|
||||||
|
on_cleanup(stop.clone());
|
||||||
|
|
||||||
|
UseMutationObserverReturn { is_supported, stop }
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Options for [`use_mutation_observer_with_options`].
|
||||||
|
#[derive(DefaultBuilder, Clone, Default)]
|
||||||
|
pub struct UseMutationObserverOptions {
|
||||||
|
/// Set to `true` to extend monitoring to the entire subtree of nodes rooted at `target`.
|
||||||
|
/// All of the other properties are then extended to all of the nodes in the subtree
|
||||||
|
/// instead of applying solely to the `target` node. The default value is `false`.
|
||||||
|
subtree: bool,
|
||||||
|
|
||||||
|
/// Set to `true` to monitor the target node (and, if `subtree` is `true`, its descendants)
|
||||||
|
/// for the addition of new child nodes or removal of existing child nodes.
|
||||||
|
/// The default value is `false`.
|
||||||
|
child_list: bool,
|
||||||
|
|
||||||
|
/// Set to `true` to watch for changes to the value of attributes on the node or nodes being
|
||||||
|
/// monitored. The default value is `true` if either of `attribute_filter` or
|
||||||
|
/// `attribute_old_value` is specified, otherwise the default value is `false`.
|
||||||
|
attributes: bool,
|
||||||
|
|
||||||
|
/// An array of specific attribute names to be monitored. If this property isn't included,
|
||||||
|
/// changes to all attributes cause mutation notifications.
|
||||||
|
#[builder(into)]
|
||||||
|
attribute_filter: Option<Vec<String>>,
|
||||||
|
|
||||||
|
/// Set to `true` to record the previous value of any attribute that changes when monitoring
|
||||||
|
/// the node or nodes for attribute changes; See
|
||||||
|
/// [Monitoring attribute values](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe#monitoring_attribute_values)
|
||||||
|
/// for an example of watching for attribute changes and recording values.
|
||||||
|
/// The default value is `false`.
|
||||||
|
attribute_old_value: bool,
|
||||||
|
|
||||||
|
/// Set to `true` to monitor the specified target node
|
||||||
|
/// (and, if `subtree` is `true`, its descendants)
|
||||||
|
/// for changes to the character data contained within the node or nodes.
|
||||||
|
/// The default value is `true` if `character_data_old_value` is specified,
|
||||||
|
/// otherwise the default value is `false`.
|
||||||
|
#[builder(into)]
|
||||||
|
character_data: Option<bool>,
|
||||||
|
|
||||||
|
/// Set to `true` to record the previous value of a node's text whenever the text changes on
|
||||||
|
/// nodes being monitored. The default value is `false`.
|
||||||
|
character_data_old_value: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<UseMutationObserverOptions> for web_sys::MutationObserverInit {
|
||||||
|
fn from(val: UseMutationObserverOptions) -> Self {
|
||||||
|
let UseMutationObserverOptions {
|
||||||
|
subtree,
|
||||||
|
child_list,
|
||||||
|
attributes,
|
||||||
|
attribute_filter,
|
||||||
|
attribute_old_value,
|
||||||
|
character_data,
|
||||||
|
character_data_old_value,
|
||||||
|
} = val;
|
||||||
|
|
||||||
|
let mut init = Self::new();
|
||||||
|
|
||||||
|
init.subtree(subtree)
|
||||||
|
.child_list(child_list)
|
||||||
|
.attributes(attributes)
|
||||||
|
.attribute_old_value(attribute_old_value)
|
||||||
|
.character_data_old_value(character_data_old_value);
|
||||||
|
|
||||||
|
if let Some(attribute_filter) = attribute_filter {
|
||||||
|
let array = js_sys::Array::from_iter(attribute_filter.into_iter().map(JsValue::from));
|
||||||
|
init.attribute_filter(array.unchecked_ref());
|
||||||
|
}
|
||||||
|
if let Some(character_data) = character_data {
|
||||||
|
init.character_data(character_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
init
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The return value of [`use_mutation_observer`].
|
/// The return value of [`use_mutation_observer`].
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
use crate::core::ElementsMaybeSignal;
|
use crate::core::ElementsMaybeSignal;
|
||||||
use crate::use_supported;
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
use wasm_bindgen::prelude::*;
|
use crate::use_supported;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
}}
|
||||||
|
|
||||||
/// Reports changes to the dimensions of an Element's content or the border-box.
|
/// Reports changes to the dimensions of an Element's content or the border-box.
|
||||||
///
|
///
|
||||||
|
@ -45,7 +49,7 @@ use wasm_bindgen::prelude::*;
|
||||||
///
|
///
|
||||||
/// ## 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 amounts to a no-op.
|
||||||
///
|
///
|
||||||
/// ## See also
|
/// ## See also
|
||||||
///
|
///
|
||||||
|
@ -63,6 +67,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Version of [`use_resize_observer`] that takes a `web_sys::ResizeObserverOptions`. See [`use_resize_observer`] for how to use.
|
/// Version of [`use_resize_observer`] that takes a `web_sys::ResizeObserverOptions`. See [`use_resize_observer`] for how to use.
|
||||||
|
#[cfg_attr(feature = "ssr", allow(unused_variables, unused_mut))]
|
||||||
pub fn use_resize_observer_with_options<El, T, F>(
|
pub fn use_resize_observer_with_options<El, T, F>(
|
||||||
target: El, // TODO : multiple elements?
|
target: El, // TODO : multiple elements?
|
||||||
mut callback: F,
|
mut callback: F,
|
||||||
|
@ -73,92 +78,95 @@ where
|
||||||
T: Into<web_sys::Element> + Clone + 'static,
|
T: Into<web_sys::Element> + Clone + 'static,
|
||||||
F: FnMut(Vec<web_sys::ResizeObserverEntry>, web_sys::ResizeObserver) + 'static,
|
F: FnMut(Vec<web_sys::ResizeObserverEntry>, web_sys::ResizeObserver) + 'static,
|
||||||
{
|
{
|
||||||
let closure_js = Closure::<dyn FnMut(js_sys::Array, web_sys::ResizeObserver)>::new(
|
cfg_if! { if #[cfg(feature = "ssr")] {
|
||||||
move |entries: js_sys::Array, observer| {
|
UseResizeObserverReturn {
|
||||||
callback(
|
is_supported: Signal::derive(|| true),
|
||||||
entries
|
stop: || {}
|
||||||
.to_vec()
|
|
||||||
.into_iter()
|
|
||||||
.map(|v| v.unchecked_into::<web_sys::ResizeObserverEntry>())
|
|
||||||
.collect(),
|
|
||||||
observer,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.into_js_value();
|
|
||||||
|
|
||||||
let observer: Rc<RefCell<Option<web_sys::ResizeObserver>>> = Rc::new(RefCell::new(None));
|
|
||||||
|
|
||||||
let is_supported = use_supported(|| JsValue::from("ResizeObserver").js_in(&window()));
|
|
||||||
|
|
||||||
let cleanup = {
|
|
||||||
let observer = Rc::clone(&observer);
|
|
||||||
|
|
||||||
move || {
|
|
||||||
let mut observer = observer.borrow_mut();
|
|
||||||
if let Some(o) = observer.as_ref() {
|
|
||||||
o.disconnect();
|
|
||||||
*observer = None;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
} else {
|
||||||
|
let closure_js = Closure::<dyn FnMut(js_sys::Array, web_sys::ResizeObserver)>::new(
|
||||||
let targets = (target).into();
|
move |entries: js_sys::Array, observer| {
|
||||||
|
callback(
|
||||||
let stop_watch = {
|
entries
|
||||||
let cleanup = cleanup.clone();
|
.to_vec()
|
||||||
|
.into_iter()
|
||||||
watch(
|
.map(|v| v.unchecked_into::<web_sys::ResizeObserverEntry>())
|
||||||
move || targets.get(),
|
.collect(),
|
||||||
move |targets, _, _| {
|
observer,
|
||||||
cleanup();
|
);
|
||||||
|
|
||||||
if is_supported.get() && !targets.is_empty() {
|
|
||||||
let obs =
|
|
||||||
web_sys::ResizeObserver::new(closure_js.clone().as_ref().unchecked_ref())
|
|
||||||
.expect("failed to create ResizeObserver");
|
|
||||||
|
|
||||||
for target in targets.iter().flatten() {
|
|
||||||
let target: web_sys::Element = target.clone().into();
|
|
||||||
obs.observe_with_options(&target, &options.clone().into());
|
|
||||||
}
|
|
||||||
|
|
||||||
observer.replace(Some(obs));
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
false,
|
|
||||||
)
|
)
|
||||||
};
|
.into_js_value();
|
||||||
|
|
||||||
let stop = move || {
|
let observer: Rc<RefCell<Option<web_sys::ResizeObserver>>> = Rc::new(RefCell::new(None));
|
||||||
cleanup();
|
|
||||||
stop_watch();
|
|
||||||
};
|
|
||||||
|
|
||||||
on_cleanup(stop.clone());
|
let is_supported = use_supported(|| JsValue::from("ResizeObserver").js_in(&window()));
|
||||||
|
|
||||||
UseResizeObserverReturn { is_supported, stop }
|
let cleanup = {
|
||||||
|
let observer = Rc::clone(&observer);
|
||||||
|
|
||||||
|
move || {
|
||||||
|
let mut observer = observer.borrow_mut();
|
||||||
|
if let Some(o) = observer.as_ref() {
|
||||||
|
o.disconnect();
|
||||||
|
*observer = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let targets = (target).into();
|
||||||
|
|
||||||
|
let stop_watch = {
|
||||||
|
let cleanup = cleanup.clone();
|
||||||
|
|
||||||
|
watch(
|
||||||
|
move || targets.get(),
|
||||||
|
move |targets, _, _| {
|
||||||
|
cleanup();
|
||||||
|
|
||||||
|
if is_supported.get() && !targets.is_empty() {
|
||||||
|
let obs =
|
||||||
|
web_sys::ResizeObserver::new(closure_js.clone().as_ref().unchecked_ref())
|
||||||
|
.expect("failed to create ResizeObserver");
|
||||||
|
|
||||||
|
for target in targets.iter().flatten() {
|
||||||
|
let target: web_sys::Element = target.clone().into();
|
||||||
|
obs.observe_with_options(&target, &options.clone().into());
|
||||||
|
}
|
||||||
|
|
||||||
|
observer.replace(Some(obs));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let stop = move || {
|
||||||
|
cleanup();
|
||||||
|
stop_watch();
|
||||||
|
};
|
||||||
|
|
||||||
|
on_cleanup(stop.clone());
|
||||||
|
|
||||||
|
UseResizeObserverReturn { is_supported, stop }
|
||||||
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Options for [`use_resize_observer_with_options`].
|
/// Options for [`use_resize_observer_with_options`].
|
||||||
#[derive(DefaultBuilder, Clone)]
|
#[derive(DefaultBuilder, Clone, Default)]
|
||||||
pub struct UseResizeObserverOptions {
|
pub struct UseResizeObserverOptions {
|
||||||
/// The box that is used to determine the dimensions of the target. Defaults to `ContentBox`.
|
/// The box that is used to determine the dimensions of the target. Defaults to `ContentBox`.
|
||||||
pub box_: web_sys::ResizeObserverBoxOptions,
|
#[builder(into)]
|
||||||
}
|
pub box_: Option<web_sys::ResizeObserverBoxOptions>,
|
||||||
|
|
||||||
impl Default for UseResizeObserverOptions {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
box_: web_sys::ResizeObserverBoxOptions::ContentBox,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<UseResizeObserverOptions> for web_sys::ResizeObserverOptions {
|
impl From<UseResizeObserverOptions> for web_sys::ResizeObserverOptions {
|
||||||
fn from(val: UseResizeObserverOptions) -> Self {
|
fn from(val: UseResizeObserverOptions) -> Self {
|
||||||
let mut options = web_sys::ResizeObserverOptions::new();
|
let mut options = web_sys::ResizeObserverOptions::new();
|
||||||
options.box_(val.box_);
|
options.box_(
|
||||||
|
val.box_
|
||||||
|
.unwrap_or(web_sys::ResizeObserverBoxOptions::ContentBox),
|
||||||
|
);
|
||||||
options
|
options
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,25 @@
|
||||||
use crate::core::ElementMaybeSignal;
|
use crate::core::ElementMaybeSignal;
|
||||||
use crate::use_event_listener::use_event_listener_with_options;
|
use crate::UseEventListenerOptions;
|
||||||
use crate::{use_debounce_fn_with_arg, use_throttle_fn_with_arg_and_options, ThrottleOptions};
|
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::ev::scrollend;
|
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
||||||
|
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 leptos::ev::scrollend;
|
||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
|
|
||||||
|
/// We have to check if the scroll amount is close enough to some threshold in order to
|
||||||
|
/// more accurately calculate arrivedState. This is because scrollTop/scrollLeft are non-rounded
|
||||||
|
/// numbers, while scrollHeight/scrollWidth and clientHeight/clientWidth are rounded.
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#determine_if_an_element_has_been_totally_scrolled
|
||||||
|
const ARRIVED_STATE_THRESHOLD_PIXELS: f64 = 1.0;
|
||||||
|
}}
|
||||||
|
|
||||||
/// Reactive scroll position and state.
|
/// Reactive scroll position and state.
|
||||||
///
|
///
|
||||||
/// ## Demo
|
/// ## Demo
|
||||||
|
@ -172,7 +184,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Version of [`use_scroll`] with options. See [`use_scroll`] for how to use.
|
/// Version of [`use_scroll`] with options. See [`use_scroll`] for how to use.
|
||||||
#[allow(unused_variables)]
|
#[cfg_attr(feature = "ssr", allow(unused_variables))]
|
||||||
pub fn use_scroll_with_options<El, T>(element: El, options: UseScrollOptions) -> UseScrollReturn
|
pub fn use_scroll_with_options<El, T>(element: El, options: UseScrollOptions) -> UseScrollReturn
|
||||||
where
|
where
|
||||||
El: Clone,
|
El: Clone,
|
||||||
|
@ -377,7 +389,7 @@ where
|
||||||
target,
|
target,
|
||||||
ev::scroll,
|
ev::scroll,
|
||||||
handler,
|
handler,
|
||||||
options.event_listener_options.clone().unwrap_or_default(),
|
options.event_listener_options,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let _ = use_event_listener_with_options::<
|
let _ = use_event_listener_with_options::<
|
||||||
|
@ -389,7 +401,7 @@ where
|
||||||
target,
|
target,
|
||||||
ev::scroll,
|
ev::scroll,
|
||||||
on_scroll_handler,
|
on_scroll_handler,
|
||||||
options.event_listener_options.clone().unwrap_or_default(),
|
options.event_listener_options,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -402,7 +414,7 @@ where
|
||||||
target,
|
target,
|
||||||
scrollend,
|
scrollend,
|
||||||
on_scroll_end,
|
on_scroll_end,
|
||||||
options.event_listener_options.unwrap_or_default(),
|
options.event_listener_options,
|
||||||
);
|
);
|
||||||
|
|
||||||
let measure = Box::new(move || {
|
let measure = Box::new(move || {
|
||||||
|
@ -426,15 +438,10 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// We have to check if the scroll amount is close enough to some threshold in order to
|
|
||||||
/// more accurately calculate arrivedState. This is because scrollTop/scrollLeft are non-rounded
|
|
||||||
/// numbers, while scrollHeight/scrollWidth and clientHeight/clientWidth are rounded.
|
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#determine_if_an_element_has_been_totally_scrolled
|
|
||||||
const ARRIVED_STATE_THRESHOLD_PIXELS: f64 = 1.0;
|
|
||||||
|
|
||||||
/// Options for [`use_scroll`].
|
/// Options for [`use_scroll`].
|
||||||
#[derive(DefaultBuilder)]
|
#[derive(DefaultBuilder)]
|
||||||
/// Options for [`use_scroll_with_options`].
|
/// Options for [`use_scroll_with_options`].
|
||||||
|
#[cfg_attr(feature = "ssr", allow(dead_code))]
|
||||||
pub struct UseScrollOptions {
|
pub struct UseScrollOptions {
|
||||||
/// Throttle time in milliseconds for the scroll events. Defaults to 0 (disabled).
|
/// Throttle time in milliseconds for the scroll events. Defaults to 0 (disabled).
|
||||||
throttle: f64,
|
throttle: f64,
|
||||||
|
@ -453,8 +460,7 @@ pub struct UseScrollOptions {
|
||||||
on_stop: Rc<dyn Fn(web_sys::Event)>,
|
on_stop: Rc<dyn Fn(web_sys::Event)>,
|
||||||
|
|
||||||
/// Options passed to the `addEventListener("scroll", ...)` call
|
/// Options passed to the `addEventListener("scroll", ...)` call
|
||||||
#[builder(into)]
|
event_listener_options: UseEventListenerOptions,
|
||||||
event_listener_options: Option<web_sys::AddEventListenerOptions>,
|
|
||||||
|
|
||||||
/// When changing the `x` or `y` signals this specifies the scroll behaviour.
|
/// When changing the `x` or `y` signals this specifies the scroll behaviour.
|
||||||
/// Can be `Auto` (= not smooth) or `Smooth`. Defaults to `Auto`.
|
/// Can be `Auto` (= not smooth) or `Smooth`. Defaults to `Auto`.
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
use crate::{use_document, UseDocument};
|
use crate::{use_document, UseDocument};
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use leptos::*;
|
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "ssr"))]
|
||||||
|
use leptos::*;
|
||||||
|
|
||||||
/// SSR safe `window()`.
|
/// SSR safe `window()`.
|
||||||
/// This returns just a new-type wrapper around `Option<Window>`.
|
/// This returns just a new-type wrapper around `Option<Window>`.
|
||||||
/// Calling this amounts to `None` on the server and `Some(Window)` on the client.
|
/// Calling this amounts to `None` on the server and `Some(Window)` on the client.
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
||||||
|
|
||||||
use crate::use_event_listener_with_options;
|
use crate::{use_event_listener_with_options, use_window, UseEventListenerOptions};
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use leptos::ev::scroll;
|
use leptos::ev::scroll;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use web_sys::AddEventListenerOptions;
|
|
||||||
|
|
||||||
/// Reactive window scroll.
|
/// Reactive window scroll.
|
||||||
///
|
///
|
||||||
|
@ -40,21 +39,17 @@ pub fn use_window_scroll() -> (Signal<f64>, Signal<f64>) {
|
||||||
let (x, set_x) = create_signal(initial_x);
|
let (x, set_x) = create_signal(initial_x);
|
||||||
let (y, set_y) = create_signal(initial_y);
|
let (y, set_y) = create_signal(initial_y);
|
||||||
|
|
||||||
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
let _ = use_event_listener_with_options(
|
||||||
let mut options = AddEventListenerOptions::new();
|
use_window(),
|
||||||
options.capture(false);
|
scroll,
|
||||||
options.passive(true);
|
move |_| {
|
||||||
|
set_x.set(window().scroll_x().unwrap_or_default());
|
||||||
let _ = use_event_listener_with_options(
|
set_y.set(window().scroll_y().unwrap_or_default());
|
||||||
window(),
|
},
|
||||||
scroll,
|
UseEventListenerOptions::default()
|
||||||
move |_| {
|
.capture(false)
|
||||||
set_x.set(window().scroll_x().unwrap_or_default());
|
.passive(true),
|
||||||
set_y.set(window().scroll_y().unwrap_or_default());
|
);
|
||||||
},
|
|
||||||
options,
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
|
|
||||||
(x.into(), y.into())
|
(x.into(), y.into())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue