Skip to content
usefy

useIntersectionObserver

Layout & observers

A powerful React hook for observing element visibility using the Intersection Observer API

Install

$ npm install @usefy/use-intersection-observer

Quick start

useIntersectionObserver.tsx
import { useIntersectionObserver } from "@usefy/use-intersection-observer";

function MyComponent() {
  const { ref, inView, entry } = useIntersectionObserver();

  return <div ref={ref}>{inView ? "👁️ Visible!" : "👻 Not visible"}</div>;
}

API reference

useIntersectionObserver(options?)

A hook that observes element visibility using the Intersection Observer API.

Parameters

ParameterTypeDescription
optionsUseIntersectionObserverOptionsOptional configuration object

Options

OptionTypeDefaultDescription
thresholdnumber | number[]0Visibility ratio(s) that trigger updates (0.0 to 1.0). Updates occur when crossing these boundaries
rootElement | Document | nullnullRoot element for intersection (null = viewport)
rootMarginstring"0px"Margin around root (CSS margin syntax). Positive expands, negative shrinks detection area
triggerOncebooleanfalseStop observing after element first becomes visible
enabledbooleantrueEnable/disable observer. When false, observer disconnects and stops all updates
initialIsIntersectingbooleanfalseInitial intersection state before first observation (useful for SSR/SSG)
onChange(entry: IntersectionEntry, inView: boolean) => voidCallback fired when isIntersecting or intersectionRatio changes
delaynumber0Delay in milliseconds before creating the observer (not individual events)

Returns UseIntersectionObserverReturn

PropertyTypeDescription
entryIntersectionEntry | nullIntersection entry data (null if not yet observed). Updates trigger re-renders
inViewbooleanWhether the element is currently intersecting (convenience derived from entry)
ref(node: Element | null) => voidCallback ref to attach to the target element you want to observe

IntersectionEntry

Extended intersection entry with convenience properties:

PropertyTypeDescription
entryIntersectionObserverEntryOriginal native IntersectionObserverEntry from the browser API
isIntersectingbooleanWhether target is intersecting with root
intersectionRationumberRatio of target visible (0.0 to 1.0)
targetElementThe observed DOM element
boundingClientRectDOMRectReadOnlyTarget element's bounding box relative to viewport
intersectionRectDOMRectReadOnlyVisible portion's bounding box (intersection of target and root)
rootBoundsDOMRectReadOnly | nullRoot element's bounding box (null if root is the viewport)
timenumberDOMHighResTimeStamp when intersection was recorded

Go deeper

This page is the quick reference. For every example, prop, and edge case, read the full README — or open Storybook to change props live.

More in layout & observers