-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use wasm_bindgen::{ | ||
closure::Closure, | ||
JsCast, | ||
}; | ||
use web_sys::{IntersectionObserver, IntersectionObserverEntry}; | ||
use yew::{NodeRef, functional::*}; | ||
use crate::use_effect_once; | ||
|
||
|
||
#[hook] | ||
/// Check if an element is visible. Internally, it uses an [`IntersectionObserver`] to receive | ||
/// notifications from the browser whenever the visibility state of the node changes. | ||
/// | ||
/// Setting the sticky bit makes this hook disconnect the observer once the element is visible, and | ||
/// keep the visibility set to `true`, even when it becomes invisible. This is often desired | ||
/// for lazy-loading components. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use yew::prelude::*; | ||
/// use yew_hooks::use_visible; | ||
/// | ||
/// #[function_component] | ||
/// fn MyComponent() -> Html { | ||
/// let node = use_node_ref(); | ||
/// let visible = use_visible(node.clone(), false); | ||
/// html! { | ||
/// <div ref={node}> | ||
/// if visible { | ||
/// <p>{"I'm visible!"}</p> | ||
/// } else { | ||
/// <p>{"I'm invisible!"}</p> | ||
/// } | ||
/// </div> | ||
/// } | ||
/// } | ||
/// ``` | ||
pub fn use_visible(node: NodeRef, sticky: bool) -> bool { | ||
// code adapted from: | ||
// https://stackoverflow.com/questions/1462138/event-listener-for-when-element-becomes-visible | ||
let visible = use_state_eq(|| false); | ||
let visible_clone = visible.clone(); | ||
use_effect_once(move || { | ||
let closure = Closure::<dyn Fn(Vec<IntersectionObserverEntry>, IntersectionObserver)>::new( | ||
move |entries: Vec<IntersectionObserverEntry>, observer: IntersectionObserver| { | ||
// determine if any part of this node is visible. | ||
let visible = entries.iter().any(|entry| entry.intersection_ratio() > 0.0); | ||
|
||
// if the visibility changed, update the state. | ||
if (visible != *visible_clone) && (!sticky || !*visible_clone) { | ||
visible_clone.set(visible); | ||
} | ||
|
||
// if this is sticky and it is currently visible, disconnect the observer. | ||
if visible && sticky { | ||
observer.disconnect(); | ||
} | ||
}, | ||
) | ||
.into_js_value(); | ||
let observer = IntersectionObserver::new(&closure.dyn_ref().unwrap()).unwrap(); | ||
if let Some(node) = node.get() { | ||
observer.observe(&node.dyn_ref().unwrap()); | ||
} | ||
move || observer.disconnect() | ||
}); | ||
*visible | ||
} |