use-intersection

Get information about intersection of given element with its scroll container
Import

Usage

use-intersection gets information about the intersection of a given element with its scroll container or body element with Intersection Observer API:

Obscured
import { useRef } from 'react';
import { useIntersection } from '@mantine/hooks';
import { Text, Paper, Box } from '@mantine/core';
function Demo() {
const containerRef = useRef();
const { ref, entry } = useIntersection({
root: containerRef.current,
threshold: 1,
});
return (
<Paper ref={containerRef} h={300} sx={{ overflowY: 'scroll' }}>
<Box pt={260} pb={280}>
<Paper
ref={ref}
p="xl"
sx={(theme) => ({
backgroundColor: entry?.isIntersecting ? theme.colors.green[9] : theme.colors.red[9],
minWidth: '50%',
})}
>
<Text c="#fff" weight={700}>
{entry?.isIntersecting ? 'Fully visible' : 'Obscured'}
</Text>
</Paper>
</Box>
</Paper>
);
}

API

Hook accepts IntersectionObserver's options as its only optional argument:

useIntersection({
root: someDomElement,
rootMargin: '0rem',
threshold: 1.0,
});

Hook returns ref function that should be passed to the observed element, and the latest entry, as returned by IntersectionObserver's callback. See Intersection Observer API documentation to learn everything about options.

On the first render (as well as during SSR), or when no element is being observed, the entry is null.

const { ref, entry } = useIntersection();
// With regular element:
<div ref={ref}>Observed</div>
// With Mantine component:
<Paper ref={ref}>Observed</Paper>

Definition

function useIntersection<T extends HTMLElement = any>(
options?: ConstructorParameters<typeof IntersectionObserver>[1]
): {
ref: (element: T | null) => void;
entry: IntersectionObserverEntry;
};