useIntersection
Know whether an element is in view, with an IntersectionObserver.
Source@monoline/react 0.4.0Markdown for AI toolsstable
- No DOM output. When IntersectionObserver is unavailable, it reports not intersecting.
Import
import '@monoline/css';
import {useIntersection} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add use-intersection
Usage
import {useIntersection, type Demo} from '@monoline/react';
function Demo() {
const [ref, entry] = useIntersection<HTMLDivElement>({threshold: 0.5});
return (
<div style={{height: '120px', overflowY: 'auto', border: '1px solid var(--rule)'}} tabIndex={0} aria-label="Scrollable area">
<div style={{height: '160px', padding: 'var(--s3)'}}>Scroll down</div>
<div ref={ref} style={{padding: 'var(--s4)', background: entry?.isIntersecting ? 'var(--wash-2)' : 'transparent'}}>{entry?.isIntersecting ? 'In view' : 'Out of view'}</div>
<div style={{height: '120px'}} />
</div>
);
}
<Demo />
Examples
Scroll to reveal
Props
UseIntersectionOptions
| Prop | Type | Default | Description |
|---|---|---|---|
once | boolean | – | Stop observing after the first time it is visible. |
Behaviour
- No DOM output. When IntersectionObserver is unavailable, it reports not intersecting.