From 54b1a9fa23bcd515d31cf9353b18ee00ee988cb4 Mon Sep 17 00:00:00 2001 From: OwsleyJr Date: Thu, 16 Nov 2023 10:22:16 -0800 Subject: [PATCH] refactor: cleaned up code and improved readability --- src/components/Layout/PullToRefresh/index.tsx | 60 ++++++++++--------- src/styles/globals.css | 19 ------ 2 files changed, 33 insertions(+), 46 deletions(-) diff --git a/src/components/Layout/PullToRefresh/index.tsx b/src/components/Layout/PullToRefresh/index.tsx index cdedcf43..0249ff57 100644 --- a/src/components/Layout/PullToRefresh/index.tsx +++ b/src/components/Layout/PullToRefresh/index.tsx @@ -4,17 +4,15 @@ import { useEffect, useRef, useState } from 'react'; const PullToRefresh = () => { const router = useRouter(); - - const [pullStartPoint, setPullStartPoint] = useState(0); - const [pullChange, setPullChange] = useState(0); + const [pullStartPoint, setPullStartPoint] = useState(0); + const [pullChange, setPullChange] = useState(0); + const [iconPlacement, setIconPlacement] = useState(0); const refreshDiv = useRef(null); - // Various pull down thresholds that determine icon location - const pullDownInitThreshold = pullChange > 20; - const pullDownStopThreshold = 120; - const pullDownReloadThreshold = pullChange > 340; - const pullDownIconLocation = pullChange / 3; - + // If pull change is greater than 20, we have passed + // the initialization threshold of the pull to refresh + // (the icon will start to show). If it hits 120, + // the reload will start on release useEffect(() => { // Reload function that is called when reload threshold has been hit // Add loading class to determine when to add spin animation @@ -37,8 +35,9 @@ const PullToRefresh = () => { refreshDiv.current?.classList.remove('hidden'); document.body.style.touchAction = 'none'; document.body.style.overscrollBehavior = 'none'; + if (html) { - html.style.overscrollBehaviorY = 'none'; + html.style.overscrollBehavior = 'none'; } } else { refreshDiv.current?.classList.remove('block'); @@ -47,13 +46,17 @@ const PullToRefresh = () => { }; // Tracks how far we have pulled down the refresh icon - const pullDown = async (e: TouchEvent) => { + const pullDown = (e: TouchEvent) => { const screenY = e.targetTouches[0].screenY; - const pullLength = pullStartPoint < screenY ? Math.abs(screenY - pullStartPoint) : 0; setPullChange(pullLength); + + // Lock the body when the icon is shown + if (iconPlacement > 50) { + document.body.style.overflow = 'hidden'; + } }; // Will reload the page if we are past the threshold @@ -61,16 +64,17 @@ const PullToRefresh = () => { const pullFinish = () => { setPullStartPoint(0); - if (pullDownReloadThreshold) { + if (pullChange > 340) { forceReload(); } else { setPullChange(0); } document.body.style.touchAction = 'auto'; - document.body.style.overscrollBehaviorY = 'auto'; + document.body.style.overscrollBehavior = 'auto'; + document.body.style.overflow = 'scroll'; if (html) { - html.style.overscrollBehaviorY = 'auto'; + html.style.overscrollBehavior = 'auto'; } }; @@ -78,26 +82,28 @@ const PullToRefresh = () => { window.addEventListener('touchmove', pullDown, { passive: false }); window.addEventListener('touchend', pullFinish, { passive: false }); + // Determines the position of the icon based on + // the pull-down distance to touch + if (pullChange / 3 < 120 && pullChange > 20) { + setIconPlacement(pullChange / 3); + } else if (pullChange > 20) { + setIconPlacement(120); + } else { + setIconPlacement(0); + } + return () => { window.removeEventListener('touchstart', pullStart); window.removeEventListener('touchmove', pullDown); window.removeEventListener('touchend', pullFinish); }; - }, [pullDownInitThreshold, pullDownReloadThreshold, pullStartPoint, router]); + }, [iconPlacement, pullChange, pullStartPoint, router]); return (
{ > 340 && 'rotate-180' } text-indigo-500 transition-all duration-300`} />
diff --git a/src/styles/globals.css b/src/styles/globals.css index 8110e87e..04f53ac8 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -524,22 +524,3 @@ height: calc(4rem + env(safe-area-inset-bottom)); } } - -.ptr--ptr { - box-shadow: initial !important; - position: absolute !important; - z-index: 30 !important; -} - -.ptr--refresh { - overflow: visible !important; - z-index: 30 !important; -} - -.ptr--pull { - z-index: 30 !important; -} - -.ptr--box { - margin-bottom: -13px !important; -}