Skip to content

useIntersectionObserver

Hook that gives you intersection observer state

sensors
medium
test coverage
Last changed: 2 months ago

TIP

This hook uses IntersectionObserver browser api to provide enhanced functionality. Make sure to check for compatibility with different browsers when using this api

Installation

Library
CLI
Manual
typescript
import { useIntersectionObserver } from '@siberiacancode/reactuse';

Usage

typescript
const { ref, entries, observer } = useIntersectionObserver();
// or
const { entries, observer } = useIntersectionObserver(ref);
// or
const { ref, entries, observer } = useIntersectionObserver(() => console.log('callback'));
// or
const { entries, observer } = useIntersectionObserver(ref, () => console.log('callback'));

Demo

Api

Parameters

NameTypeDefaultNote
targetHookTarget-The target element to detect intersection
options.enabled?booleantrueThe IntersectionObserver options
options.onChange?((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined-The callback to execute when intersection is detected
options.root?HookTargetdocumentThe root element to observe

Returns

UseIntersectionObserverReturn

Parameters

NameTypeDefaultNote
options.enabled?booleantrueThe IntersectionObserver options
options.onChange?((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined-The callback to execute when intersection is detected
options.root?HookTargetdocumentThe root element to observe

Returns

UseIntersectionObserverReturn & { ref: StateRef<Target> }

Parameters

NameTypeDefaultNote
callbackUseIntersectionObserverCallback-The callback to execute when intersection is detected

Returns

UseIntersectionObserverReturn & { ref: StateRef<Target> }

Parameters

NameTypeDefaultNote
callbackUseIntersectionObserverCallback-The callback to execute when intersection is detected
targetHookTarget-The target element to detect intersection

Returns

UseIntersectionObserverReturn

Type declaration

typescript
import type { HookTarget } from '@/utils/helpers';

import type { StateRef } from '../useRefState/useRefState';

export type UseIntersectionObserverCallback = (
  entries: IntersectionObserverEntry[],
  observer: IntersectionObserver
) => void;

export interface UseIntersectionObserverOptions extends Omit<IntersectionObserverInit, 'root'> {
  /** The enabled state of the intersection observer */
  enabled?: boolean;
  /** The callback to execute when intersection is detected */
  onChange?: UseIntersectionObserverCallback;
  /** The root element to observe */
  root?: HookTarget;
}

export interface UseIntersectionObserverReturn {
  /** The intersection observer entry */
  entries?: IntersectionObserverEntry[];
  /** The intersection observer instance */
  observer?: IntersectionObserver;
}

export interface UseIntersectionObserver {
  <Target extends Element>(
    options?: UseIntersectionObserverOptions,
    target?: never
  ): UseIntersectionObserverReturn & { ref: StateRef<Target> };

  (target: HookTarget, options?: UseIntersectionObserverOptions): UseIntersectionObserverReturn;

  <Target extends Element>(
    callback: UseIntersectionObserverCallback,
    target?: never
  ): UseIntersectionObserverReturn & { ref: StateRef<Target> };

  (target: HookTarget, callback: UseIntersectionObserverCallback): UseIntersectionObserverReturn;
}

Source

SourceDemo

Contributors

D
debabin
debabin

Released under the MIT License.