useIntersectionObserver ​
Hook that gives you intersection observer state
browser
test coverage
Last changed: 9 days 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, entry, inView } = useIntersectionObserver();
// or
const { entry, inView } = useIntersectionObserver(ref);
// or
const { ref, entry, inView } = useIntersectionObserver(() => console.log('callback'));
// or
const { entry, inView } = useIntersectionObserver(() => console.log('callback'), ref);
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 | (entry: IntersectionObserverEntry) => void | - | The callback to execute when intersection is detected |
Returns
UseIntersectionObserverReturn & { ref: StateRef<Target> }
Parameters
Name | Type | Default | Note |
---|---|---|---|
callback | (entry: IntersectionObserverEntry) => void | - | 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 interface UseIntersectionObserverOptions extends Omit<IntersectionObserverInit, 'root'> {
enabled?: boolean;
root?: HookTarget;
onChange?: (entry: IntersectionObserverEntry) => void;
}
export interface UseIntersectionObserverReturn {
entry?: IntersectionObserverEntry;
inView: boolean;
}
export interface UseIntersectionObserver {
<Target extends Element>(
options?: UseIntersectionObserverOptions,
target?: never
): UseIntersectionObserverReturn & { ref: StateRef<Target> };
(target: HookTarget, options?: UseIntersectionObserverOptions): UseIntersectionObserverReturn;
<Target extends Element>(
callback: (entry: IntersectionObserverEntry) => void,
target?: never
): UseIntersectionObserverReturn & { ref: StateRef<Target> };
(
callback: (entry: IntersectionObserverEntry) => void,
target: HookTarget
): UseIntersectionObserverReturn;
}
Source ​
Source • DemoContributors ​
D