mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 01:35:17 +01:00
35 lines
853 B
JavaScript
35 lines
853 B
JavaScript
import React, { useState, useRef, useEffect } from 'react';
|
|
|
|
function isInViewport(node) {
|
|
return (
|
|
window.pageYOffset < node.offsetTop + node.clientHeight ||
|
|
window.pageXOffset < node.offsetLeft + node.clientWidth
|
|
);
|
|
}
|
|
|
|
export default function CheckVisible({ children }) {
|
|
const [visible, setVisible] = useState(false);
|
|
const ref = useRef();
|
|
|
|
useEffect(() => {
|
|
const checkPosition = () => {
|
|
if (ref.current) {
|
|
const state = isInViewport(ref.current);
|
|
if (state !== visible) {
|
|
setVisible(state);
|
|
}
|
|
}
|
|
};
|
|
|
|
checkPosition();
|
|
|
|
window.addEventListener('scroll', checkPosition);
|
|
|
|
return () => {
|
|
window.removeEventListener('scroll', checkPosition);
|
|
};
|
|
}, [visible]);
|
|
|
|
return <div ref={ref}>{typeof children === 'function' ? children(visible) : children}</div>;
|
|
}
|