Skip to content
Elements

useIntersectionObserver

Reactive wrapper around the IntersectionObserver API. Observes one or more elements for intersection changes with pause/resume/stop support. Targets can be Ref, MaybeElement, or a plain Element`.

Intersection Observer
false
↓ scroll down
target element
import { useRef$, useIntersectionObserver } from "@usels/core";
function Component() {
const el$ = useRef$<HTMLDivElement>();
const { isActive$, pause, resume } = useIntersectionObserver(
el$,
(entries) => {
entries.forEach((entry) => {
console.log(entry.isIntersecting);
});
},
{ threshold: 0.5 }
);
return <div ref={el$} />;
}

Trigger the callback at multiple intersection ratios:

useIntersectionObserver(el$, callback, {
threshold: [0, 0.25, 0.5, 0.75, 1],
});
useIntersectionObserver(el$, callback, {
root: scrollContainerEl,
rootMargin: "0px 0px -100px 0px",
});

Pass an Observable to reactively recreate the observer when the value changes:

const rootMargin$ = observable("0px");
useIntersectionObserver(el$, callback, { rootMargin: rootMargin$ });
// later — observer is automatically recreated with new margin
rootMargin$.set("-50px 0px");

Set immediate: false to start observation manually:

const { resume } = useIntersectionObserver(el$, callback, { immediate: false });
resume(); // starts observing
const { pause, resume } = useIntersectionObserver(el$, callback);
pause(); // disconnects the observer, isActive$ → false
resume(); // reconnects the observer, isActive$ → true
const { stop } = useIntersectionObserver(el$, callback);
stop(); // disconnects and prevents future resume()
const { isSupported$ } = useIntersectionObserver(el$, callback);
console.log(isSupported$.get()); // Observable<boolean>
ParameterTypeDescription
targetMaybeEventTarget | MaybeEventTarget[]-
callbackIntersectionObserverCallback-
optionsUseIntersectionObserverOptions (optional)-
OptionTypeDefaultDescription
immediateboolean-Whether to start observing immediately on mount. Default: true
rootMaybeEventTarget-The element or document used as the viewport. Default: browser viewport
rootMarginstring-Margin around the root. Accepts CSS-style values. Default: “0px”
thresholdnumber | number[]-Threshold(s) at which to trigger the callback. Default: 0

UseIntersectionObserverReturn

NameTypeDescription
stop() => void-
isSupported$ReadonlyObservable<boolean>-
isActive$ReadonlyObservable<boolean>-
pauseFn-
resumeFn-

View on GitHub