+ {/* Add a minimal rotation here to trick the browser to go into hardware acceleration mode
+ this will make the animation a little smoother, specially for Firefox*/}
diff --git a/src/utils/browser.ts b/src/utils/browser.ts
deleted file mode 100644
index c0a393fa..00000000
--- a/src/utils/browser.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export function isFirefox() {
- return typeof navigator !== 'undefined' && navigator.userAgent.toLowerCase().includes('firefox');
-}
diff --git a/src/utils/useScrollListener.ts b/src/utils/useScrollListener.ts
index a23981a1..ba73add0 100644
--- a/src/utils/useScrollListener.ts
+++ b/src/utils/useScrollListener.ts
@@ -1,19 +1,16 @@
-import { useEffect, useState } from 'react';
+import { useEffect, useRef, useState } from 'react';
-import { isFirefox } from './browser';
-
-export function useScrollThresholdListener(threshold: number, debounce = 500) {
+export function useScrollThresholdListener(threshold: number, debounce = 300) {
const [isAboveThreshold, setIsAbove] = useState(false);
const [isDebouncing, setIsDebouncing] = useState(false);
- useEffect(() => {
- let timeoutId: NodeJS.Timeout | null;
+ const timeoutId = useRef(null);
+ useEffect(() => {
const listener = () => {
- // TODO find a way to make this animation smooth in Firefox
- if (isFirefox()) return;
-
const handleScroll = () => {
+ if (isDebouncing) return;
+
if (window.scrollY > threshold && !isAboveThreshold) {
setIsAbove(true);
setIsDebouncing(true);
@@ -24,10 +21,10 @@ export function useScrollThresholdListener(threshold: number, debounce = 500) {
};
if (isDebouncing) {
- if (!timeoutId) {
- setTimeout(() => {
+ if (!timeoutId.current) {
+ timeoutId.current = setTimeout(() => {
setIsDebouncing(false);
- timeoutId = null;
+ timeoutId.current = null;
handleScroll();
}, debounce);
}
@@ -39,7 +36,7 @@ export function useScrollThresholdListener(threshold: number, debounce = 500) {
window.addEventListener('scroll', listener, { passive: true });
return () => {
window.removeEventListener('scroll', listener);
- if (timeoutId) clearTimeout(timeoutId);
+ if (timeoutId.current) clearTimeout(timeoutId.current);
};
}, [threshold, debounce, isAboveThreshold, isDebouncing]);