Improve mobile scrolling behavior by debouncing category index updates
Add a scroll timer to debounce the calculation and update of the current category index on mobile devices, preventing visual flickering during scrolling. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 2cd2c0bc-434c-4bc9-ad3f-b99d3897a0d1 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/8cc42625-c1f5-4e43-99bd-77f2c4dedee2/2cd2c0bc-434c-4bc9-ad3f-b99d3897a0d1/OdlP8Wj
This commit is contained in:
parent
c89bd4a1be
commit
aaf1231ed0
@ -195,6 +195,7 @@ function CategoryRow({ category, onVideoClick, hideScrollButtons = false }: Cate
|
|||||||
const [currentIndex, setCurrentIndex] = useState(0);
|
const [currentIndex, setCurrentIndex] = useState(0);
|
||||||
const [touchStart, setTouchStart] = useState(0);
|
const [touchStart, setTouchStart] = useState(0);
|
||||||
const [touchEnd, setTouchEnd] = useState(0);
|
const [touchEnd, setTouchEnd] = useState(0);
|
||||||
|
const [scrollTimer, setScrollTimer] = useState<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
const checkScrollButtons = () => {
|
const checkScrollButtons = () => {
|
||||||
if (scrollRef.current) {
|
if (scrollRef.current) {
|
||||||
@ -260,26 +261,44 @@ function CategoryRow({ category, onVideoClick, hideScrollButtons = false }: Cate
|
|||||||
};
|
};
|
||||||
}, [category.videos]);
|
}, [category.videos]);
|
||||||
|
|
||||||
|
// Cleanup timer on unmount
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
if (scrollTimer) {
|
||||||
|
clearTimeout(scrollTimer);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [scrollTimer]);
|
||||||
|
|
||||||
const handleScroll = () => {
|
const handleScroll = () => {
|
||||||
checkScrollButtons();
|
checkScrollButtons();
|
||||||
|
|
||||||
// Calculate current index for mobile dots
|
// Clear existing timer
|
||||||
|
if (scrollTimer) {
|
||||||
|
clearTimeout(scrollTimer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate current index for mobile dots with delay
|
||||||
if (scrollRef.current) {
|
if (scrollRef.current) {
|
||||||
const containerWidth = scrollRef.current.clientWidth;
|
const containerWidth = scrollRef.current.clientWidth;
|
||||||
const scrollLeft = scrollRef.current.scrollLeft;
|
const scrollLeft = scrollRef.current.scrollLeft;
|
||||||
const isMobile = window.innerWidth < 768;
|
const isMobile = window.innerWidth < 768;
|
||||||
|
|
||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
// On mobile, calculate which card is most visible
|
// Debounce the dot update to prevent flickering during scroll
|
||||||
const cardWidth = containerWidth - 24; // Account for container margins (12px each side)
|
const timer = setTimeout(() => {
|
||||||
const scrollProgress = scrollLeft / cardWidth;
|
const cardWidth = containerWidth - 24; // Account for container margins (12px each side)
|
||||||
const newIndex = Math.round(scrollProgress);
|
const scrollProgress = scrollLeft / cardWidth;
|
||||||
const clampedIndex = Math.min(Math.max(newIndex, 0), 9); // Ensure 0-9 range
|
const newIndex = Math.round(scrollProgress);
|
||||||
|
const clampedIndex = Math.min(Math.max(newIndex, 0), 9); // Ensure 0-9 range
|
||||||
|
|
||||||
|
// Only update if index actually changed
|
||||||
|
if (clampedIndex !== currentIndex) {
|
||||||
|
setCurrentIndex(clampedIndex);
|
||||||
|
}
|
||||||
|
}, 100); // 100ms delay for stable positioning
|
||||||
|
|
||||||
// Only update if index actually changed to prevent flickering
|
setScrollTimer(timer);
|
||||||
if (clampedIndex !== currentIndex) {
|
|
||||||
setCurrentIndex(clampedIndex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user