mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-27 20:10:58 +00:00
29 lines
744 B
JavaScript
29 lines
744 B
JavaScript
'use client';
|
|
import { useState, useRef, useCallback } from 'react';
|
|
|
|
function useIntersection(options) {
|
|
const [entry, setEntry] = useState(null);
|
|
const observer = useRef(null);
|
|
const ref = useCallback(
|
|
(element) => {
|
|
if (observer.current) {
|
|
observer.current.disconnect();
|
|
observer.current = null;
|
|
}
|
|
if (element === null) {
|
|
setEntry(null);
|
|
return;
|
|
}
|
|
observer.current = new IntersectionObserver(([_entry]) => {
|
|
setEntry(_entry);
|
|
}, options);
|
|
observer.current.observe(element);
|
|
},
|
|
[options?.rootMargin, options?.root, options?.threshold]
|
|
);
|
|
return { ref, entry };
|
|
}
|
|
|
|
export { useIntersection };
|
|
//# sourceMappingURL=use-intersection.mjs.map
|