1/*
2 ! Add the following to your .globals.css
3
4 .animated-cards::before {
5 @apply pointer-events-none absolute select-none rounded-3xl opacity-0 transition-opacity duration-300 hover:opacity-100;
6 background: radial-gradient(
7 1000px circle at var(--x) var(--y),
8 #c9ee80 0,
9 #eebbe2 10%,
10 #adc0ec 25%,
11 #c9ee80 35%,
12 rgba(255, 255, 255, 0) 50%,
13 transparent 80%
14 );
15 z-index: -1;
16 content: "";
17 inset: -1px;
18 }
19*/
20"use client"
21
22import {
23 forwardRef,
24 useCallback,
25 useEffect,
26 useRef,
27 useState,
28 type MouseEvent,
29} from "react"
30import Image, { type StaticImageData } from "next/image"
31import clsx from "clsx"
32import {
33 AnimatePresence,
34 motion,
35 useMotionTemplate,
36 useMotionValue,
37 type MotionStyle,
38 type MotionValue,
39 type Variants,
40} from "motion/react"
41import Balancer from "react-wrap-balancer"
42
43import { cn } from "@/lib/utils"
44
45// Types
46type WrapperStyle = MotionStyle & {
47 "--x": MotionValue<string>
48 "--y": MotionValue<string>
49}
50
51interface CardProps {
52 title: string
53 description: string
54 bgClass?: string
55}
56
57interface ImageSet {
58 step1dark1?: StaticImageData | string
59 step1dark2?: StaticImageData | string
60 step1light1: StaticImageData | string
61 step1light2: StaticImageData | string
62 step2dark1?: StaticImageData | string
63 step2dark2?: StaticImageData | string
64 step2light1: StaticImageData | string
65 step2light2: StaticImageData | string
66 step3dark?: StaticImageData | string
67 step3light: StaticImageData | string
68 step4light: StaticImageData | string
69 alt: string
70}
71
72interface FeatureCarouselProps extends CardProps {
73 step1img1Class?: string
74 step1img2Class?: string
75 step2img1Class?: string
76 step2img2Class?: string
77 step3imgClass?: string
78 step4imgClass?: string
79 image: ImageSet
80}
81
82interface StepImageProps {
83 src: StaticImageData | string
84 alt: string
85 className?: string
86 style?: React.CSSProperties
87 width?: number
88 height?: number
89}
90
91interface Step {
92 id: string
93 name: string
94 title: string
95 description: string
96}
97
98// Constants
99const TOTAL_STEPS = 4
100
101const steps = [
102 {
103 id: "1",
104 name: "Step 1",
105 title: "Feature 1",
106 description: "Feature 1 description ",
107 },
108 {
109 id: "2",
110 name: "Step 2",
111 title: "Feature 2",
112 description: "Feature 2 description",
113 },
114 {
115 id: "3",
116 name: "Step 3",
117 title: "Feature 3",
118 description: "Feature 3 description",
119 },
120 {
121 id: "4",
122 name: "Step 4",
123 title: "Feature 4",
124 description: "Feature 4 description",
125 },
126] as const
127
128/**
129 * Animation presets for reusable motion configurations.
130 * Each preset defines the initial, animate, and exit states,
131 * along with spring physics parameters for smooth transitions.
132 */
133const ANIMATION_PRESETS = {
134 fadeInScale: {
135 initial: { opacity: 0, scale: 0.95 },
136 animate: { opacity: 1, scale: 1 },
137 exit: { opacity: 0, scale: 0.95 },
138 transition: {
139 type: "spring",
140 stiffness: 300, // Higher value = more rigid spring
141 damping: 25, // Higher value = less oscillation
142 mass: 0.5, // Lower value = faster movement
143 },
144 },
145 slideInRight: {
146 initial: { opacity: 0, x: 20 },
147 animate: { opacity: 1, x: 0 },
148 exit: { opacity: 0, x: -20 },
149 transition: {
150 type: "spring",
151 stiffness: 300,
152 damping: 25,
153 mass: 0.5,
154 },
155 },
156 slideInLeft: {
157 initial: { opacity: 0, x: -20 },
158 animate: { opacity: 1, x: 0 },
159 exit: { opacity: 0, x: 20 },
160 transition: {
161 type: "spring",
162 stiffness: 300,
163 damping: 25,
164 mass: 0.5,
165 },
166 },
167} as const
168
169type AnimationPreset = keyof typeof ANIMATION_PRESETS
170
171interface AnimatedStepImageProps extends StepImageProps {
172 preset?: AnimationPreset
173 delay?: number
174 onAnimationComplete?: () => void
175}
176
177/**
178 * Custom hook for managing cyclic transitions with auto-play functionality.
179 * Handles both automatic cycling and manual transitions between steps.
180 */
181function useNumberCycler(
182 totalSteps: number = TOTAL_STEPS,
183 interval: number = 3000
184) {
185 const [currentNumber, setCurrentNumber] = useState(0)
186 const [isManualInteraction, setIsManualInteraction] = useState(false)
187 const timerRef = useRef<NodeJS.Timeout | null>(null)
188
189 // Setup timer function
190 const setupTimer = useCallback(() => {
191 console.log("Setting up timer")
192 // Clear any existing timer
193 if (timerRef.current) {
194 clearTimeout(timerRef.current)
195 }
196
197 timerRef.current = setTimeout(() => {
198 console.log("Timer triggered, advancing to next step")
199 setCurrentNumber((prev) => (prev + 1) % totalSteps)
200 setIsManualInteraction(false)
201 // Recursively setup next timer
202 setupTimer()
203 }, interval)
204 }, [interval, totalSteps])
205
206 // Handle manual increment
207 const increment = useCallback(() => {
208 console.log("Manual increment triggered")
209 setIsManualInteraction(true)
210 setCurrentNumber((prev) => (prev + 1) % totalSteps)
211
212 // Reset timer on manual interaction
213 setupTimer()
214 }, [totalSteps, setupTimer])
215
216 // Initial timer setup and cleanup
217 useEffect(() => {
218 console.log("Initial timer setup")
219 setupTimer()
220
221 return () => {
222 console.log("Cleaning up timer")
223 if (timerRef.current) {
224 clearTimeout(timerRef.current)
225 }
226 }
227 }, [setupTimer])
228
229 // Debug logging
230 useEffect(() => {
231 console.log("Current state:", {
232 currentNumber,
233 isManualInteraction,
234 hasTimer: !!timerRef.current,
235 })
236 }, [currentNumber, isManualInteraction])
237
238 return {
239 currentNumber,
240 increment,
241 isManualInteraction,
242 }
243}
244
245function useIsMobile() {
246 const [isMobile, setIsMobile] = useState(false)
247
248 useEffect(() => {
249 const userAgent = navigator.userAgent
250 const isSmall = window.matchMedia("(max-width: 768px)").matches
251 const isMobile = Boolean(
252 /Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i.exec(
253 userAgent
254 )
255 )
256
257 const isDev = process.env.NODE_ENV !== "production"
258 if (isDev) setIsMobile(isSmall || isMobile)
259
260 setIsMobile(isSmall && isMobile)
261 }, [])
262
263 return isMobile
264}
265
266// Components
267function IconCheck({ className, ...props }: React.ComponentProps<"svg">) {
268 return (
269 <svg
270 xmlns="http://www.w3.org/2000/svg"
271 viewBox="0 0 256 256"
272 fill="currentColor"
273 className={cn("h-4 w-4", className)}
274 {...props}
275 >
276 <path d="m229.66 77.66-128 128a8 8 0 0 1-11.32 0l-56-56a8 8 0 0 1 11.32-11.32L96 188.69 218.34 66.34a8 8 0 0 1 11.32 11.32Z" />
277 </svg>
278 )
279}
280
281const stepVariants: Variants = {
282 inactive: {
283 scale: 0.8,
284 opacity: 0.5,
285 },
286 active: {
287 scale: 1,
288 opacity: 1,
289 },
290}
291
292const StepImage = forwardRef<
293 HTMLImageElement,
294 StepImageProps & { [key: string]: any }
295>(
296 (
297 { src, alt, className, style, width = 1200, height = 630, ...props },
298 ref
299 ) => {
300 return (
301 <Image
302 ref={ref}
303 alt={alt}
304 className={className}
305 src={src}
306 width={width}
307 height={height}
308 style={{
309 position: "absolute",
310 userSelect: "none",
311 maxWidth: "unset",
312 ...style,
313 }}
314 {...props}
315 />
316 )
317 }
318)
319StepImage.displayName = "StepImage"
320
321const MotionStepImage = motion(StepImage)
322
323/**
324 * Wrapper component for StepImage that applies animation presets.
325 * Simplifies the application of complex animations through preset configurations.
326 */
327const AnimatedStepImage = ({
328 preset = "fadeInScale",
329 delay = 0,
330 onAnimationComplete,
331 ...props
332}: AnimatedStepImageProps) => {
333 const presetConfig = ANIMATION_PRESETS[preset]
334 return (
335 <MotionStepImage
336 {...props}
337 {...presetConfig}
338 transition={{
339 ...presetConfig.transition,
340 delay,
341 }}
342 onAnimationComplete={onAnimationComplete}
343 />
344 )
345}
346
347/**
348 * Main card component that handles mouse tracking for gradient effect.
349 * Uses motion values to create an interactive gradient that follows the cursor.
350 */
351function FeatureCard({
352 bgClass,
353 children,
354 step,
355}: CardProps & {
356 children: React.ReactNode
357 step: number
358}) {
359 const [mounted, setMounted] = useState(false)
360 const mouseX = useMotionValue(0)
361 const mouseY = useMotionValue(0)
362 const isMobile = useIsMobile()
363
364 function handleMouseMove({ currentTarget, clientX, clientY }: MouseEvent) {
365 if (isMobile) return
366 const { left, top } = currentTarget.getBoundingClientRect()
367 mouseX.set(clientX - left)
368 mouseY.set(clientY - top)
369 }
370
371 useEffect(() => {
372 setMounted(true)
373 }, [])
374
375 return (
376 <motion.div
377 className="animated-cards relative w-full rounded-[16px]"
378 onMouseMove={handleMouseMove}
379 style={
380 {
381 "--x": useMotionTemplate`${mouseX}px`,
382 "--y": useMotionTemplate`${mouseY}px`,
383 } as WrapperStyle
384 }
385 >
386 <div
387 className={clsx(
388 "group relative w-full overflow-hidden rounded-3xl border border-black/10 bg-gradient-to-b from-neutral-900/90 to-stone-800 transition duration-300 dark:from-neutral-950/90 dark:to-neutral-800/90",
389 "md:hover:border-transparent",
390 bgClass
391 )}
392 >
393 <div className="m-10 min-h-[450px] w-full">
394 <AnimatePresence mode="wait">
395 <motion.div
396 key={step}
397 className="flex w-4/6 flex-col gap-3"
398 initial={{ opacity: 0, y: 20 }}
399 animate={{ opacity: 1, y: 0 }}
400 exit={{ opacity: 0, y: -20 }}
401 transition={{
402 duration: 0.3,
403 ease: [0.23, 1, 0.32, 1],
404 }}
405 >
406 <motion.h2
407 className="text-xl font-bold tracking-tight text-white md:text-2xl"
408 initial={{ opacity: 0, x: -20 }}
409 animate={{ opacity: 1, x: 0 }}
410 transition={{
411 delay: 0.1,
412 duration: 0.3,
413 ease: [0.23, 1, 0.32, 1],
414 }}
415 >
416 {steps[step].title}
417 </motion.h2>
418 <motion.div
419 initial={{ opacity: 0, x: -20 }}
420 animate={{ opacity: 1, x: 0 }}
421 transition={{
422 delay: 0.2,
423 duration: 0.3,
424 ease: [0.23, 1, 0.32, 1],
425 }}
426 >
427 <p className="text-sm leading-5 text-neutral-300 sm:text-base sm:leading-5 dark:text-zinc-400">
428 <Balancer>{steps[step].description}</Balancer>
429 </p>
430 </motion.div>
431 </motion.div>
432 </AnimatePresence>
433 {mounted ? children : null}
434 </div>
435 </div>
436 </motion.div>
437 )
438}
439
440/**
441 * Progress indicator component that shows current step and completion status.
442 * Handles complex state transitions and animations for step indicators.
443 */
444function Steps({
445 steps,
446 current,
447 onChange,
448}: {
449 steps: readonly Step[]
450 current: number
451 onChange: (index: number) => void
452}) {
453 return (
454 <nav aria-label="Progress" className="flex justify-center px-4">
455 <ol
456 className="flex w-full flex-wrap items-start justify-start gap-2 sm:justify-center md:w-10/12 md:divide-y-0"
457 role="list"
458 >
459 {steps.map((step, stepIdx) => {
460 // Calculate step states for styling and animations
461 const isCompleted = current > stepIdx
462 const isCurrent = current === stepIdx
463 const isFuture = !isCompleted && !isCurrent
464
465 return (
466 <motion.li
467 key={`${step.name}-${stepIdx}`}
468 initial="inactive"
469 animate={isCurrent ? "active" : "inactive"}
470 variants={stepVariants}
471 transition={{ duration: 0.3 }}
472 className={cn(
473 "relative z-50 rounded-full px-3 py-1 transition-all duration-300 ease-in-out md:flex",
474 isCompleted ? "bg-neutral-500/20" : "bg-neutral-500/10"
475 )}
476 >
477 <div
478 className={cn(
479 "group flex w-full cursor-pointer items-center focus:outline-none focus-visible:ring-2",
480 (isFuture || isCurrent) && "pointer-events-none"
481 )}
482 onClick={() => onChange(stepIdx)}
483 >
484 <span className="flex items-center gap-2 text-sm font-medium">
485 <motion.span
486 initial={false}
487 animate={{
488 scale: isCurrent ? 1.2 : 1,
489 }}
490 className={cn(
491 "flex h-4 w-4 shrink-0 items-center justify-center rounded-full duration-300",
492 isCompleted &&
493 "bg-brand-400 text-white dark:bg-brand-400",
494 isCurrent &&
495 "bg-brand-300/80 text-neutral-400 dark:bg-neutral-500/50",
496 isFuture && "bg-brand-300/10 dark:bg-neutral-500/20"
497 )}
498 >
499 {isCompleted ? (
500 <motion.div
501 initial={{ scale: 0 }}
502 animate={{ scale: 1 }}
503 transition={{
504 type: "spring",
505 stiffness: 300,
506 damping: 20,
507 }}
508 >
509 <IconCheck className="h-3 w-3 stroke-white stroke-[3] text-white dark:stroke-black" />
510 </motion.div>
511 ) : (
512 <span
513 className={cn(
514 "text-xs",
515 !isCurrent && "text-[#C6EA7E]"
516 )}
517 >
518 {stepIdx + 1}
519 </span>
520 )}
521 </motion.span>
522 <motion.span
523 initial={{ opacity: 0, x: -10 }}
524 animate={{ opacity: 1, x: 0 }}
525 className={clsx(
526 "text-sm font-medium duration-300",
527 isCompleted && "text-muted-foreground",
528 isCurrent && "text-lime-300 dark:text-lime-500",
529 isFuture && "text-neutral-500"
530 )}
531 >
532 {step.name}
533 </motion.span>
534 </span>
535 </div>
536 </motion.li>
537 )
538 })}
539 </ol>
540 </nav>
541 )
542}
543
544const defaultClasses = {
545 step1img1:
546 "pointer-events-none w-[50%] border border-border-100/10 transition-all duration-500 dark:border-border-700/50 rounded-2xl",
547 step1img2:
548 "pointer-events-none w-[60%] border border-border-100/10 dark:border-border-700/50 transition-all duration-500 overflow-hidden rounded-2xl",
549 step2img1:
550 "pointer-events-none w-[50%] border border-border-100/10 transition-all duration-500 dark:border-border-700 rounded-2xl overflow-hidden",
551 step2img2:
552 "pointer-events-none w-[40%] border border-border-100/10 dark:border-border-700 transition-all duration-500 rounded-2xl overflow-hidden",
553 step3img:
554 "pointer-events-none w-[90%] border border-border-100/10 dark:border-border-700 rounded-2xl transition-all duration-500 overflow-hidden",
555 step4img:
556 "pointer-events-none w-[90%] border border-border-100/10 dark:border-border-700 rounded-2xl transition-all duration-500 overflow-hidden",
557} as const
558
559/**
560 * Main component that orchestrates the multi-step animation sequence.
561 * Manages state transitions, handles animation timing, and prevents
562 * animation conflicts through the isAnimating flag.
563 */
564export function FeatureCarousel({
565 image,
566 step1img1Class = defaultClasses.step1img1,
567 step1img2Class = defaultClasses.step1img2,
568 step2img1Class = defaultClasses.step2img1,
569 step2img2Class = defaultClasses.step2img2,
570 step3imgClass = defaultClasses.step3img,
571 step4imgClass = defaultClasses.step4img,
572 ...props
573}: FeatureCarouselProps) {
574 const { currentNumber: step, increment } = useNumberCycler()
575 const [isAnimating, setIsAnimating] = useState(false)
576
577 const handleIncrement = () => {
578 if (isAnimating) return
579 setIsAnimating(true)
580 increment()
581 }
582
583 const handleAnimationComplete = () => {
584 setIsAnimating(false)
585 }
586
587 const renderStepContent = () => {
588 const content = () => {
589 switch (step) {
590 case 0:
591 /**
592 * Layout: Two images side by side
593 * - Left image (step1img1): 50% width, positioned left
594 * - Right image (step1img2): 60% width, positioned right
595 * Animation:
596 * - Left image slides in from left
597 * - Right image slides in from right with 0.1s delay
598 * - Both use spring animation for smooth motion
599 */
600 return (
601 <motion.div
602 className="relative w-full h-full"
603 onAnimationComplete={handleAnimationComplete}
604 >
605 <AnimatedStepImage
606 alt={image.alt}
607 className={clsx(step1img1Class)}
608 src={image.step1light1}
609 preset="slideInLeft"
610 />
611 <AnimatedStepImage
612 alt={image.alt}
613 className={clsx(step1img2Class)}
614 src={image.step1light2}
615 preset="slideInRight"
616 delay={0.1}
617 />
618 </motion.div>
619 )
620 case 1:
621 /**
622 * Layout: Two images with overlapping composition
623 * - First image (step2img1): 50% width, positioned left
624 * - Second image (step2img2): 40% width, overlaps first image
625 * Animation:
626 * - Both images fade in and scale up from 95%
627 * - Second image has 0.1s delay for staggered effect
628 * - Uses spring physics for natural motion
629 */
630 return (
631 <motion.div
632 className="relative w-full h-full"
633 onAnimationComplete={handleAnimationComplete}
634 >
635 <AnimatedStepImage
636 alt={image.alt}
637 className={clsx(step2img1Class, "rounded-2xl")}
638 src={image.step2light1}
639 preset="fadeInScale"
640 />
641 <AnimatedStepImage
642 alt={image.alt}
643 className={clsx(step2img2Class, "rounded-2xl")}
644 src={image.step2light2}
645 preset="fadeInScale"
646 delay={0.1}
647 />
648 </motion.div>
649 )
650 case 2:
651 /**
652 * Layout: Single centered image
653 * - Full width image (step3img): 90% width, centered
654 * Animation:
655 * - Fades in and scales up from 95%
656 * - Uses spring animation for smooth scaling
657 * - Triggers animation complete callback
658 */
659 return (
660 <AnimatedStepImage
661 alt={image.alt}
662 className={clsx(step3imgClass, "rounded-2xl")}
663 src={image.step3light}
664 preset="fadeInScale"
665 onAnimationComplete={handleAnimationComplete}
666 />
667 )
668 case 3:
669 /**
670 * Layout: Final showcase layout
671 * - Container: Centered, 60% width on desktop
672 * - Image (cult): 90% width, positioned slightly up
673 * Animation:
674 * - Container fades in and scales up
675 * - Image follows with 0.1s delay
676 * - Both use spring physics for natural motion
677 */
678 return (
679 <motion.div
680 className={clsx(
681 "absolute left-2/4 top-1/3 flex w-[100%] -translate-x-1/2 -translate-y-[33%] flex-col gap-12 text-center text-2xl font-bold md:w-[60%]"
682 )}
683 {...ANIMATION_PRESETS.fadeInScale}
684 onAnimationComplete={handleAnimationComplete}
685 >
686 <AnimatedStepImage
687 alt={image.alt}
688 className="pointer-events-none top-[50%] w-[90%] overflow-hidden rounded-2xl border border-neutral-100/10 md:left-[35px] md:top-[30%] md:w-full dark:border-zinc-700"
689 src="/cults.png"
690 preset="fadeInScale"
691 delay={0.1}
692 />
693 </motion.div>
694 )
695 default:
696 return null
697 }
698 }
699
700 return (
701 <AnimatePresence mode="wait">
702 <motion.div
703 key={step}
704 {...ANIMATION_PRESETS.fadeInScale}
705 className="w-full h-full absolute"
706 >
707 {content()}
708 </motion.div>
709 </AnimatePresence>
710 )
711 }
712
713 return (
714 <FeatureCard {...props} step={step}>
715 {renderStepContent()}
716 <motion.div
717 initial={{ opacity: 0 }}
718 animate={{ opacity: 1 }}
719 transition={{ delay: 0.2 }}
720 className="absolute left-[12rem] top-5 z-50 h-full w-full cursor-pointer md:left-0"
721 >
722 <Steps current={step} onChange={() => {}} steps={steps} />
723 </motion.div>
724 <motion.div
725 className="absolute right-0 top-0 z-50 h-full w-full cursor-pointer md:left-0"
726 onClick={handleIncrement}
727 whileHover={{ scale: 1.02 }}
728 whileTap={{ scale: 0.98 }}
729 />
730 </FeatureCard>
731 )
732}
733
734export default FeatureCarousel
735