|
| 1 | +import { useRef, useEffect, useState } from 'react'; |
| 2 | +import { motion, useScroll, useTransform, Variants } from 'framer-motion'; |
| 3 | +import Heading from '../../../components/Typography/heading'; |
| 4 | +import MeetupCard from '../../../components/Cards/MeetupCard'; |
| 5 | +import { MEETUP_ITEMS, ITEM_WIDTH, GAP } from '../constants'; |
| 6 | + |
| 7 | +interface HorizontalScrollSectionProps { |
| 8 | + containerVariants: Variants; |
| 9 | +} |
| 10 | + |
| 11 | +export default function HorizontalScrollSection({ |
| 12 | + containerVariants, |
| 13 | +}: HorizontalScrollSectionProps) { |
| 14 | + const containerRef = useRef<HTMLDivElement | null>(null); |
| 15 | + const [scrollHeight, setScrollHeight] = useState(0); |
| 16 | + |
| 17 | + const totalDistance = (MEETUP_ITEMS.length - 1) * (ITEM_WIDTH + GAP); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const updateHeight = () => { |
| 21 | + setScrollHeight(totalDistance + window.innerHeight); |
| 22 | + }; |
| 23 | + |
| 24 | + updateHeight(); |
| 25 | + window.addEventListener('resize', updateHeight); |
| 26 | + return () => window.removeEventListener('resize', updateHeight); |
| 27 | + }, [totalDistance]); |
| 28 | + |
| 29 | + const { scrollYProgress } = useScroll({ |
| 30 | + target: containerRef, |
| 31 | + offset: ['start start', 'end start'], |
| 32 | + }); |
| 33 | + |
| 34 | + const x = useTransform(scrollYProgress, [0, 1], [0, -totalDistance]); |
| 35 | + |
| 36 | + return ( |
| 37 | + <motion.div |
| 38 | + className="mt-24 w-full lg:mt-28 sm:mt-20" |
| 39 | + variants={containerVariants} |
| 40 | + initial="hidden" |
| 41 | + whileInView="visible" |
| 42 | + viewport={{ once: true, amount: 0.3 }} |
| 43 | + > |
| 44 | + <section className="mb-16 text-center sm:mb-10"> |
| 45 | + <Heading |
| 46 | + typeStyle="heading-lg" |
| 47 | + className="px-4 text-white font-extrabold sm:text-3xl" |
| 48 | + level="h1" |
| 49 | + > |
| 50 | + What happens at AsyncAPI Meetup? |
| 51 | + </Heading> |
| 52 | + </section> |
| 53 | + |
| 54 | + <div className="sm:hidden"> |
| 55 | + <div |
| 56 | + ref={containerRef} |
| 57 | + className="relative w-full" |
| 58 | + style={{ height: scrollHeight }} |
| 59 | + > |
| 60 | + <div className="sticky top-[100px] flex w-full items-start overflow-hidden pt-8 lg:top-20"> |
| 61 | + <motion.div |
| 62 | + className="flex gap-[30px] pl-[10vw] will-change-transform lg:gap-5 lg:pl-[6vw]" |
| 63 | + style={{ x }} |
| 64 | + > |
| 65 | + {MEETUP_ITEMS.map((item) => ( |
| 66 | + <MeetupCard key={item.id} {...item} /> |
| 67 | + ))} |
| 68 | + </motion.div> |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + |
| 73 | + <div className="hidden px-4 sm:block"> |
| 74 | + <div className="grid grid-cols-1 gap-5"> |
| 75 | + {MEETUP_ITEMS.map((item) => ( |
| 76 | + <MeetupCard key={item.id} {...item} isDesktop={false} /> |
| 77 | + ))} |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + </motion.div> |
| 81 | + ); |
| 82 | +} |
0 commit comments