-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: previous logic fix and timeoutId refactor
- Loading branch information
Showing
3 changed files
with
32 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,44 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
export function useScrollThresholdListener(threshold: number, debounceTime = 200) { | ||
export function useScrollThresholdListener(threshold: number, debounce = 300) { | ||
const [isAboveThreshold, setIsAbove] = useState(false); | ||
const [isDebouncing, setIsDebouncing] = useState(false); | ||
|
||
const timeoutId = useRef<null | NodeJS.Timeout>(null); | ||
|
||
const handleScroll = useCallback(() => { | ||
if (isDebouncing) return; // Skip handling scroll when disabled | ||
|
||
if (window.scrollY > threshold && !isAboveThreshold) { | ||
setIsAbove(true); | ||
setIsDebouncing(true); | ||
} else if (window.scrollY <= threshold && isAboveThreshold) { | ||
setIsAbove(false); | ||
setIsDebouncing(true); | ||
} | ||
}, [threshold, isAboveThreshold, isDebouncing]); | ||
|
||
const debouncedHandleScroll = debounce(handleScroll, 20); | ||
const timeoutId = useRef<NodeJS.Timeout | null>(null); | ||
|
||
useEffect(() => { | ||
if (isDebouncing && !timeoutId.current) { | ||
timeoutId.current = setTimeout(() => { | ||
setIsDebouncing(false); | ||
timeoutId.current = null; | ||
}, debounceTime); | ||
} | ||
|
||
window.addEventListener('scroll', debouncedHandleScroll, { passive: true }); | ||
const listener = () => { | ||
const handleScroll = () => { | ||
if (isDebouncing) return; | ||
|
||
if (window.scrollY > threshold && !isAboveThreshold) { | ||
setIsAbove(true); | ||
setIsDebouncing(true); | ||
} else if (window.scrollY <= threshold && isAboveThreshold) { | ||
setIsAbove(false); | ||
setIsDebouncing(true); | ||
} | ||
}; | ||
|
||
if (isDebouncing) { | ||
if (!timeoutId.current) { | ||
timeoutId.current = setTimeout(() => { | ||
setIsDebouncing(false); | ||
timeoutId.current = null; | ||
handleScroll(); | ||
}, debounce); | ||
} | ||
} else { | ||
handleScroll(); | ||
} | ||
}; | ||
|
||
window.addEventListener('scroll', listener, { passive: true }); | ||
return () => { | ||
window.removeEventListener('scroll', debouncedHandleScroll); | ||
window.removeEventListener('scroll', listener); | ||
if (timeoutId.current) clearTimeout(timeoutId.current); | ||
}; | ||
}, [debouncedHandleScroll, isDebouncing, debounceTime]); | ||
}, [threshold, debounce, isAboveThreshold, isDebouncing]); | ||
|
||
return isAboveThreshold; | ||
} | ||
|
||
function debounce(fn: () => void, delay: number) { | ||
let timeoutId: number; | ||
return function () { | ||
if (timeoutId) { | ||
clearTimeout(timeoutId); | ||
} | ||
timeoutId = window.setTimeout(fn, delay); | ||
}; | ||
} |