forked from Github/frigate
web(test): routes/Events
This commit is contained in:
committed by
Blake Blackshear
parent
f87813805a
commit
fe59d90c51
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useMemo, useState } from 'preact/hooks';
|
||||
import { useEffect, useMemo, useRef, useState } from 'preact/hooks';
|
||||
|
||||
export function useResizeObserver(...refs) {
|
||||
const [dimensions, setDimensions] = useState(
|
||||
@@ -28,3 +28,32 @@ export function useResizeObserver(...refs) {
|
||||
|
||||
return dimensions;
|
||||
}
|
||||
|
||||
export function useIntersectionObserver() {
|
||||
const [entry, setEntry] = useState({});
|
||||
const [node, setNode] = useState(null);
|
||||
|
||||
const observer = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (observer.current) {
|
||||
observer.current.disconnect();
|
||||
}
|
||||
|
||||
observer.current = new IntersectionObserver((entries) => {
|
||||
window.requestAnimationFrame(() => {
|
||||
setEntry(entries[0]);
|
||||
});
|
||||
});
|
||||
|
||||
if (node) {
|
||||
observer.current.observe(node);
|
||||
}
|
||||
|
||||
return () => {
|
||||
observer.current.disconnect();
|
||||
};
|
||||
}, [node]);
|
||||
|
||||
return [entry, setNode];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user