mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-23 00:59:22 -05:00
added use_mouse_in_element
This commit is contained in:
parent
34c913caae
commit
ffff14ea9c
16 changed files with 743 additions and 8 deletions
|
@ -7,9 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
### New Functions 🚀
|
### New Functions 🚀
|
||||||
|
|
||||||
|
- `use_mouse_in_element`
|
||||||
- `use_device_pixel_ratio` (thanks to @mondeja)
|
- `use_device_pixel_ratio` (thanks to @mondeja)
|
||||||
- `use_element_bounding`
|
- `use_element_bounding`
|
||||||
|
|
||||||
|
### Changes 🔥
|
||||||
|
|
||||||
|
- The `UseMouseReturn` signals `x`, `y`, and `source_type` are now of type `Signal<f64>` instead of `ReadSignal<f64>`.
|
||||||
|
|
||||||
## [0.9.0] - 2023-12-06
|
## [0.9.0] - 2023-12-06
|
||||||
|
|
||||||
### New Functions 🚀
|
### New Functions 🚀
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
- [use_element_size](elements/use_element_size.md)
|
- [use_element_size](elements/use_element_size.md)
|
||||||
- [use_element_visibility](elements/use_element_visibility.md)
|
- [use_element_visibility](elements/use_element_visibility.md)
|
||||||
- [use_intersection_observer](elements/use_intersection_observer.md)
|
- [use_intersection_observer](elements/use_intersection_observer.md)
|
||||||
|
- [use_mouse_in_element](elements/use_mouse_in_element.md)
|
||||||
- [use_mutation_observer](elements/use_mutation_observer.md)
|
- [use_mutation_observer](elements/use_mutation_observer.md)
|
||||||
- [use_resize_observer](elements/use_resize_observer.md)
|
- [use_resize_observer](elements/use_resize_observer.md)
|
||||||
- [use_window](elements/use_window.md)
|
- [use_window](elements/use_window.md)
|
||||||
|
|
3
docs/book/src/elements/use_mouse_in_element.md
Normal file
3
docs/book/src/elements/use_mouse_in_element.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# use_mouse_in_element
|
||||||
|
|
||||||
|
<!-- cmdrun python3 ../extract_doc_comment.py use_mouse_in_element -->
|
|
@ -34,6 +34,7 @@ members = [
|
||||||
"use_intl_number_format",
|
"use_intl_number_format",
|
||||||
"use_media_query",
|
"use_media_query",
|
||||||
"use_mouse",
|
"use_mouse",
|
||||||
|
"use_mouse_in_element",
|
||||||
"use_mutation_observer",
|
"use_mutation_observer",
|
||||||
"use_raf_fn",
|
"use_raf_fn",
|
||||||
"use_resize_observer",
|
"use_resize_observer",
|
||||||
|
|
16
examples/use_mouse_in_element/Cargo.toml
Normal file
16
examples/use_mouse_in_element/Cargo.toml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
[package]
|
||||||
|
name = "use_mouse_in_element"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
leptos = { version = "0.5", features = ["nightly", "csr"] }
|
||||||
|
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_mouse_in_element/README.md
Normal file
23
examples/use_mouse_in_element/README.md
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
A simple example for `use_mouse_in_element`.
|
||||||
|
|
||||||
|
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_mouse_in_element/Trunk.toml
Normal file
2
examples/use_mouse_in_element/Trunk.toml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
[build]
|
||||||
|
public_url = "/demo/"
|
7
examples/use_mouse_in_element/index.html
Normal file
7
examples/use_mouse_in_element/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_mouse_in_element/input.css
Normal file
3
examples/use_mouse_in_element/input.css
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
@tailwind base;
|
||||||
|
@tailwind components;
|
||||||
|
@tailwind utilities;
|
2
examples/use_mouse_in_element/rust-toolchain.toml
Normal file
2
examples/use_mouse_in_element/rust-toolchain.toml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
[toolchain]
|
||||||
|
channel = "nightly"
|
53
examples/use_mouse_in_element/src/main.rs
Normal file
53
examples/use_mouse_in_element/src/main.rs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
use leptos::html::Div;
|
||||||
|
use leptos::*;
|
||||||
|
use leptos_use::docs::demo_or_body;
|
||||||
|
use leptos_use::{use_mouse_in_element, UseMouseInElementReturn};
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn Demo() -> impl IntoView {
|
||||||
|
let el = create_node_ref::<Div>();
|
||||||
|
|
||||||
|
let UseMouseInElementReturn {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
source_type,
|
||||||
|
element_x,
|
||||||
|
element_y,
|
||||||
|
element_position_x,
|
||||||
|
element_position_y,
|
||||||
|
element_width,
|
||||||
|
element_height,
|
||||||
|
is_outside,
|
||||||
|
..
|
||||||
|
} = use_mouse_in_element(el);
|
||||||
|
|
||||||
|
view! {
|
||||||
|
<div class="flex gap-4">
|
||||||
|
<div
|
||||||
|
node_ref=el
|
||||||
|
class="el w-40 h-40 bg-gray-400/20 border-rounded flex place-content-center select-none"
|
||||||
|
>
|
||||||
|
<div class="m-auto">Hover me</div>
|
||||||
|
</div>
|
||||||
|
<pre lang="yaml"> x: {x}
|
||||||
|
y: {y}
|
||||||
|
source_type: {move || format!("{:?}", source_type())}
|
||||||
|
element_x: {element_x}
|
||||||
|
element_y: {element_y}
|
||||||
|
element_position_x: {element_position_x}
|
||||||
|
element_position_y: {element_position_y}
|
||||||
|
element_width: {element_width}
|
||||||
|
element_height: {element_height}
|
||||||
|
is_outside: {is_outside}</pre>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
_ = console_log::init_with_level(log::Level::Debug);
|
||||||
|
console_error_panic_hook::set_once();
|
||||||
|
|
||||||
|
mount_to(demo_or_body(), || {
|
||||||
|
view! { <Demo/> }
|
||||||
|
})
|
||||||
|
}
|
352
examples/use_mouse_in_element/style/output.css
Normal file
352
examples/use_mouse_in_element/style/output.css
Normal file
|
@ -0,0 +1,352 @@
|
||||||
|
[type='text'],input:where(:not([type])),[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, input:where(:not([type])):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;
|
||||||
|
text-align: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
::-webkit-datetime-edit {
|
||||||
|
display: inline-flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
::-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],[size]:where(select:not([size="1"])) {
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (forced-colors: active) {
|
||||||
|
[type='checkbox']:checked {
|
||||||
|
-webkit-appearance: auto;
|
||||||
|
-moz-appearance: auto;
|
||||||
|
appearance: auto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[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");
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (forced-colors: active) {
|
||||||
|
[type='radio']:checked {
|
||||||
|
-webkit-appearance: auto;
|
||||||
|
-moz-appearance: auto;
|
||||||
|
appearance: auto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (forced-colors: active) {
|
||||||
|
[type='checkbox']:indeterminate {
|
||||||
|
-webkit-appearance: auto;
|
||||||
|
-moz-appearance: auto;
|
||||||
|
appearance: auto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[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: ;
|
||||||
|
}
|
||||||
|
|
||||||
|
.static {
|
||||||
|
position: static;
|
||||||
|
}
|
||||||
|
|
||||||
|
.m-auto {
|
||||||
|
margin: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.flex {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.h-40 {
|
||||||
|
height: 10rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.w-40 {
|
||||||
|
width: 10rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.select-none {
|
||||||
|
-webkit-user-select: none;
|
||||||
|
-moz-user-select: none;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.place-content-center {
|
||||||
|
place-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gap-4 {
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.bg-gray-400\/20 {
|
||||||
|
background-color: rgb(156 163 175 / 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.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_mouse_in_element/tailwind.config.js
Normal file
15
examples/use_mouse_in_element/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'),
|
||||||
|
],
|
||||||
|
}
|
|
@ -14,6 +14,7 @@ mod is_none;
|
||||||
mod is_ok;
|
mod is_ok;
|
||||||
mod is_some;
|
mod is_some;
|
||||||
mod on_click_outside;
|
mod on_click_outside;
|
||||||
|
mod use_mouse_in_element;
|
||||||
mod signal_debounced;
|
mod signal_debounced;
|
||||||
mod signal_throttled;
|
mod signal_throttled;
|
||||||
mod use_active_element;
|
mod use_active_element;
|
||||||
|
@ -71,6 +72,7 @@ pub use is_none::*;
|
||||||
pub use is_ok::*;
|
pub use is_ok::*;
|
||||||
pub use is_some::*;
|
pub use is_some::*;
|
||||||
pub use on_click_outside::*;
|
pub use on_click_outside::*;
|
||||||
|
pub use use_mouse_in_element::*;
|
||||||
pub use signal_debounced::*;
|
pub use signal_debounced::*;
|
||||||
pub use signal_throttled::*;
|
pub use signal_throttled::*;
|
||||||
pub use use_active_element::*;
|
pub use use_active_element::*;
|
||||||
|
|
|
@ -94,8 +94,7 @@ pub fn use_mouse() -> UseMouseReturn {
|
||||||
/// Variant of [`use_mouse`] that accepts options. Please see [`use_mouse`] for how to use.
|
/// Variant of [`use_mouse`] that accepts options. Please see [`use_mouse`] for how to use.
|
||||||
pub fn use_mouse_with_options<El, T, Ex>(options: UseMouseOptions<El, T, Ex>) -> UseMouseReturn
|
pub fn use_mouse_with_options<El, T, Ex>(options: UseMouseOptions<El, T, Ex>) -> UseMouseReturn
|
||||||
where
|
where
|
||||||
El: Clone,
|
El: Into<ElementMaybeSignal<T, web_sys::EventTarget>> + Clone,
|
||||||
El: Into<ElementMaybeSignal<T, web_sys::EventTarget>>,
|
|
||||||
T: Into<web_sys::EventTarget> + Clone + 'static,
|
T: Into<web_sys::EventTarget> + Clone + 'static,
|
||||||
Ex: UseMouseEventExtractor + Clone + 'static,
|
Ex: UseMouseEventExtractor + Clone + 'static,
|
||||||
{
|
{
|
||||||
|
@ -196,11 +195,11 @@ where
|
||||||
}}
|
}}
|
||||||
|
|
||||||
UseMouseReturn {
|
UseMouseReturn {
|
||||||
x,
|
x: x.into(),
|
||||||
y,
|
y: y.into(),
|
||||||
set_x,
|
set_x,
|
||||||
set_y,
|
set_y,
|
||||||
source_type,
|
source_type: source_type.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -306,15 +305,15 @@ impl UseMouseEventExtractor for UseMouseEventExtractorDefault {}
|
||||||
/// Return type of [`use_mouse`].
|
/// Return type of [`use_mouse`].
|
||||||
pub struct UseMouseReturn {
|
pub struct UseMouseReturn {
|
||||||
/// X coordinate of the mouse pointer / touch
|
/// X coordinate of the mouse pointer / touch
|
||||||
pub x: ReadSignal<f64>,
|
pub x: Signal<f64>,
|
||||||
/// Y coordinate of the mouse pointer / touch
|
/// Y coordinate of the mouse pointer / touch
|
||||||
pub y: ReadSignal<f64>,
|
pub y: Signal<f64>,
|
||||||
/// Sets the value of `x`. This does not actually move the mouse cursor.
|
/// Sets the value of `x`. This does not actually move the mouse cursor.
|
||||||
pub set_x: WriteSignal<f64>,
|
pub set_x: WriteSignal<f64>,
|
||||||
/// Sets the value of `y`. This does not actually move the mouse cursor.
|
/// Sets the value of `y`. This does not actually move the mouse cursor.
|
||||||
pub set_y: WriteSignal<f64>,
|
pub set_y: WriteSignal<f64>,
|
||||||
/// Identifies the source of the reported coordinates
|
/// Identifies the source of the reported coordinates
|
||||||
pub source_type: ReadSignal<UseMouseSourceType>,
|
pub source_type: Signal<UseMouseSourceType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Identifies the source of the reported coordinates
|
/// Identifies the source of the reported coordinates
|
||||||
|
|
251
src/use_mouse_in_element.rs
Normal file
251
src/use_mouse_in_element.rs
Normal file
|
@ -0,0 +1,251 @@
|
||||||
|
use crate::core::{ElementMaybeSignal, Position};
|
||||||
|
use crate::{
|
||||||
|
use_mouse_with_options, use_window, UseMouseCoordType, UseMouseEventExtractor,
|
||||||
|
UseMouseEventExtractorDefault, UseMouseOptions, UseMouseReturn, UseMouseSourceType, UseWindow,
|
||||||
|
};
|
||||||
|
use cfg_if::cfg_if;
|
||||||
|
use default_struct_builder::DefaultBuilder;
|
||||||
|
use leptos::*;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
/// Reactive mouse position related to an element.
|
||||||
|
///
|
||||||
|
/// ## Demo
|
||||||
|
///
|
||||||
|
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_mouse_in_element)
|
||||||
|
///
|
||||||
|
/// ## Usage
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use leptos::*;
|
||||||
|
/// # use leptos::html::Div;
|
||||||
|
/// # use leptos_use::{use_mouse_in_element, UseMouseInElementReturn};
|
||||||
|
/// #
|
||||||
|
/// # #[component]
|
||||||
|
/// # fn Demo() -> impl IntoView {
|
||||||
|
/// let target = create_node_ref::<Div>();
|
||||||
|
/// let UseMouseInElementReturn { x, y, is_outside, .. } = use_mouse_in_element(target);
|
||||||
|
///
|
||||||
|
/// view! {
|
||||||
|
/// <div node_ref=target>
|
||||||
|
/// <h1>Hello world</h1>
|
||||||
|
/// </div>
|
||||||
|
/// }
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// ## Server-Side Rendering
|
||||||
|
///
|
||||||
|
/// On the server this returns simple Signals with the `initial_value` for `x` and `y`,
|
||||||
|
/// no-op for `stop`, `is_outside = true` and `0.0` for the rest of the signals.
|
||||||
|
pub fn use_mouse_in_element<El, T>(target: El) -> UseMouseInElementReturn<impl Fn() + Clone>
|
||||||
|
where
|
||||||
|
El: Into<ElementMaybeSignal<T, web_sys::Element>> + Clone,
|
||||||
|
T: Into<web_sys::Element> + Clone + 'static,
|
||||||
|
{
|
||||||
|
use_mouse_in_element_with_options(target, Default::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Version of [`use_mouse_in_element`] that takes a `UseMouseInElementOptions`. See [`use_mouse_in_element`] for how to use.
|
||||||
|
pub fn use_mouse_in_element_with_options<El, T, OptEl, OptT, OptEx>(
|
||||||
|
target: El,
|
||||||
|
options: UseMouseInElementOptions<OptEl, OptT, OptEx>,
|
||||||
|
) -> UseMouseInElementReturn<impl Fn() + Clone>
|
||||||
|
where
|
||||||
|
El: Into<ElementMaybeSignal<T, web_sys::Element>> + Clone,
|
||||||
|
T: Into<web_sys::Element> + Clone + 'static,
|
||||||
|
OptEl: Into<ElementMaybeSignal<OptT, web_sys::EventTarget>> + Clone,
|
||||||
|
OptT: Into<web_sys::EventTarget> + Clone + 'static,
|
||||||
|
OptEx: UseMouseEventExtractor + Clone + 'static,
|
||||||
|
{
|
||||||
|
let UseMouseInElementOptions {
|
||||||
|
coord_type,
|
||||||
|
target: use_mouse_target,
|
||||||
|
touch,
|
||||||
|
reset_on_touch_ends,
|
||||||
|
initial_value,
|
||||||
|
handle_outside,
|
||||||
|
..
|
||||||
|
} = options;
|
||||||
|
|
||||||
|
let UseMouseReturn {
|
||||||
|
x, y, source_type, ..
|
||||||
|
} = use_mouse_with_options(
|
||||||
|
UseMouseOptions::default()
|
||||||
|
.coord_type(coord_type)
|
||||||
|
.target(use_mouse_target)
|
||||||
|
.touch(touch)
|
||||||
|
.reset_on_touch_ends(reset_on_touch_ends)
|
||||||
|
.initial_value(initial_value),
|
||||||
|
);
|
||||||
|
|
||||||
|
let (element_x, set_element_x) = create_signal(0.0);
|
||||||
|
let (element_y, set_element_y) = create_signal(0.0);
|
||||||
|
let (element_position_x, set_element_position_x) = create_signal(0.0);
|
||||||
|
let (element_position_y, set_element_position_y) = create_signal(0.0);
|
||||||
|
let (element_width, set_element_width) = create_signal(0.0);
|
||||||
|
let (element_height, set_element_height) = create_signal(0.0);
|
||||||
|
let (is_outside, set_outside) = create_signal(true);
|
||||||
|
|
||||||
|
cfg_if! { if #[cfg(feature = "ssr")] {
|
||||||
|
let stop = || ();
|
||||||
|
|
||||||
|
let _ = handle_outside;
|
||||||
|
|
||||||
|
let _ = set_element_x;
|
||||||
|
let _ = set_element_y;
|
||||||
|
let _ = set_element_position_x;
|
||||||
|
let _ = set_element_position_y;
|
||||||
|
let _ = set_element_width;
|
||||||
|
let _ = set_element_height;
|
||||||
|
let _ = set_outside;
|
||||||
|
let _ = target;
|
||||||
|
} else {
|
||||||
|
use crate::use_event_listener;
|
||||||
|
use leptos::ev::mouseleave;
|
||||||
|
|
||||||
|
let target = target.into();
|
||||||
|
let window = window();
|
||||||
|
|
||||||
|
let stop = watch(
|
||||||
|
move || (target.get(), x.get(), y.get()),
|
||||||
|
move |(el, x, y), _, _| {
|
||||||
|
if let Some(el) = el {
|
||||||
|
let el: web_sys::Element = el.clone().into();
|
||||||
|
let rect = el.get_bounding_client_rect();
|
||||||
|
let left = rect.left();
|
||||||
|
let top = rect.top();
|
||||||
|
let width = rect.width();
|
||||||
|
let height = rect.height();
|
||||||
|
|
||||||
|
set_element_position_x.set(left + window.page_x_offset().unwrap_or_default());
|
||||||
|
set_element_position_y.set(top + window.page_y_offset().unwrap_or_default());
|
||||||
|
|
||||||
|
set_element_height.set(height);
|
||||||
|
set_element_width.set(width);
|
||||||
|
|
||||||
|
let el_x = *x - element_position_x.get_untracked();
|
||||||
|
let el_y = *y - element_position_y.get_untracked();
|
||||||
|
|
||||||
|
set_outside.set(
|
||||||
|
width == 0.0
|
||||||
|
|| height == 0.0
|
||||||
|
|| el_x <= 0.0
|
||||||
|
|| el_y <= 0.0
|
||||||
|
|| el_x > width
|
||||||
|
|| el_y > height,
|
||||||
|
);
|
||||||
|
|
||||||
|
if handle_outside || !is_outside.get_untracked() {
|
||||||
|
set_element_x.set(el_x);
|
||||||
|
set_element_y.set(el_y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = use_event_listener(document(), mouseleave, move |_| set_outside.set(true));
|
||||||
|
}}
|
||||||
|
|
||||||
|
UseMouseInElementReturn {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
source_type,
|
||||||
|
element_x: element_x.into(),
|
||||||
|
element_y: element_y.into(),
|
||||||
|
element_position_x: element_position_x.into(),
|
||||||
|
element_position_y: element_position_y.into(),
|
||||||
|
element_width: element_width.into(),
|
||||||
|
element_height: element_height.into(),
|
||||||
|
is_outside: is_outside.into(),
|
||||||
|
stop,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Options for [`use_mouse_in_element_with_options`].
|
||||||
|
#[derive(DefaultBuilder)]
|
||||||
|
pub struct UseMouseInElementOptions<El, T, Ex>
|
||||||
|
where
|
||||||
|
El: Clone + Into<ElementMaybeSignal<T, web_sys::EventTarget>>,
|
||||||
|
T: Into<web_sys::EventTarget> + Clone + 'static,
|
||||||
|
Ex: UseMouseEventExtractor + Clone,
|
||||||
|
{
|
||||||
|
/// How to extract the x, y coordinates from mouse events or touches
|
||||||
|
coord_type: UseMouseCoordType<Ex>,
|
||||||
|
|
||||||
|
/// Listen events on `target` element. Defaults to `window`
|
||||||
|
target: El,
|
||||||
|
|
||||||
|
/// Listen to `touchmove` events. Defaults to `true`.
|
||||||
|
touch: bool,
|
||||||
|
|
||||||
|
/// Reset to initial value when `touchend` event fired. Defaults to `false`
|
||||||
|
reset_on_touch_ends: bool,
|
||||||
|
|
||||||
|
/// Initial values. Defaults to `{x: 0.0, y: 0.0}`.
|
||||||
|
initial_value: Position,
|
||||||
|
|
||||||
|
/// If `true` updates the `element_x` and `element_y` signals even if the
|
||||||
|
/// mouse is outside of the element. If `false` it doesn't update them when outside.
|
||||||
|
/// Defaults to `true`.
|
||||||
|
handle_outside: bool,
|
||||||
|
|
||||||
|
#[builder(skip)]
|
||||||
|
_marker: PhantomData<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default
|
||||||
|
for UseMouseInElementOptions<UseWindow, web_sys::Window, UseMouseEventExtractorDefault>
|
||||||
|
{
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
coord_type: UseMouseCoordType::<UseMouseEventExtractorDefault>::default(),
|
||||||
|
target: use_window(),
|
||||||
|
touch: true,
|
||||||
|
reset_on_touch_ends: false,
|
||||||
|
initial_value: Position { x: 0.0, y: 0.0 },
|
||||||
|
handle_outside: true,
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return type of [`use_mouse_in_element`].
|
||||||
|
pub struct UseMouseInElementReturn<F>
|
||||||
|
where
|
||||||
|
F: Fn() + Clone,
|
||||||
|
{
|
||||||
|
/// X coordinate of the mouse pointer / touch
|
||||||
|
pub x: Signal<f64>,
|
||||||
|
|
||||||
|
/// Y coordinate of the mouse pointer / touch
|
||||||
|
pub y: Signal<f64>,
|
||||||
|
|
||||||
|
/// Identifies the source of the reported coordinates
|
||||||
|
pub source_type: Signal<UseMouseSourceType>,
|
||||||
|
|
||||||
|
/// X coordinate of the pointer relative to the left edge of the element
|
||||||
|
pub element_x: Signal<f64>,
|
||||||
|
|
||||||
|
/// Y coordinate of the pointer relative to the top edge of the element
|
||||||
|
pub element_y: Signal<f64>,
|
||||||
|
|
||||||
|
/// X coordinate of the element relative to the left edge of the document
|
||||||
|
pub element_position_x: Signal<f64>,
|
||||||
|
|
||||||
|
/// Y coordinate of the element relative to the top edge of the document
|
||||||
|
pub element_position_y: Signal<f64>,
|
||||||
|
|
||||||
|
/// Width of the element
|
||||||
|
pub element_width: Signal<f64>,
|
||||||
|
|
||||||
|
/// Height of the element
|
||||||
|
pub element_height: Signal<f64>,
|
||||||
|
|
||||||
|
/// `true` if the mouse is outside of the element
|
||||||
|
pub is_outside: Signal<bool>,
|
||||||
|
|
||||||
|
/// Stop watching
|
||||||
|
pub stop: F,
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue