diff --git a/surfsense_web/components/ui/hero-carousel.tsx b/surfsense_web/components/ui/hero-carousel.tsx index cfafd68f6..327970de2 100644 --- a/surfsense_web/components/ui/hero-carousel.tsx +++ b/surfsense_web/components/ui/hero-carousel.tsx @@ -152,13 +152,38 @@ function usePrefetchVideos() { }, []); } +const AUTOPLAY_MS = 6000; + function HeroCarousel() { const [activeIndex, setActiveIndex] = useState(0); const [isGifExpanded, setIsGifExpanded] = useState(false); + const [isHovered, setIsHovered] = useState(false); + const [isTabVisible, setIsTabVisible] = useState(true); const directionRef = useRef<"forward" | "backward">("forward"); usePrefetchVideos(); + const shouldAutoPlay = !isGifExpanded && !isHovered && isTabVisible; + + useEffect(() => { + if (!shouldAutoPlay) return; + + const id = setTimeout(() => { + directionRef.current = "forward"; + setActiveIndex((prev) => + prev >= carouselItems.length - 1 ? 0 : prev + 1 + ); + }, AUTOPLAY_MS); + + return () => clearTimeout(id); + }, [activeIndex, shouldAutoPlay]); + + useEffect(() => { + const handler = () => setIsTabVisible(!document.hidden); + document.addEventListener("visibilitychange", handler); + return () => document.removeEventListener("visibilitychange", handler); + }, []); + const goTo = useCallback( (newIndex: number) => { directionRef.current = newIndex >= activeIndex ? "forward" : "backward"; @@ -179,7 +204,11 @@ function HeroCarousel() { const isForward = directionRef.current === "forward"; return ( -
+
setIsHovered(true)} + onMouseLeave={() => setIsHovered(false)} + >
!isGifExpanded && goTo(i)} - className={`h-2 rounded-full transition-all duration-300 ${ + className={`relative h-2 overflow-hidden rounded-full transition-all duration-300 ${ i === activeIndex - ? "w-6 bg-neutral-900 dark:bg-white" + ? shouldAutoPlay + ? "w-6 bg-neutral-300 dark:bg-neutral-600" + : "w-6 bg-neutral-900 dark:bg-white" : "w-2 bg-neutral-300 hover:bg-neutral-400 dark:bg-neutral-600 dark:hover:bg-neutral-500" }`} aria-label={`Go to slide ${i + 1}`} - /> + > + {i === activeIndex && shouldAutoPlay && ( + + )} + ))}