|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React from "react"; |
| 4 | +import Image from "next/image"; |
| 5 | + |
| 6 | +import { Media } from "@/payload-types"; |
| 7 | +import { Button } from "../ui/button"; |
| 8 | + |
| 9 | +import { motion, useTransform, useScroll, useSpring } from "framer-motion"; |
| 10 | +import parallaxConfig from "@/config/parallax"; |
| 11 | + |
| 12 | +export type CardProps = { |
| 13 | + icon: React.ReactNode; |
| 14 | + background: string | null; |
| 15 | + alt: string; |
| 16 | + title: string; |
| 17 | + summary: string; |
| 18 | + description?: string; |
| 19 | + link?: string | undefined; |
| 20 | + sponsorLogos?: { logo?: Media | string | null }[] | null; |
| 21 | +}; |
| 22 | + |
| 23 | +const ScrollingLogos = ({ logos }: { logos: { logo?: Media | string | null }[] }) => { |
| 24 | + const containerRef = React.useRef<HTMLDivElement>(null); |
| 25 | + const [duplicateCount, setDuplicateCount] = React.useState(2); |
| 26 | + const validLogos = logos.filter((item) => typeof item.logo === "object" && item.logo?.url); // Filter out invalid logos |
| 27 | + |
| 28 | + // Calculate how many times to duplicate logos based on container width |
| 29 | + React.useEffect(() => { |
| 30 | + if (!containerRef.current || validLogos.length === 0) return; |
| 31 | + |
| 32 | + const containerWidth = containerRef.current.offsetWidth; |
| 33 | + const itemWidth = 88; // 64px logo + 24px gap |
| 34 | + const totalLogosWidth = itemWidth * validLogos.length; |
| 35 | + |
| 36 | + // Duplicate enough times to fill at least 2x the container width |
| 37 | + const needed = Math.ceil((containerWidth * 2) / totalLogosWidth); |
| 38 | + setDuplicateCount(Math.max(2, needed)); |
| 39 | + }, [validLogos.length]); |
| 40 | + |
| 41 | + if (validLogos.length === 0) return null; |
| 42 | + |
| 43 | + const duplicatedLogos = Array(duplicateCount).fill(validLogos).flat(); |
| 44 | + const duration = 40 + duplicatedLogos.length * 0.3; // Seconds to complete one full loop - increase/decrease the base number to speed up/slow down one loop |
| 45 | + |
| 46 | + const LogoSet = ({ keyPrefix }: { keyPrefix: string }) => ( |
| 47 | + <motion.div |
| 48 | + className="flex h-full shrink-0 items-center gap-23 pr-23" |
| 49 | + animate={{ x: [0, "-100%"] }} |
| 50 | + transition={{ |
| 51 | + x: { |
| 52 | + repeat: Infinity, |
| 53 | + repeatType: "loop", |
| 54 | + duration: duration, |
| 55 | + ease: "linear", |
| 56 | + }, |
| 57 | + }} |
| 58 | + > |
| 59 | + {duplicatedLogos.map((item, index) => { |
| 60 | + const logo = item.logo as Media; |
| 61 | + return ( |
| 62 | + <div |
| 63 | + key={`${keyPrefix}-${index}`} |
| 64 | + className="relative h-full shrink-0" |
| 65 | + style={{ aspectRatio: `${logo.width || 1} / ${logo.height || 1}` }} |
| 66 | + > |
| 67 | + <Image |
| 68 | + src={logo.url!} |
| 69 | + alt={logo.alt || `sponsor ${(index % validLogos.length) + 1}`} |
| 70 | + fill |
| 71 | + className="rounded-md object-contain" |
| 72 | + quality={90} |
| 73 | + /> |
| 74 | + </div> |
| 75 | + ); |
| 76 | + })} |
| 77 | + </motion.div> |
| 78 | + ); |
| 79 | + |
| 80 | + return ( |
| 81 | + <div ref={containerRef} className="relative h-full w-full overflow-hidden"> |
| 82 | + <div className="flex h-full"> |
| 83 | + <LogoSet keyPrefix="first" /> |
| 84 | + <LogoSet keyPrefix="second" /> |
| 85 | + </div> |
| 86 | + |
| 87 | + <div className="pointer-events-none absolute inset-y-0 left-0 w-8 bg-linear-to-r from-(--lightblue) to-transparent" /> |
| 88 | + <div className="pointer-events-none absolute inset-y-0 right-0 w-8 bg-linear-to-l from-(--lightblue) to-transparent" /> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +const Card = ({ |
| 94 | + icon, |
| 95 | + background, |
| 96 | + alt, |
| 97 | + title, |
| 98 | + summary, |
| 99 | + description, |
| 100 | + link, |
| 101 | + sponsorLogos, |
| 102 | +}: CardProps) => { |
| 103 | + const isLinked = link ? link.trim() !== "" : false; |
| 104 | + const isSponsored = sponsorLogos && sponsorLogos.length > 0; |
| 105 | + |
| 106 | + const { rangeIn, rangeOut, spring } = parallaxConfig; |
| 107 | + const { scrollY } = useScroll(); |
| 108 | + // image parallax effect scroll speed |
| 109 | + const rawY = useTransform(scrollY, [0, rangeIn], [0, rangeOut]); |
| 110 | + // smooth motion |
| 111 | + const y = useSpring(rawY, spring); |
| 112 | + |
| 113 | + return ( |
| 114 | + <div className="group relative block h-[400px] w-full overflow-hidden rounded-lg px-18 py-18 text-(--lightblue)"> |
| 115 | + {/* On Display: Background Image */} |
| 116 | + <motion.div |
| 117 | + className="absolute inset-x-0 -inset-y-[20%] z-0 will-change-transform" |
| 118 | + style={{ y }} |
| 119 | + > |
| 120 | + <Image |
| 121 | + src={background!} |
| 122 | + alt={alt} |
| 123 | + fill |
| 124 | + priority |
| 125 | + quality={90} |
| 126 | + sizes="(max-width: 768px) 110vw, 55vw" |
| 127 | + className="object-cover object-center" |
| 128 | + /> |
| 129 | + </motion.div> |
| 130 | + |
| 131 | + {/* On Display: Content */} |
| 132 | + <div className="relative z-10 flex h-full flex-col items-center text-center"> |
| 133 | + <div className="flex h-1/2 w-full flex-col items-center justify-between"> |
| 134 | + <div className="flex justify-center">{icon}</div> |
| 135 | + |
| 136 | + <h1 className="m-0! text-4xl! font-semibold!">{title}</h1> |
| 137 | + </div> |
| 138 | + |
| 139 | + <div className="mt-4 flex h-1/2 w-full items-start"> |
| 140 | + <p className="text-base">{summary}</p> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + |
| 144 | + {/* On Hover: Background Colour */} |
| 145 | + <div className="absolute inset-0 z-10 transition-colors duration-500 group-hover:bg-(--navy)"></div> |
| 146 | + |
| 147 | + {/* On Hover: Content */} |
| 148 | + <div className="absolute inset-0 z-20 flex flex-col items-center justify-start px-18 py-18 text-center opacity-0 transition-opacity duration-500 group-hover:opacity-100"> |
| 149 | + <div className="mb-4 flex justify-center">{icon}</div> |
| 150 | + |
| 151 | + {isSponsored && ( |
| 152 | + <div className="relative mb-4 flex h-24 w-full items-center overflow-hidden rounded-md bg-(--lightblue) py-3"> |
| 153 | + {/* Visible Content */} |
| 154 | + <ScrollingLogos logos={sponsorLogos!} /> |
| 155 | + </div> |
| 156 | + )} |
| 157 | + |
| 158 | + {isLinked ? ( |
| 159 | + <Button variant="link" asChild className="mt-7"> |
| 160 | + <a href={link} target="_blank" rel="noopener noreferrer"> |
| 161 | + <h1 className="m-0! text-3xl! font-semibold!">{description}</h1> |
| 162 | + </a> |
| 163 | + </Button> |
| 164 | + ) : ( |
| 165 | + <p className="text-center text-base">{description}</p> |
| 166 | + )} |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + ); |
| 170 | +}; |
| 171 | + |
| 172 | +export default Card; |
0 commit comments