Skip to Content
ohooks

useIntersectionObserver

Observes changes in the intersection of a target DOM element with an ancestor element or the viewport.

Loading preview...

The useIntersectionObserver hook wraps the browser IntersectionObserver API to track when a target element enters or leaves the viewport (or a custom scroll container) and returns the latest IntersectionObserverEntry.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useIntersectionObserver.json

Usage

Examples

One-Time Reveal Animation (freezeOnceVisible)

Enable freezeOnceVisible: true to disconnect the observer as soon as the element becomes visible for the first time.

Reacting via onChange Callback

Use the onChange option to trigger side effects (such as analytics impression tracking) without needing an extra useEffect.

API Reference

UseIntersectionObserverOptions

Configuration options for IntersectionObserver thresholds, root container, and lifecycle callbacks.

Property
Specification
root?
uniondefault:null
Ancestor element or document used as the viewport for checking visibility
rootMargin?
stringdefault:'0px'
Margin around the root element
threshold?
uniondefault:0
Visibility ratio threshold(s) at which the observer callback fires
freezeOnceVisible?
uniondefault:false
Stop observing once the target element has intersected at least once
enabled?
uniondefault:true
Whether the observer is active
onChange?
function
Callback fired whenever the intersection entry updates