From a2c25d5e4b1ae7ccbe3e64910de5472b629f49d3 Mon Sep 17 00:00:00 2001 From: Brandon Cohen Date: Mon, 23 Sep 2024 15:32:21 -0700 Subject: [PATCH] fix: correct icon showing on certain phones when not pulled (#3939) --- src/components/Layout/PullToRefresh/index.tsx | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/components/Layout/PullToRefresh/index.tsx b/src/components/Layout/PullToRefresh/index.tsx index cdedcf43c..f2a1c7bae 100644 --- a/src/components/Layout/PullToRefresh/index.tsx +++ b/src/components/Layout/PullToRefresh/index.tsx @@ -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(null); @@ -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(); @@ -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'); @@ -41,6 +43,7 @@ const PullToRefresh = () => { html.style.overscrollBehaviorY = 'none'; } } else { + setPullStartPoint(0); refreshDiv.current?.classList.remove('block'); refreshDiv.current?.classList.add('hidden'); } @@ -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; @@ -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'; @@ -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 (
{