From d7d838ebf8bc79bc199a5ecfe31cb9f4c52a8c2f Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Fri, 29 Dec 2023 18:29:08 +0800 Subject: [PATCH] Rebuild useScroll, less states --- src/app.css | 7 ++ src/components/timeline.jsx | 114 ++++++++++++++++++++++---------- src/utils/useScrollFn.js | 126 ++++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 33 deletions(-) create mode 100644 src/utils/useScrollFn.js diff --git a/src/app.css b/src/app.css index bb360a5c..041187e1 100644 --- a/src/app.css +++ b/src/app.css @@ -1092,6 +1092,13 @@ a[href^='http'][rel*='nofollow']:visited:not(:has(div)) { transform: translate(-50%, 0); font-size: 90%; pointer-events: auto; + transition: all 0.3s ease-in-out; + + header[hidden] & { + opacity: 0; + transform: translate(-50%, -100%) scale(0.9); + pointer-events: none; + } } .updates-button .icon { vertical-align: top; diff --git a/src/components/timeline.jsx b/src/components/timeline.jsx index 99644dca..25533018 100644 --- a/src/components/timeline.jsx +++ b/src/components/timeline.jsx @@ -12,6 +12,7 @@ import { groupBoosts, groupContext } from '../utils/timeline-utils'; import useInterval from '../utils/useInterval'; import usePageVisibility from '../utils/usePageVisibility'; import useScroll from '../utils/useScroll'; +import useScrollFn from '../utils/useScrollFn'; import Icon from './icon'; import Link from './link'; @@ -203,17 +204,47 @@ function Timeline({ } }); - const { - scrollDirection, - nearReachStart, - nearReachEnd, - reachStart, - reachEnd, - } = useScroll({ - scrollableRef, - distanceFromEnd: 2, - scrollThresholdStart: 44, - }); + // const { + // scrollDirection, + // nearReachStart, + // nearReachEnd, + // reachStart, + // reachEnd, + // } = useScroll({ + // scrollableRef, + // distanceFromEnd: 2, + // scrollThresholdStart: 44, + // }); + const headerRef = useRef(); + // const [hiddenUI, setHiddenUI] = useState(false); + const [nearReachStart, setNearReachStart] = useState(false); + useScrollFn( + { + scrollableRef, + distanceFromEnd: 2, + scrollThresholdStart: 44, + }, + ({ + scrollDirection, + nearReachStart, + nearReachEnd, + reachStart, + reachEnd, + }) => { + // setHiddenUI(scrollDirection === 'end' && !nearReachEnd); + if (headerRef.current) { + const hiddenUI = scrollDirection === 'end' && !nearReachStart; + headerRef.current.hidden = hiddenUI; + } + setNearReachStart(nearReachStart); + if (reachStart) { + loadItems(true); + } else if (nearReachEnd || (reachEnd && showMore)) { + loadItems(); + } + }, + [], + ); useEffect(() => { scrollableRef.current?.scrollTo({ top: 0 }); @@ -223,17 +254,17 @@ function Timeline({ loadItems(true); }, [refresh]); - useEffect(() => { - if (reachStart) { - loadItems(true); - } - }, [reachStart]); + // useEffect(() => { + // if (reachStart) { + // loadItems(true); + // } + // }, [reachStart]); - useEffect(() => { - if (nearReachEnd || (reachEnd && showMore)) { - loadItems(); - } - }, [nearReachEnd, showMore]); + // useEffect(() => { + // if (nearReachEnd || (reachEnd && showMore)) { + // loadItems(); + // } + // }, [nearReachEnd, showMore]); const prevView = useRef(view); useEffect(() => { @@ -304,7 +335,7 @@ function Timeline({ : null, ); - const hiddenUI = scrollDirection === 'end' && !nearReachStart; + // const hiddenUI = scrollDirection === 'end' && !nearReachStart; return ( @@ -321,7 +352,8 @@ function Timeline({ >
{items.length > 0 && uiState !== 'loading' && - !hiddenUI && + // !hiddenUI && showNew && ( {' '}