euv_ui/hook/use_intersection_observer/fn.rs
1use super::*;
2
3/// Obtains the [`UseIntersectionObserver`] registered against the
4/// current hook context slot.
5///
6/// Behaves like `HookContext::use_hook` - the same handle is returned on
7/// every render at the same hook index, so the observer is created once
8/// and the visibility survives re-renders.
9///
10/// Attach `get_target()` to the caller's markup with the `ref:`
11/// attribute, then read `get_visible()` inside a render closure to
12/// react to the element entering or leaving the viewport.
13///
14/// # Returns
15///
16/// - `UseIntersectionObserver` - The handle, with `get_target()`
17/// returning the [`NodeRef`] and `get_visible()` the reactive
18/// `Signal<bool>`.
19pub fn use_intersection_observer() -> UseIntersectionObserver {
20 let handle: UseIntersectionObserver = HookContext::use_hook(|| {
21 let handle: UseIntersectionObserver = UseIntersectionObserver::new();
22 handle.attach();
23 handle
24 });
25 App::use_cleanup({
26 let handle: UseIntersectionObserver = handle.clone();
27 move || handle.detach()
28 });
29 handle
30}