From 4e6e4a00164356202083a9202803a95aa648d021 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Thu, 23 May 2024 11:18:21 +0000 Subject: [PATCH] feat: add progress at the bottom of the slide --- .../src/components/(marketing)/carousel.tsx | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/apps/marketing/src/components/(marketing)/carousel.tsx b/apps/marketing/src/components/(marketing)/carousel.tsx index e58cfc67b..54c693e26 100644 --- a/apps/marketing/src/components/(marketing)/carousel.tsx +++ b/apps/marketing/src/components/(marketing)/carousel.tsx @@ -6,6 +6,7 @@ import Autoplay from 'embla-carousel-autoplay'; import useEmblaCarousel from 'embla-carousel-react'; import { Card } from '@documenso/ui/primitives/card'; +import { Progress } from '@documenso/ui/primitives/progress'; import { Thumb } from './thumb'; @@ -51,6 +52,8 @@ export const Carousel = () => { const slides = SLIDES; const [_isPlaying, setIsPlaying] = useState(false); const [selectedIndex, setSelectedIndex] = useState(0); + const [progress, setProgress] = React.useState(0); + const videoRefs = useRef<(HTMLVideoElement | null)[]>([]); const [emblaRef, emblaApi] = useEmblaCarousel({ loop: true }, [ Autoplay({ playOnInit: true, delay: 5000 }), ]); @@ -75,9 +78,13 @@ export const Carousel = () => { if (!emblaApi || !emblaThumbsApi) return; setSelectedIndex(emblaApi.selectedScrollSnap()); emblaThumbsApi.scrollTo(emblaApi.selectedScrollSnap()); + + resetProgress(); }, [emblaApi, emblaThumbsApi, setSelectedIndex]); - const videoRefs = useRef<(HTMLVideoElement | null)[]>([]); + const resetProgress = useCallback(() => { + setProgress(0); + }, []); useEffect(() => { const observer = new IntersectionObserver( @@ -128,6 +135,23 @@ export const Carousel = () => { .on('reInit', () => setIsPlaying(autoplay.isPlaying())); }, [emblaApi]); + useEffect(() => { + const updateInterval = 50; + const increment = 100 / (5000 / updateInterval); + + const timer = setInterval(() => { + setProgress((prevProgress) => { + if (prevProgress >= 100) { + clearInterval(timer); + return 100; + } + return prevProgress + increment; + }); + }, updateInterval); + + return () => clearInterval(timer); + }, [selectedIndex]); + return ( <> @@ -150,6 +174,13 @@ export const Carousel = () => { ))} + +
+ + {selectedIndex + 1}/{slides.length + 1} + + +