@resourge/react-fetch
Version:
[](LICENSE)
69 lines (66 loc) • 1.95 kB
JavaScript
/**
* react-fetch v1.41.3
*
* Copyright (c) resourge.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
import { useRef, useEffect } from 'react';
import { jsx } from 'react/jsx-runtime';
function getOverflowParent(element) {
while (element && element !== document.body) {
const overflowY = window.getComputedStyle(element).overflowY;
const overflowX = window.getComputedStyle(element).overflowX;
if ((overflowY === 'auto' || overflowY === 'scroll' || overflowY === 'hidden') && element.scrollHeight > element.clientHeight || (overflowX === 'auto' || overflowX === 'scroll' || overflowX === 'hidden') && element.scrollWidth > element.clientWidth) {
return element;
}
element = element.parentElement;
}
return null;
}
function RefreshControl({
context,
root = null,
detectionMargin,
renderComponent,
preload
}) {
const ref = useRef(null);
useEffect(() => {
if (ref.current) {
var _ref;
const _root = (_ref = root && root.current ? root.current : root) != null ? _ref : getOverflowParent(ref.current);
const observer = new IntersectionObserver(entries => {
if (entries[0].isIntersecting) {
if (preload) {
context.preload();
} else {
context.loadMore();
}
}
}, {
root: _root,
rootMargin: detectionMargin,
threshold: 1.0
});
observer.observe(ref.current);
return () => {
observer.disconnect();
};
}
}, [detectionMargin, context.data.length]);
return jsx("div", {
ref: ref,
children: renderComponent && renderComponent({
isLastIncomplete: context.isLastIncomplete,
onClick: () => {
context.loadMore();
}
})
});
}
export { RefreshControl as default };
//# sourceMappingURL=RefreshControl.js.map