diff --git a/.idea/leptos-use.iml b/.idea/leptos-use.iml
index b9141b0..c55e122 100644
--- a/.idea/leptos-use.iml
+++ b/.idea/leptos-use.iml
@@ -16,6 +16,7 @@
+
@@ -28,6 +29,7 @@
+
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 346e1c6..997602c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog
+## 0.1.7
+
+#### New Function
+- `use_element_size`
+
## 0.1.6
- Fixed documentation so all feature are documented
diff --git a/Cargo.toml b/Cargo.toml
index fa2cd4a..c260d7a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "leptos-use"
-version = "0.1.6"
+version = "0.1.7"
edition = "2021"
authors = ["Marc-Stefan Cassola"]
categories = ["gui", "web-programming"]
@@ -10,6 +10,7 @@ keywords = ["leptos", "utilities"]
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/Synphonyte/leptos-use"
+homepage = "https://leptos-use.rs"
[dependencies]
@@ -24,6 +25,7 @@ version = "0.3.63"
features = [
"CssStyleDeclaration",
"DomRectReadOnly",
+ "Element",
"MouseEvent",
"Navigator",
"ResizeObserver",
diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md
index e4ce538..a00a283 100644
--- a/docs/book/src/SUMMARY.md
+++ b/docs/book/src/SUMMARY.md
@@ -7,6 +7,7 @@
# Elements
+- [use_element_size](elements/use_element_size.md)
- [use_resize_observer](elements/use_resize_observer.md)
# Browser
diff --git a/docs/book/src/elements/use_element_size.md b/docs/book/src/elements/use_element_size.md
new file mode 100644
index 0000000..b59e781
--- /dev/null
+++ b/docs/book/src/elements/use_element_size.md
@@ -0,0 +1,3 @@
+# use_element_size
+
+
diff --git a/examples/use_element_size/.cargo/config.toml b/examples/use_element_size/.cargo/config.toml
new file mode 100644
index 0000000..8467175
--- /dev/null
+++ b/examples/use_element_size/.cargo/config.toml
@@ -0,0 +1,2 @@
+[build]
+rustflags = ["--cfg=web_sys_unstable_apis"]
diff --git a/examples/use_element_size/Cargo.toml b/examples/use_element_size/Cargo.toml
new file mode 100644
index 0000000..e249885
--- /dev/null
+++ b/examples/use_element_size/Cargo.toml
@@ -0,0 +1,16 @@
+[package]
+name = "use_element_size"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+leptos = "0.3"
+console_error_panic_hook = "0.1"
+console_log = "1"
+log = "0.4"
+leptos-use = { path = "../..", features = ["docs"] }
+web-sys = "0.3"
+
+[dev-dependencies]
+wasm-bindgen = "0.2"
+wasm-bindgen-test = "0.3.0"
diff --git a/examples/use_element_size/README.md b/examples/use_element_size/README.md
new file mode 100644
index 0000000..9f63d84
--- /dev/null
+++ b/examples/use_element_size/README.md
@@ -0,0 +1,23 @@
+A simple example for `use_element_size`.
+
+If you don't have it installed already, install [Trunk](https://trunkrs.dev/) and [Tailwind](https://tailwindcss.com/docs/installation)
+as well as the nightly toolchain for Rust and the wasm32-unknown-unknown target:
+
+```bash
+cargo install trunk
+npm install -D tailwindcss @tailwindcss/forms
+rustup toolchain install nightly
+rustup target add wasm32-unknown-unknown
+```
+
+Then, open two terminals. In the first one, run:
+
+```
+npx tailwindcss -i ./input.css -o ./style/output.css --watch
+```
+
+In the second one, run:
+
+```bash
+trunk serve --open
+```
\ No newline at end of file
diff --git a/examples/use_element_size/Trunk.toml b/examples/use_element_size/Trunk.toml
new file mode 100644
index 0000000..3e4be08
--- /dev/null
+++ b/examples/use_element_size/Trunk.toml
@@ -0,0 +1,2 @@
+[build]
+public_url = "/demo/"
\ No newline at end of file
diff --git a/examples/use_element_size/index.html b/examples/use_element_size/index.html
new file mode 100644
index 0000000..ae249a6
--- /dev/null
+++ b/examples/use_element_size/index.html
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/examples/use_element_size/input.css b/examples/use_element_size/input.css
new file mode 100644
index 0000000..bd6213e
--- /dev/null
+++ b/examples/use_element_size/input.css
@@ -0,0 +1,3 @@
+@tailwind base;
+@tailwind components;
+@tailwind utilities;
\ No newline at end of file
diff --git a/examples/use_element_size/rust-toolchain.toml b/examples/use_element_size/rust-toolchain.toml
new file mode 100644
index 0000000..271800c
--- /dev/null
+++ b/examples/use_element_size/rust-toolchain.toml
@@ -0,0 +1,2 @@
+[toolchain]
+channel = "nightly"
\ No newline at end of file
diff --git a/examples/use_element_size/src/main.rs b/examples/use_element_size/src/main.rs
new file mode 100644
index 0000000..14a18dc
--- /dev/null
+++ b/examples/use_element_size/src/main.rs
@@ -0,0 +1,31 @@
+use leptos::*;
+use leptos_use::docs::{demo_or_body, Note};
+use leptos_use::{use_element_size, UseElementSizeReturn};
+
+#[component]
+fn Demo(cx: Scope) -> impl IntoView {
+ let el = create_node_ref(cx);
+
+ let UseElementSizeReturn { width, height } = use_element_size(cx, el);
+
+ let text = move || format!("width: {}\nheight: {}", width.get(), height.get());
+
+ view! { cx,
+ "Resize the box to see changes"
+
+ }
+}
+
+fn main() {
+ _ = console_log::init_with_level(log::Level::Debug);
+ console_error_panic_hook::set_once();
+
+ mount_to(demo_or_body(), |cx| {
+ view! { cx, }
+ })
+}
diff --git a/examples/use_element_size/style/output.css b/examples/use_element_size/style/output.css
new file mode 100644
index 0000000..ddec9cf
--- /dev/null
+++ b/examples/use_element_size/style/output.css
@@ -0,0 +1,318 @@
+[type='text'],[type='email'],[type='url'],[type='password'],[type='number'],[type='date'],[type='datetime-local'],[type='month'],[type='search'],[type='tel'],[type='time'],[type='week'],[multiple],textarea,select {
+ -webkit-appearance: none;
+ -moz-appearance: none;
+ appearance: none;
+ background-color: #fff;
+ border-color: #6b7280;
+ border-width: 1px;
+ border-radius: 0px;
+ padding-top: 0.5rem;
+ padding-right: 0.75rem;
+ padding-bottom: 0.5rem;
+ padding-left: 0.75rem;
+ font-size: 1rem;
+ line-height: 1.5rem;
+ --tw-shadow: 0 0 #0000;
+}
+
+[type='text']:focus, [type='email']:focus, [type='url']:focus, [type='password']:focus, [type='number']:focus, [type='date']:focus, [type='datetime-local']:focus, [type='month']:focus, [type='search']:focus, [type='tel']:focus, [type='time']:focus, [type='week']:focus, [multiple]:focus, textarea:focus, select:focus {
+ outline: 2px solid transparent;
+ outline-offset: 2px;
+ --tw-ring-inset: var(--tw-empty,/*!*/ /*!*/);
+ --tw-ring-offset-width: 0px;
+ --tw-ring-offset-color: #fff;
+ --tw-ring-color: #2563eb;
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow);
+ border-color: #2563eb;
+}
+
+input::-moz-placeholder, textarea::-moz-placeholder {
+ color: #6b7280;
+ opacity: 1;
+}
+
+input::placeholder,textarea::placeholder {
+ color: #6b7280;
+ opacity: 1;
+}
+
+::-webkit-datetime-edit-fields-wrapper {
+ padding: 0;
+}
+
+::-webkit-date-and-time-value {
+ min-height: 1.5em;
+}
+
+::-webkit-datetime-edit,::-webkit-datetime-edit-year-field,::-webkit-datetime-edit-month-field,::-webkit-datetime-edit-day-field,::-webkit-datetime-edit-hour-field,::-webkit-datetime-edit-minute-field,::-webkit-datetime-edit-second-field,::-webkit-datetime-edit-millisecond-field,::-webkit-datetime-edit-meridiem-field {
+ padding-top: 0;
+ padding-bottom: 0;
+}
+
+select {
+ background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
+ background-position: right 0.5rem center;
+ background-repeat: no-repeat;
+ background-size: 1.5em 1.5em;
+ padding-right: 2.5rem;
+ -webkit-print-color-adjust: exact;
+ print-color-adjust: exact;
+}
+
+[multiple] {
+ background-image: initial;
+ background-position: initial;
+ background-repeat: unset;
+ background-size: initial;
+ padding-right: 0.75rem;
+ -webkit-print-color-adjust: unset;
+ print-color-adjust: unset;
+}
+
+[type='checkbox'],[type='radio'] {
+ -webkit-appearance: none;
+ -moz-appearance: none;
+ appearance: none;
+ padding: 0;
+ -webkit-print-color-adjust: exact;
+ print-color-adjust: exact;
+ display: inline-block;
+ vertical-align: middle;
+ background-origin: border-box;
+ -webkit-user-select: none;
+ -moz-user-select: none;
+ user-select: none;
+ flex-shrink: 0;
+ height: 1rem;
+ width: 1rem;
+ color: #2563eb;
+ background-color: #fff;
+ border-color: #6b7280;
+ border-width: 1px;
+ --tw-shadow: 0 0 #0000;
+}
+
+[type='checkbox'] {
+ border-radius: 0px;
+}
+
+[type='radio'] {
+ border-radius: 100%;
+}
+
+[type='checkbox']:focus,[type='radio']:focus {
+ outline: 2px solid transparent;
+ outline-offset: 2px;
+ --tw-ring-inset: var(--tw-empty,/*!*/ /*!*/);
+ --tw-ring-offset-width: 2px;
+ --tw-ring-offset-color: #fff;
+ --tw-ring-color: #2563eb;
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(2px + var(--tw-ring-offset-width)) var(--tw-ring-color);
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow);
+}
+
+[type='checkbox']:checked,[type='radio']:checked {
+ border-color: transparent;
+ background-color: currentColor;
+ background-size: 100% 100%;
+ background-position: center;
+ background-repeat: no-repeat;
+}
+
+[type='checkbox']:checked {
+ background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3cpath d='M12.207 4.793a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0l-2-2a1 1 0 011.414-1.414L6.5 9.086l4.293-4.293a1 1 0 011.414 0z'/%3e%3c/svg%3e");
+}
+
+[type='radio']:checked {
+ background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3ccircle cx='8' cy='8' r='3'/%3e%3c/svg%3e");
+}
+
+[type='checkbox']:checked:hover,[type='checkbox']:checked:focus,[type='radio']:checked:hover,[type='radio']:checked:focus {
+ border-color: transparent;
+ background-color: currentColor;
+}
+
+[type='checkbox']:indeterminate {
+ background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3e%3cpath stroke='white' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M4 8h8'/%3e%3c/svg%3e");
+ border-color: transparent;
+ background-color: currentColor;
+ background-size: 100% 100%;
+ background-position: center;
+ background-repeat: no-repeat;
+}
+
+[type='checkbox']:indeterminate:hover,[type='checkbox']:indeterminate:focus {
+ border-color: transparent;
+ background-color: currentColor;
+}
+
+[type='file'] {
+ background: unset;
+ border-color: inherit;
+ border-width: 0;
+ border-radius: 0;
+ padding: 0;
+ font-size: unset;
+ line-height: inherit;
+}
+
+[type='file']:focus {
+ outline: 1px solid ButtonText;
+ outline: 1px auto -webkit-focus-ring-color;
+}
+
+*, ::before, ::after {
+ --tw-border-spacing-x: 0;
+ --tw-border-spacing-y: 0;
+ --tw-translate-x: 0;
+ --tw-translate-y: 0;
+ --tw-rotate: 0;
+ --tw-skew-x: 0;
+ --tw-skew-y: 0;
+ --tw-scale-x: 1;
+ --tw-scale-y: 1;
+ --tw-pan-x: ;
+ --tw-pan-y: ;
+ --tw-pinch-zoom: ;
+ --tw-scroll-snap-strictness: proximity;
+ --tw-gradient-from-position: ;
+ --tw-gradient-via-position: ;
+ --tw-gradient-to-position: ;
+ --tw-ordinal: ;
+ --tw-slashed-zero: ;
+ --tw-numeric-figure: ;
+ --tw-numeric-spacing: ;
+ --tw-numeric-fraction: ;
+ --tw-ring-inset: ;
+ --tw-ring-offset-width: 0px;
+ --tw-ring-offset-color: #fff;
+ --tw-ring-color: rgb(59 130 246 / 0.5);
+ --tw-ring-offset-shadow: 0 0 #0000;
+ --tw-ring-shadow: 0 0 #0000;
+ --tw-shadow: 0 0 #0000;
+ --tw-shadow-colored: 0 0 #0000;
+ --tw-blur: ;
+ --tw-brightness: ;
+ --tw-contrast: ;
+ --tw-grayscale: ;
+ --tw-hue-rotate: ;
+ --tw-invert: ;
+ --tw-saturate: ;
+ --tw-sepia: ;
+ --tw-drop-shadow: ;
+ --tw-backdrop-blur: ;
+ --tw-backdrop-brightness: ;
+ --tw-backdrop-contrast: ;
+ --tw-backdrop-grayscale: ;
+ --tw-backdrop-hue-rotate: ;
+ --tw-backdrop-invert: ;
+ --tw-backdrop-opacity: ;
+ --tw-backdrop-saturate: ;
+ --tw-backdrop-sepia: ;
+}
+
+::backdrop {
+ --tw-border-spacing-x: 0;
+ --tw-border-spacing-y: 0;
+ --tw-translate-x: 0;
+ --tw-translate-y: 0;
+ --tw-rotate: 0;
+ --tw-skew-x: 0;
+ --tw-skew-y: 0;
+ --tw-scale-x: 1;
+ --tw-scale-y: 1;
+ --tw-pan-x: ;
+ --tw-pan-y: ;
+ --tw-pinch-zoom: ;
+ --tw-scroll-snap-strictness: proximity;
+ --tw-gradient-from-position: ;
+ --tw-gradient-via-position: ;
+ --tw-gradient-to-position: ;
+ --tw-ordinal: ;
+ --tw-slashed-zero: ;
+ --tw-numeric-figure: ;
+ --tw-numeric-spacing: ;
+ --tw-numeric-fraction: ;
+ --tw-ring-inset: ;
+ --tw-ring-offset-width: 0px;
+ --tw-ring-offset-color: #fff;
+ --tw-ring-color: rgb(59 130 246 / 0.5);
+ --tw-ring-offset-shadow: 0 0 #0000;
+ --tw-ring-shadow: 0 0 #0000;
+ --tw-shadow: 0 0 #0000;
+ --tw-shadow-colored: 0 0 #0000;
+ --tw-blur: ;
+ --tw-brightness: ;
+ --tw-contrast: ;
+ --tw-grayscale: ;
+ --tw-hue-rotate: ;
+ --tw-invert: ;
+ --tw-saturate: ;
+ --tw-sepia: ;
+ --tw-drop-shadow: ;
+ --tw-backdrop-blur: ;
+ --tw-backdrop-brightness: ;
+ --tw-backdrop-contrast: ;
+ --tw-backdrop-grayscale: ;
+ --tw-backdrop-hue-rotate: ;
+ --tw-backdrop-invert: ;
+ --tw-backdrop-opacity: ;
+ --tw-backdrop-saturate: ;
+ --tw-backdrop-sepia: ;
+}
+
+.mb-2 {
+ margin-bottom: 0.5rem;
+}
+
+.h-\[100px\] {
+ height: 100px;
+}
+
+.w-\[200px\] {
+ width: 200px;
+}
+
+.resize {
+ resize: both;
+}
+
+.rounded-md {
+ border-radius: 0.375rem;
+}
+
+.p-4 {
+ padding: 1rem;
+}
+
+.text-2xl {
+ font-size: 1.5rem;
+ line-height: 2rem;
+}
+
+.leading-10 {
+ line-height: 2.5rem;
+}
+
+.text-\[--brand-color\] {
+ color: var(--brand-color);
+}
+
+.text-green-600 {
+ --tw-text-opacity: 1;
+ color: rgb(22 163 74 / var(--tw-text-opacity));
+}
+
+.opacity-75 {
+ opacity: 0.75;
+}
+
+@media (prefers-color-scheme: dark) {
+ .dark\:text-green-500 {
+ --tw-text-opacity: 1;
+ color: rgb(34 197 94 / var(--tw-text-opacity));
+ }
+}
\ No newline at end of file
diff --git a/examples/use_element_size/tailwind.config.js b/examples/use_element_size/tailwind.config.js
new file mode 100644
index 0000000..bc09f5e
--- /dev/null
+++ b/examples/use_element_size/tailwind.config.js
@@ -0,0 +1,15 @@
+/** @type {import('tailwindcss').Config} */
+module.exports = {
+ content: {
+ files: ["*.html", "./src/**/*.rs", "../../src/docs/**/*.rs"],
+ },
+ theme: {
+ extend: {},
+ },
+ corePlugins: {
+ preflight: false,
+ },
+ plugins: [
+ require('@tailwindcss/forms'),
+ ],
+}
\ No newline at end of file
diff --git a/examples/use_resize_observer/src/main.rs b/examples/use_resize_observer/src/main.rs
index 0bb9ab8..8d66046 100644
--- a/examples/use_resize_observer/src/main.rs
+++ b/examples/use_resize_observer/src/main.rs
@@ -18,7 +18,12 @@ fn Demo(cx: Scope) -> impl IntoView {
view! { cx,
"Resize the box to see changes"
-
+
}
}
diff --git a/src/core/mod.rs b/src/core/mod.rs
index e632f20..af2d1f6 100644
--- a/src/core/mod.rs
+++ b/src/core/mod.rs
@@ -1,5 +1,7 @@
mod event_target_maybe_signal;
mod position;
+mod size;
pub use event_target_maybe_signal::*;
pub use position::*;
+pub use size::*;
\ No newline at end of file
diff --git a/src/core/size.rs b/src/core/size.rs
new file mode 100644
index 0000000..2a6d6e5
--- /dev/null
+++ b/src/core/size.rs
@@ -0,0 +1,5 @@
+#[derive(Copy, Clone, Default, PartialEq)]
+pub struct Size {
+ pub width: f64,
+ pub height: f64,
+}
diff --git a/src/docs/mod.rs b/src/docs/mod.rs
index f205682..44dca27 100644
--- a/src/docs/mod.rs
+++ b/src/docs/mod.rs
@@ -1,3 +1,6 @@
+#![doc(cfg(feature = "docs"))]
+//! Collection of documentation related utilities. Used extensively in the examples.
+
mod boolean_display;
mod demo;
mod note;
diff --git a/src/lib.rs b/src/lib.rs
index d2f06db..dc7589a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -7,6 +7,8 @@ pub mod docs;
#[cfg(feature = "math")]
pub mod math;
mod use_debounce_fn;
+#[cfg(web_sys_unstable_apis)]
+mod use_element_size;
mod use_event_listener;
mod use_mouse;
#[cfg(web_sys_unstable_apis)]
@@ -18,6 +20,8 @@ pub mod utils;
mod watch;
pub use use_debounce_fn::*;
+#[cfg(web_sys_unstable_apis)]
+pub use use_element_size::*;
pub use use_event_listener::*;
pub use use_mouse::*;
#[cfg(web_sys_unstable_apis)]
diff --git a/src/math/mod.rs b/src/math/mod.rs
index 6a749c5..1d530c4 100644
--- a/src/math/mod.rs
+++ b/src/math/mod.rs
@@ -1,3 +1,6 @@
+#![doc(cfg(feature = "math"))]
+//! Collection of reactive math functions
+
mod shared;
mod use_floor;
mod use_max;
diff --git a/src/use_element_size.rs b/src/use_element_size.rs
new file mode 100644
index 0000000..2615439
--- /dev/null
+++ b/src/use_element_size.rs
@@ -0,0 +1,206 @@
+use crate::core::{ElementMaybeSignal, Size};
+use crate::watch;
+use crate::{use_resize_observer_with_options, UseResizeObserverOptions};
+use default_struct_builder::DefaultBuilder;
+use leptos::*;
+use wasm_bindgen::prelude::wasm_bindgen;
+use wasm_bindgen::JsCast;
+
+/// Reactive size of an HTML element.
+///
+/// > This function requires `--cfg=web_sys_unstable_apis` to be activated as
+/// [described in the wasm-bindgen guide](https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html).
+///
+/// Please refer to [ResizeObserver on MDN](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)
+/// for more details.
+///
+/// ## Demo
+///
+/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_element_size)
+///
+/// ## Usage
+///
+/// ```
+/// # use leptos::*;
+/// # use leptos_use::{use_element_size, UseElementSizeReturn};
+/// #
+/// # #[component]
+/// # fn Demo(cx: Scope) -> impl IntoView {
+/// let el = create_node_ref(cx);
+///
+/// let UseElementSizeReturn { width, height } = use_element_size(cx, el);
+///
+/// view! { cx,
+///
+/// "Width: " {width}
+/// "Height: " {height}
+///
+/// }
+/// # }
+/// ```
+///
+/// ## See also
+///
+/// - [`use_resize_observer`]
+pub fn use_element_size(cx: Scope, target: El) -> UseElementSizeReturn
+where
+ El: Clone,
+ (Scope, El): Into>,
+ T: Into + Clone + 'static,
+{
+ use_element_size_with_options(cx, target, UseElementSizeOptions::default())
+}
+
+/// Version of [`use_element_size`] that takes a `UseElementSizeOptions`. See [`use_element_size`] for how to use.
+pub fn use_element_size_with_options(
+ cx: Scope,
+ target: El,
+ options: UseElementSizeOptions,
+) -> UseElementSizeReturn
+where
+ El: Clone,
+ (Scope, El): Into>,
+ T: Into + Clone + 'static,
+{
+ let window = window();
+ let box_ = options.box_;
+ let initial_size = options.initial_size;
+
+ let targ = (cx, target.clone()).into();
+
+ let t = targ.clone();
+ let is_svg = move || {
+ if let Some(target) = t.get_untracked() {
+ target
+ .into()
+ .namespace_uri()
+ .map(|ns| ns.contains("svg"))
+ .unwrap_or(false)
+ } else {
+ false
+ }
+ };
+
+ let (width, set_width) = create_signal(cx, options.initial_size.width);
+ let (height, set_height) = create_signal(cx, options.initial_size.height);
+
+ let t = targ.clone();
+ let _ = use_resize_observer_with_options(
+ cx,
+ target,
+ move |entries, _| {
+ let entry = &entries[0];
+
+ 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) = t.get() {
+ if let Ok(Some(styles)) = window.get_computed_style(&target.into()) {
+ set_height(
+ styles
+ .get_property_value("height")
+ .map(|v| v.parse().unwrap_or_default())
+ .unwrap_or_default(),
+ );
+ set_width(
+ 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(format_box_size.iter().fold(0.0, |acc, v| {
+ acc + v.as_ref().clone().unchecked_into::().inline_size()
+ }));
+ set_height(format_box_size.iter().fold(0.0, |acc, v| {
+ acc + v.as_ref().clone().unchecked_into::().block_size()
+ }))
+ } else {
+ // fallback
+ set_width(entry.content_rect().width());
+ set_height(entry.content_rect().height())
+ }
+ },
+ options.into(),
+ );
+
+ let _ = watch(
+ cx,
+ move || targ.get(),
+ move |ele, _, _| {
+ if ele.is_some() {
+ set_width(initial_size.width);
+ set_height(initial_size.height);
+ } else {
+ set_width(0.0);
+ set_height(0.0);
+ }
+ },
+ false,
+ );
+
+ UseElementSizeReturn {
+ width: width.into(),
+ height: height.into(),
+ }
+}
+
+#[derive(DefaultBuilder)]
+/// Options for [`use_element_size_with_options`].
+pub struct UseElementSizeOptions {
+ /// Initial size returned before any measurements on the `target` are done. Also the value reported
+ /// at first when the `target` is a signal and changes.
+ initial_size: Size,
+
+ /// The box that is used to determine the dimensions of the target. Defaults to `ContentBox`.
+ pub box_: web_sys::ResizeObserverBoxOptions,
+}
+
+impl Default for UseElementSizeOptions {
+ fn default() -> Self {
+ Self {
+ initial_size: Size::default(),
+ box_: web_sys::ResizeObserverBoxOptions::ContentBox,
+ }
+ }
+}
+
+impl From for UseResizeObserverOptions {
+ fn from(options: UseElementSizeOptions) -> Self {
+ Self::default().box_(options.box_)
+ }
+}
+
+/// The return value of [`use_element_size`].
+pub struct UseElementSizeReturn {
+ /// The width of the element.
+ pub width: Signal,
+ /// The height of the element.
+ pub height: Signal,
+}
+
+#[wasm_bindgen]
+extern "C" {
+ type BoxSize;
+
+ #[wasm_bindgen(method, getter = blockSize)]
+ fn block_size(this: &BoxSize) -> f64;
+
+ #[wasm_bindgen(method, getter = inlineSize)]
+ fn inline_size(this: &BoxSize) -> f64;
+}
diff --git a/src/use_mouse.rs b/src/use_mouse.rs
index 5905af9..d97e88e 100644
--- a/src/use_mouse.rs
+++ b/src/use_mouse.rs
@@ -198,6 +198,7 @@ where
}
#[derive(DefaultBuilder)]
+/// Options for [`use_mouse_with_options`].
pub struct UseMouseOptions
where
El: Clone,
diff --git a/src/use_resize_observer.rs b/src/use_resize_observer.rs
index f34b707..a0de787 100644
--- a/src/use_resize_observer.rs
+++ b/src/use_resize_observer.rs
@@ -1,5 +1,6 @@
use crate::core::ElementMaybeSignal;
use crate::{use_supported, watch};
+use default_struct_builder::DefaultBuilder;
use leptos::*;
use std::cell::RefCell;
use std::rc::Rc;
@@ -36,8 +37,13 @@ use wasm_bindgen::prelude::*;
/// set_text(format!("width: {}\nheight: {}", rect.width(), rect.height()));
/// },
/// );
+/// #
+/// # view! { cx, }
/// # }
/// ```
+/// ## See also
+///
+/// - [`use_element_size`]
pub fn use_resize_observer(
cx: Scope,
target: El, // TODO : multiple elements?
@@ -48,15 +54,15 @@ where
T: Into + Clone + 'static,
F: FnMut(Vec, web_sys::ResizeObserver) + Clone + 'static,
{
- use_resize_observer_with_options(cx, target, callback, web_sys::ResizeObserverOptions::new())
+ use_resize_observer_with_options(cx, target, callback, UseResizeObserverOptions::default())
}
-/// Version of [`use_resize_observer`] that takes a `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.
pub fn use_resize_observer_with_options(
cx: Scope,
target: El, // TODO : multiple elements?
callback: F,
- options: web_sys::ResizeObserverOptions,
+ options: UseResizeObserverOptions,
) -> UseResizeObserverReturn
where
(Scope, El): Into>,
@@ -106,7 +112,7 @@ where
closure.forget();
let target: web_sys::Element = target.clone().into();
- obs.observe_with_options(&target, &options);
+ obs.observe_with_options(&target, &options.clone().into());
observer.replace(Some(obs));
}
@@ -128,6 +134,29 @@ where
}
}
+#[derive(DefaultBuilder, Clone)]
+/// Options for [`use_resize_observer_with_options`].
+pub struct UseResizeObserverOptions {
+ /// The box that is used to determine the dimensions of the target. Defaults to `ContentBox`.
+ pub box_: web_sys::ResizeObserverBoxOptions,
+}
+
+impl Default for UseResizeObserverOptions {
+ fn default() -> Self {
+ Self {
+ box_: web_sys::ResizeObserverBoxOptions::ContentBox,
+ }
+ }
+}
+
+impl Into for UseResizeObserverOptions {
+ fn into(self) -> web_sys::ResizeObserverOptions {
+ let mut options = web_sys::ResizeObserverOptions::new();
+ options.box_(self.box_);
+ options
+ }
+}
+
/// The return value of [`use_resize_observer`].
pub struct UseResizeObserverReturn {
/// Whether the browser supports the ResizeObserver API
diff --git a/src/use_scroll.rs b/src/use_scroll.rs
index fef2525..7ae4e59 100644
--- a/src/use_scroll.rs
+++ b/src/use_scroll.rs
@@ -424,6 +424,7 @@ const ARRIVED_STATE_THRESHOLD_PIXELS: f64 = 1.0;
/// Options for [`use_scroll`].
#[derive(DefaultBuilder)]
+/// Options for [`use_scroll_with_options`].
pub struct UseScrollOptions {
/// Throttle time in milliseconds for the scroll events. Defaults to 0 (disabled).
throttle: f64,