import 'swiper/css' import { useBreakpoint } from 'common' import { domAnimation, LazyMotion, m, useAnimation, useInView } from 'motion/react' import React, { useEffect, useRef, useState } from 'react' import { Swiper, SwiperSlide } from 'swiper/react' import { cn } from 'ui' import Panel from '../Panel' interface PanelProps { id: string label: string | React.ReactNode paragraph?: string icon?: React.ReactNode isActive?: boolean isInView?: boolean onClick?: VoidFunction progress?: number | undefined intervalDuration?: number image?: any } const TimedPanel = ({ isActive, label, paragraph, onClick, progress, intervalDuration, image, isInView, }: PanelProps) => { const isSm = useBreakpoint() const Image: any = image ?? null return ( ) } interface Props { panels: PanelProps[] intervalDuration?: number updateFrequency?: number isInView?: boolean } const TimedAccordionPanels = ({ panels, intervalDuration = 10, updateFrequency = 10 }: Props) => { const ref = useRef(null) const isInView = useInView(ref, { margin: '-25%' }) const [activeTab, setActiveTab] = useState(0) const [progress, setProgress] = useState(0) const [apiSwiper, setApiSwiper] = useState(undefined) const controls = useAnimation() useEffect(() => { if (!apiSwiper) return // @ts-ignore apiSwiper.slideTo(activeTab) }, [activeTab]) const restartInterval = (tabIndex: number) => { setProgress(0) setActiveTab(tabIndex) } const animation = { width: '100%', transition: { duration: intervalDuration, ease: 'linear' }, } useEffect(() => { // pause timer when component is not in view if (!isInView) { controls.stop() setProgress(progress) return } const progressIncrement = (100 / intervalDuration) * (updateFrequency / 1000) controls.start(animation) const progressInterval = setInterval(() => { setProgress((prevProgress) => (prevProgress + progressIncrement) % 101) }, updateFrequency) return () => { clearInterval(progressInterval) setProgress(0) } }, [isInView]) useEffect(() => { if (progress >= 100.9) { setActiveTab((prevActiveTab) => (prevActiveTab === panels.length - 1 ? 0 : prevActiveTab + 1)) } }, [progress]) const handleTabClick = (tabIndex: number) => { // Disable trigger if tab is already active if (tabIndex === activeTab) return restartInterval(tabIndex) } return (
{panels.map((panel, index) => ( handleTabClick(index)} progress={index === activeTab ? progress : undefined} intervalDuration={intervalDuration} {...panel} /> ))}
handleTabClick(s.activeIndex)} slidesPerView={1.1} speed={300} > {panels.map((panel, index) => ( handleTabClick(index)} progress={index === activeTab ? progress : undefined} intervalDuration={intervalDuration} {...panel} /> ))}
) } export default TimedAccordionPanels