Skip to content

Commit 4bfd439

Browse files
committed
use_visible demo
1 parent 63e5c43 commit 4bfd439

File tree

8 files changed

+50
-13
lines changed

8 files changed

+50
-13
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn counter() -> Html {
5454
let counter = counter.clone();
5555
Callback::from(move |_| counter.decrease())
5656
};
57-
57+
5858
html! {
5959
<>
6060
<button onclick={onincrease}>{ "Increase" }</button>
@@ -72,7 +72,7 @@ fn counter() -> Html {
7272

7373
- `use_toggle` - tracks state of counterparts.
7474
- `use_bool_toggle` - tracks state of a boolean.
75-
- `use_counter` - tracks state of a number.
75+
- `use_counter` - tracks state of a number.
7676
- `use_latest` - returns the latest immutable ref to state or props.
7777
- `use_mut_latest` - returns the latest mutable ref to state or props.
7878
- `use_previous` - returns the previous immutable ref to state or props.
@@ -134,6 +134,7 @@ fn counter() -> Html {
134134
- `use_measure` - tracks an HTML element's dimensions using the `ResizeObserver` API.
135135
- `use_geolocation` - tracks user's geographic location.
136136
- `use_swipe` - detects swipe based on TouchEvent.
137+
- `use_visible` - checks if an element is visible.
137138

138139
### UI
139140

@@ -178,7 +179,7 @@ fn counter() -> Html {
178179
let counter = counter.clone();
179180
Callback::from(move |_| counter.reset())
180181
};
181-
182+
182183
html! {
183184
<div>
184185
<button onclick={onincrease}>{ "Increase" }</button>

crates/yew-hooks/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "yew-hooks"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition = "2021"
55
authors = ["Jet Li <[email protected]>"]
66
categories = ["gui", "wasm", "web-programming"]
@@ -16,7 +16,7 @@ documentation = "https://docs.rs/yew-hooks/"
1616

1717
[dependencies]
1818
log = "0.4"
19-
yew = { version = "0.21.0", features=["csr"] }
19+
yew = { version = "0.21.0", features = ["csr"] }
2020
gloo = "0.10"
2121
wasm-bindgen = "0.2"
2222
wasm-bindgen-futures = "0.4"

crates/yew-hooks/src/hooks/use_visible.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use yew::prelude::*;
44

55
use super::use_effect_once;
66

7-
#[hook]
87
/// Check if an element is visible. Internally, it uses an [`IntersectionObserver`] to receive
98
/// notifications from the browser whenever the visibility state of the node changes.
109
///
@@ -15,13 +14,15 @@ use super::use_effect_once;
1514
/// # Example
1615
///
1716
/// ```rust
18-
/// use yew::prelude::*;
19-
/// use yew_hooks::use_visible;
17+
/// # use yew::prelude::*;
18+
/// #
19+
/// use yew_hooks::prelude::*;
2020
///
2121
/// #[function_component]
2222
/// fn MyComponent() -> Html {
2323
/// let node = use_node_ref();
2424
/// let visible = use_visible(node.clone(), false);
25+
///
2526
/// html! {
2627
/// <div ref={node}>
2728
/// if visible {
@@ -33,21 +34,21 @@ use super::use_effect_once;
3334
/// }
3435
/// }
3536
/// ```
37+
#[hook]
3638
pub fn use_visible(node: NodeRef, sticky: bool) -> bool {
3739
// code adapted from:
3840
// https://stackoverflow.com/questions/1462138/event-listener-for-when-element-becomes-visible
3941
let visible = use_state_eq(|| false);
4042
let visible_clone = visible.clone();
43+
4144
use_effect_once(move || {
4245
let closure = Closure::<dyn Fn(Vec<IntersectionObserverEntry>, IntersectionObserver)>::new(
4346
move |entries: Vec<IntersectionObserverEntry>, observer: IntersectionObserver| {
4447
// determine if any part of this node is visible.
4548
let visible = entries.iter().any(|entry| entry.intersection_ratio() > 0.0);
4649

4750
// if the visibility changed, update the state.
48-
if (visible != *visible_clone) && (!sticky || !*visible_clone) {
49-
visible_clone.set(visible);
50-
}
51+
visible_clone.set(visible);
5152

5253
// if this is sticky and it is currently visible, disconnect the observer.
5354
if visible && sticky {
@@ -62,5 +63,6 @@ pub fn use_visible(node: NodeRef, sticky: bool) -> bool {
6263
}
6364
move || observer.disconnect()
6465
});
66+
6567
*visible
6668
}

crates/yew-hooks/src/hooks/use_websocket.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ pub struct UseWebSocketOptions {
3333
/// `WebSocket` close callback.
3434
pub onclose: Option<Box<dyn FnMut(CloseEvent)>>,
3535

36-
/// Retry times.
36+
/// Retry times. Defaults to 3, use `u32::MAX` for infinite retries.
3737
pub reconnect_limit: Option<u32>,
38-
/// Retry interval(ms).
38+
/// Retry interval(ms). Defaults to 3000.
3939
pub reconnect_interval: Option<u32>,
4040
/// Manually starts connection
4141
pub manual: Option<bool>,

examples/yew-app/src/app/home.rs

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub fn home() -> Html {
8989
<li><Link<AppRoute> to={AppRoute::UseMeasure} classes="text-emerald-800 underline" >{ "use_measure" }</Link<AppRoute>> { " - tracks an HTML element's dimensions using the ResizeObserver API." }</li>
9090
<li><Link<AppRoute> to={AppRoute::UseGeolocation} classes="text-emerald-800 underline" >{ "use_geolocation" }</Link<AppRoute>> { " - tracks user's geographic location." }</li>
9191
<li><Link<AppRoute> to={AppRoute::UseSwipe} classes="text-emerald-800 underline" >{ "use_swipe" }</Link<AppRoute>> { " - detects swipe based on TouchEvent." }</li>
92+
<li><Link<AppRoute> to={AppRoute::UseVisible} classes="text-emerald-800 underline" >{ "use_visible" }</Link<AppRoute>> { " - checks if an element is visible." }</li>
9293
</ul>
9394

9495
<h2 class="text-2xl font-bold">{ "UI" }</h2>

examples/yew-app/src/app/hooks/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ mod use_title;
5151
mod use_toggle;
5252
mod use_unmount;
5353
mod use_update;
54+
mod use_visible;
5455
mod use_websocket;
5556
mod use_window_scroll;
5657
mod use_window_size;
@@ -108,6 +109,7 @@ pub use use_title::*;
108109
pub use use_toggle::*;
109110
pub use use_unmount::*;
110111
pub use use_update::*;
112+
pub use use_visible::*;
111113
pub use use_websocket::*;
112114
pub use use_window_scroll::*;
113115
pub use use_window_size::*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use yew::prelude::*;
2+
use yew_hooks::prelude::*;
3+
4+
/// `use_visible` demo
5+
#[function_component]
6+
pub fn UseVisible() -> Html {
7+
let node = use_node_ref();
8+
let visible = use_visible(node.clone(), false);
9+
10+
html! {
11+
<div class="container">
12+
<header class="mt-24 text-xl text-center">
13+
<div class="space-x-4 space-y-4">
14+
<p>
15+
<b>{ " Visible: " }</b>
16+
{ visible }
17+
</p>
18+
<div class="w-[600px] h-[400px] overflow-scroll bg-emerald-800 mx-auto text-slate-100">
19+
<div class="w-[1000px] h-[1000px] text-left">
20+
<div class="h-[600px]">{ "Try to scroll in this area." }</div>
21+
<div ref={node} class="w-[100px] h-[100px] bg-slate-800"></div>
22+
</div>
23+
</div>
24+
</div>
25+
</header>
26+
</div>
27+
}
28+
}

examples/yew-app/src/app/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ pub enum AppRoute {
127127
UseClipboard,
128128
#[at("/use_infinite_scroll")]
129129
UseInfiniteScroll,
130+
#[at("/use_visible")]
131+
UseVisible,
130132
#[not_found]
131133
#[at("/page-not-found")]
132134
PageNotFound,
@@ -195,6 +197,7 @@ pub fn switch(routes: AppRoute) -> Html {
195197
AppRoute::UseFavicon => html! { <UseFavicon /> },
196198
AppRoute::UseClipboard => html! { <UseClipboard /> },
197199
AppRoute::UseInfiniteScroll => html! { <UseInfiniteScroll /> },
200+
AppRoute::UseVisible => html! { <UseVisible /> },
198201
AppRoute::PageNotFound => html! { <Home /> },
199202
}
200203
}

0 commit comments

Comments
 (0)