Skip to content

Commit

Permalink
fix: correct icon showing on certain phones when not pulled (#3939)
Browse files Browse the repository at this point in the history
  • Loading branch information
OwsleyJr authored Sep 23, 2024
1 parent 1946157 commit a2c25d5
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/components/Layout/PullToRefresh/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useEffect, useRef, useState } from 'react';

const PullToRefresh = () => {
const router = useRouter();

const [pullStartPoint, setPullStartPoint] = useState(0);
const [pullChange, setPullChange] = useState(0);
const refreshDiv = useRef<HTMLDivElement>(null);
Expand All @@ -19,6 +18,7 @@ const PullToRefresh = () => {
// Reload function that is called when reload threshold has been hit
// Add loading class to determine when to add spin animation
const forceReload = () => {
setPullStartPoint(0);
refreshDiv.current?.classList.add('loading');
setTimeout(() => {
router.reload();
Expand All @@ -32,6 +32,8 @@ const PullToRefresh = () => {
const pullStart = (e: TouchEvent) => {
setPullStartPoint(e.targetTouches[0].screenY);

const html = document.querySelector('html');

if (window.scrollY === 0 && window.scrollX === 0) {
refreshDiv.current?.classList.add('block');
refreshDiv.current?.classList.remove('hidden');
Expand All @@ -41,6 +43,7 @@ const PullToRefresh = () => {
html.style.overscrollBehaviorY = 'none';
}
} else {
setPullStartPoint(0);
refreshDiv.current?.classList.remove('block');
refreshDiv.current?.classList.add('hidden');
}
Expand All @@ -49,7 +52,6 @@ const PullToRefresh = () => {
// Tracks how far we have pulled down the refresh icon
const pullDown = async (e: TouchEvent) => {
const screenY = e.targetTouches[0].screenY;

const pullLength =
pullStartPoint < screenY ? Math.abs(screenY - pullStartPoint) : 0;

Expand All @@ -59,12 +61,11 @@ const PullToRefresh = () => {
// Will reload the page if we are past the threshold
// Otherwise, we reset the pull
const pullFinish = () => {
setPullStartPoint(0);

if (pullDownReloadThreshold) {
if (pullDownReloadThreshold && pullStartPoint !== 0) {
forceReload();
} else {
setPullChange(0);
setTimeout(() => setPullStartPoint(0), 200);
}

document.body.style.touchAction = 'auto';
Expand All @@ -83,7 +84,21 @@ const PullToRefresh = () => {
window.removeEventListener('touchmove', pullDown);
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 (
<div
Expand All @@ -102,7 +117,7 @@ const PullToRefresh = () => {
<div
className={`${
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' }}
>
<ArrowPathIcon
Expand Down

0 comments on commit a2c25d5

Please sign in to comment.