useIntersectionObserverperformance
useIntersectionObserver
NewSSR SAFETriggers when an element enters or leaves the viewport.
$ pnpm dlx shadcn@latest add @byphongphan/use-intersection-observerRequires
@byphongphan in components.jsonuse-intersection-observer.tsx
Loading preview...
Installation
$ pnpm dlx shadcn@latest add @byphongphan/use-intersection-observerRequires
@byphongphan in components.jsonUsage
Props & Parameters
| Param | Type | Default | Description |
|---|---|---|---|
threshold | number | number[] | 0 | Either a single number or an array of numbers between 0.0 and 1.0 indicating visibility ratio. |
root | Element | Document | null | null | The element that is used as the viewport for checking visibility. Defaults to browser viewport. |
rootMargin | string | "0%" | Margin around the root element, similar to CSS margin property. |
freezeOnceVisible | boolean | false | When true, automatically unobserves element after it first enters viewport. |
enabled | boolean | true | Whether intersection observer is active. |
Returns
| Return | Type | Description |
|---|---|---|
ref | RefObject<T | null> | React Ref to attach to target DOM element container. |
isIntersecting | boolean | true when target is currently intersecting viewport based on threshold. |
entry | IntersectionObserverEntry | null | Raw native intersection observer entry record. |