|
|
@ -4,7 +4,6 @@ import { useEffect, useRef, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
|
|
const PullToRefresh = () => {
|
|
|
|
const PullToRefresh = () => {
|
|
|
|
const router = useRouter();
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
|
|
const [pullStartPoint, setPullStartPoint] = useState(0);
|
|
|
|
const [pullStartPoint, setPullStartPoint] = useState(0);
|
|
|
|
const [pullChange, setPullChange] = useState(0);
|
|
|
|
const [pullChange, setPullChange] = useState(0);
|
|
|
|
const refreshDiv = useRef<HTMLDivElement>(null);
|
|
|
|
const refreshDiv = useRef<HTMLDivElement>(null);
|
|
|
@ -19,6 +18,7 @@ const PullToRefresh = () => {
|
|
|
|
// Reload function that is called when reload threshold has been hit
|
|
|
|
// Reload function that is called when reload threshold has been hit
|
|
|
|
// Add loading class to determine when to add spin animation
|
|
|
|
// Add loading class to determine when to add spin animation
|
|
|
|
const forceReload = () => {
|
|
|
|
const forceReload = () => {
|
|
|
|
|
|
|
|
setPullStartPoint(0);
|
|
|
|
refreshDiv.current?.classList.add('loading');
|
|
|
|
refreshDiv.current?.classList.add('loading');
|
|
|
|
setTimeout(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
router.reload();
|
|
|
|
router.reload();
|
|
|
@ -32,6 +32,8 @@ const PullToRefresh = () => {
|
|
|
|
const pullStart = (e: TouchEvent) => {
|
|
|
|
const pullStart = (e: TouchEvent) => {
|
|
|
|
setPullStartPoint(e.targetTouches[0].screenY);
|
|
|
|
setPullStartPoint(e.targetTouches[0].screenY);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const html = document.querySelector('html');
|
|
|
|
|
|
|
|
|
|
|
|
if (window.scrollY === 0 && window.scrollX === 0) {
|
|
|
|
if (window.scrollY === 0 && window.scrollX === 0) {
|
|
|
|
refreshDiv.current?.classList.add('block');
|
|
|
|
refreshDiv.current?.classList.add('block');
|
|
|
|
refreshDiv.current?.classList.remove('hidden');
|
|
|
|
refreshDiv.current?.classList.remove('hidden');
|
|
|
@ -41,6 +43,7 @@ const PullToRefresh = () => {
|
|
|
|
html.style.overscrollBehaviorY = 'none';
|
|
|
|
html.style.overscrollBehaviorY = 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
setPullStartPoint(0);
|
|
|
|
refreshDiv.current?.classList.remove('block');
|
|
|
|
refreshDiv.current?.classList.remove('block');
|
|
|
|
refreshDiv.current?.classList.add('hidden');
|
|
|
|
refreshDiv.current?.classList.add('hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -49,7 +52,6 @@ const PullToRefresh = () => {
|
|
|
|
// Tracks how far we have pulled down the refresh icon
|
|
|
|
// Tracks how far we have pulled down the refresh icon
|
|
|
|
const pullDown = async (e: TouchEvent) => {
|
|
|
|
const pullDown = async (e: TouchEvent) => {
|
|
|
|
const screenY = e.targetTouches[0].screenY;
|
|
|
|
const screenY = e.targetTouches[0].screenY;
|
|
|
|
|
|
|
|
|
|
|
|
const pullLength =
|
|
|
|
const pullLength =
|
|
|
|
pullStartPoint < screenY ? Math.abs(screenY - pullStartPoint) : 0;
|
|
|
|
pullStartPoint < screenY ? Math.abs(screenY - pullStartPoint) : 0;
|
|
|
|
|
|
|
|
|
|
|
@ -59,12 +61,11 @@ const PullToRefresh = () => {
|
|
|
|
// Will reload the page if we are past the threshold
|
|
|
|
// Will reload the page if we are past the threshold
|
|
|
|
// Otherwise, we reset the pull
|
|
|
|
// Otherwise, we reset the pull
|
|
|
|
const pullFinish = () => {
|
|
|
|
const pullFinish = () => {
|
|
|
|
setPullStartPoint(0);
|
|
|
|
if (pullDownReloadThreshold && pullStartPoint !== 0) {
|
|
|
|
|
|
|
|
|
|
|
|
if (pullDownReloadThreshold) {
|
|
|
|
|
|
|
|
forceReload();
|
|
|
|
forceReload();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
setPullChange(0);
|
|
|
|
setPullChange(0);
|
|
|
|
|
|
|
|
setTimeout(() => setPullStartPoint(0), 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
document.body.style.touchAction = 'auto';
|
|
|
|
document.body.style.touchAction = 'auto';
|
|
|
@ -83,7 +84,21 @@ const PullToRefresh = () => {
|
|
|
|
window.removeEventListener('touchmove', pullDown);
|
|
|
|
window.removeEventListener('touchmove', pullDown);
|
|
|
|
window.removeEventListener('touchend', pullFinish);
|
|
|
|
window.removeEventListener('touchend', pullFinish);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}, [pullDownInitThreshold, pullDownReloadThreshold, pullStartPoint, router]);
|
|
|
|
}, [
|
|
|
|
|
|
|
|
pullDownInitThreshold,
|
|
|
|
|
|
|
|
pullDownReloadThreshold,
|
|
|
|
|
|
|
|
pullStartPoint,
|
|
|
|
|
|
|
|
refreshDiv,
|
|
|
|
|
|
|
|
router,
|
|
|
|
|
|
|
|
setPullStartPoint,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
pullStartPoint === 0 &&
|
|
|
|
|
|
|
|
!refreshDiv.current?.classList.contains('loading')
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
<div
|
|
|
@ -102,7 +117,7 @@ const PullToRefresh = () => {
|
|
|
|
<div
|
|
|
|
<div
|
|
|
|
className={`${
|
|
|
|
className={`${
|
|
|
|
refreshDiv.current?.classList.contains('loading') && 'animate-spin'
|
|
|
|
refreshDiv.current?.classList.contains('loading') && 'animate-spin'
|
|
|
|
} relative -top-24 h-9 w-9 rounded-full border-4 border-gray-800 bg-gray-800 shadow-md shadow-black ring-1 ring-gray-700`}
|
|
|
|
} relative -top-28 h-9 w-9 rounded-full border-4 border-gray-800 bg-gray-800 shadow-md shadow-black ring-1 ring-gray-700`}
|
|
|
|
style={{ animationDirection: 'reverse' }}
|
|
|
|
style={{ animationDirection: 'reverse' }}
|
|
|
|
>
|
|
|
|
>
|
|
|
|
<ArrowPathIcon
|
|
|
|
<ArrowPathIcon
|
|
|
|