use-element-in-view
Version:
A simple React hook to track whether an element is visible in the viewport with the Intersection Observer. This API provides a native way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-leve
16 lines (15 loc) • 749 B
TypeScript
import { RefObject, RefCallback } from 'react';
export interface IElementInViewOptions<T> extends IntersectionObserverInit {
ref?: T | RefObject<T> | null;
defaultInView?: boolean;
disconnectOnceVisible?: boolean;
onChange?: (entry: IntersectionObserverEntry) => void;
}
interface IElementInViewResult<T> {
readonly inView: boolean;
readonly entry: IntersectionObserverEntry | undefined;
readonly assignRef: RefCallback<T>;
readonly disconnect: () => void;
}
export declare function useElementInView<T extends HTMLElement = HTMLElement>({ ref: forwardedRef, root, rootMargin, threshold, defaultInView, disconnectOnceVisible, onChange, }?: IElementInViewOptions<T>): IElementInViewResult<T>;
export {};