useIntersectionObserver ​
Hook that gives you intersection observer state
sensors
medium
test coverage
Last changed: last month
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
| Name | Type | Default | Note |
|---|---|---|---|
| target | HookTarget | - | The target element to detect intersection |
| options.enabled? | boolean | true | The IntersectionObserver options |
| options.onChange? | ((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined | - | The callback to execute when intersection is detected |
| options.root? | HookTarget | document | The root element to observe |
Returns
UseIntersectionObserverReturn
Parameters
| Name | Type | Default | Note |
|---|---|---|---|
| options.enabled? | boolean | true | The IntersectionObserver options |
| options.onChange? | ((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined | - | The callback to execute when intersection is detected |
| options.root? | HookTarget | document | The root element to observe |
Returns
UseIntersectionObserverReturn & { ref: StateRef<Target> }
Parameters
| Name | Type | Default | Note |
|---|---|---|---|
| callback | UseIntersectionObserverCallback | - | The callback to execute when intersection is detected |
Returns
UseIntersectionObserverReturn & { ref: StateRef<Target> }
Parameters
| Name | Type | Default | Note |
|---|---|---|---|
| callback | UseIntersectionObserverCallback | - | The callback to execute when intersection is detected |
| target | HookTarget | - | 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 ​
Source • DemoContributors ​
D