mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-22 16:49:22 -05:00
added use_element_size
This commit is contained in:
parent
cc89683d95
commit
5e076b1c9d
25 changed files with 697 additions and 6 deletions
2
.idea/leptos-use.iml
generated
2
.idea/leptos-use.iml
generated
|
@ -16,6 +16,7 @@
|
|||
<sourceFolder url="file://$MODULE_DIR$/examples/use_resize_observer/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/examples/use_mouse/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/examples/use_floor/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/examples/use_element_size/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/examples/use_event_listener/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/docs/book/book" />
|
||||
|
@ -28,6 +29,7 @@
|
|||
<excludeFolder url="file://$MODULE_DIR$/examples/use_resize_observer/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/examples/use_mouse/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/examples/use_floor/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/examples/use_element_size/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
# Changelog
|
||||
|
||||
## 0.1.7
|
||||
|
||||
#### New Function
|
||||
- `use_element_size`
|
||||
|
||||
## 0.1.6
|
||||
|
||||
- Fixed documentation so all feature are documented
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
# Elements
|
||||
|
||||
- [use_element_size](elements/use_element_size.md)
|
||||
- [use_resize_observer](elements/use_resize_observer.md)
|
||||
|
||||
# Browser
|
||||
|
|
3
docs/book/src/elements/use_element_size.md
Normal file
3
docs/book/src/elements/use_element_size.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# use_element_size
|
||||
|
||||
<!-- cmdrun python3 ../extract_doc_comment.py use_element_size -->
|
2
examples/use_element_size/.cargo/config.toml
Normal file
2
examples/use_element_size/.cargo/config.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
[build]
|
||||
rustflags = ["--cfg=web_sys_unstable_apis"]
|
16
examples/use_element_size/Cargo.toml
Normal file
16
examples/use_element_size/Cargo.toml
Normal file
|
@ -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"
|
23
examples/use_element_size/README.md
Normal file
23
examples/use_element_size/README.md
Normal file
|
@ -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
|
||||
```
|
2
examples/use_element_size/Trunk.toml
Normal file
2
examples/use_element_size/Trunk.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
[build]
|
||||
public_url = "/demo/"
|
7
examples/use_element_size/index.html
Normal file
7
examples/use_element_size/index.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link data-trunk rel="css" href="style/output.css">
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
3
examples/use_element_size/input.css
Normal file
3
examples/use_element_size/input.css
Normal file
|
@ -0,0 +1,3 @@
|
|||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
2
examples/use_element_size/rust-toolchain.toml
Normal file
2
examples/use_element_size/rust-toolchain.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
[toolchain]
|
||||
channel = "nightly"
|
31
examples/use_element_size/src/main.rs
Normal file
31
examples/use_element_size/src/main.rs
Normal file
|
@ -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,
|
||||
<Note class="mb-2">"Resize the box to see changes"</Note>
|
||||
<textarea
|
||||
node_ref=el
|
||||
readonly
|
||||
class="resize rounded-md p-4 w-[200px] h-[100px] text-2xl leading-10"
|
||||
prop:value=text
|
||||
/>
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
_ = console_log::init_with_level(log::Level::Debug);
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
mount_to(demo_or_body(), |cx| {
|
||||
view! { cx, <Demo /> }
|
||||
})
|
||||
}
|
318
examples/use_element_size/style/output.css
Normal file
318
examples/use_element_size/style/output.css
Normal file
|
@ -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));
|
||||
}
|
||||
}
|
15
examples/use_element_size/tailwind.config.js
Normal file
15
examples/use_element_size/tailwind.config.js
Normal file
|
@ -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'),
|
||||
],
|
||||
}
|
|
@ -18,7 +18,12 @@ fn Demo(cx: Scope) -> impl IntoView {
|
|||
|
||||
view! { cx,
|
||||
<Note class="mb-2">"Resize the box to see changes"</Note>
|
||||
<textarea node_ref=el class="resize rounded-md p-4 w-[200px] h-[100px] text-2xl leading-10" disabled prop:value=text />
|
||||
<textarea
|
||||
node_ref=el
|
||||
readonly
|
||||
class="resize rounded-md p-4 w-[200px] h-[100px] text-2xl leading-10"
|
||||
prop:value=text
|
||||
/>
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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::*;
|
5
src/core/size.rs
Normal file
5
src/core/size.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
#[derive(Copy, Clone, Default, PartialEq)]
|
||||
pub struct Size {
|
||||
pub width: f64,
|
||||
pub height: f64,
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
#![doc(cfg(feature = "math"))]
|
||||
//! Collection of reactive math functions
|
||||
|
||||
mod shared;
|
||||
mod use_floor;
|
||||
mod use_max;
|
||||
|
|
206
src/use_element_size.rs
Normal file
206
src/use_element_size.rs
Normal file
|
@ -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,
|
||||
/// <div node_ref=el>
|
||||
/// "Width: " {width}
|
||||
/// "Height: " {height}
|
||||
/// </div>
|
||||
/// }
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// ## See also
|
||||
///
|
||||
/// - [`use_resize_observer`]
|
||||
pub fn use_element_size<El, T>(cx: Scope, target: El) -> UseElementSizeReturn
|
||||
where
|
||||
El: Clone,
|
||||
(Scope, El): Into<ElementMaybeSignal<T, web_sys::Element>>,
|
||||
T: Into<web_sys::Element> + 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<El, T>(
|
||||
cx: Scope,
|
||||
target: El,
|
||||
options: UseElementSizeOptions,
|
||||
) -> UseElementSizeReturn
|
||||
where
|
||||
El: Clone,
|
||||
(Scope, El): Into<ElementMaybeSignal<T, web_sys::Element>>,
|
||||
T: Into<web_sys::Element> + 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::<BoxSize>().inline_size()
|
||||
}));
|
||||
set_height(format_box_size.iter().fold(0.0, |acc, v| {
|
||||
acc + v.as_ref().clone().unchecked_into::<BoxSize>().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<UseElementSizeOptions> 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<f64>,
|
||||
/// The height of the element.
|
||||
pub height: Signal<f64>,
|
||||
}
|
||||
|
||||
#[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;
|
||||
}
|
|
@ -198,6 +198,7 @@ where
|
|||
}
|
||||
|
||||
#[derive(DefaultBuilder)]
|
||||
/// Options for [`use_mouse_with_options`].
|
||||
pub struct UseMouseOptions<El, T, Ex>
|
||||
where
|
||||
El: Clone,
|
||||
|
|
|
@ -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<El, T, F>(
|
||||
cx: Scope,
|
||||
target: El, // TODO : multiple elements?
|
||||
|
@ -48,15 +54,15 @@ where
|
|||
T: Into<web_sys::Element> + Clone + 'static,
|
||||
F: FnMut(Vec<web_sys::ResizeObserverEntry>, 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<El, T, F>(
|
||||
cx: Scope,
|
||||
target: El, // TODO : multiple elements?
|
||||
callback: F,
|
||||
options: web_sys::ResizeObserverOptions,
|
||||
options: UseResizeObserverOptions,
|
||||
) -> UseResizeObserverReturn
|
||||
where
|
||||
(Scope, El): Into<ElementMaybeSignal<T, web_sys::Element>>,
|
||||
|
@ -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<web_sys::ResizeObserverOptions> 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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue