|
| 1 | +import { |
| 2 | + motion, |
| 3 | + useInView, |
| 4 | + useScroll, |
| 5 | + useTransform, |
| 6 | + type UseInViewOptions, |
| 7 | + type MotionProps, |
| 8 | +} from "motion/react"; |
| 9 | +import { useRef } from "react"; |
| 10 | + |
| 11 | +type MarginType = UseInViewOptions["margin"]; |
| 12 | + |
| 13 | +interface ScrollFadeProps extends MotionProps { |
| 14 | + children: React.ReactNode; |
| 15 | + className?: string; |
| 16 | + duration?: number; |
| 17 | + delay?: number; |
| 18 | + offset?: number; |
| 19 | + direction?: "up" | "down" | "left" | "right"; |
| 20 | + inViewMargin?: MarginType; |
| 21 | + threshold?: number; |
| 22 | + parallaxIntensity?: number; // How much the section follows scroll (0 = no parallax, 1 = full parallax) |
| 23 | + enableParallax?: boolean; |
| 24 | + // Note: Fade effects removed for better performance - now only provides parallax movement |
| 25 | +} |
| 26 | + |
| 27 | +export function ScrollFade({ |
| 28 | + children, |
| 29 | + className, |
| 30 | + duration = 0.6, |
| 31 | + delay = 0, |
| 32 | + offset = 50, |
| 33 | + direction = "up", |
| 34 | + inViewMargin = "-100px", |
| 35 | + threshold = 0.1, |
| 36 | + parallaxIntensity = 0.3, |
| 37 | + enableParallax = false, |
| 38 | + ...props |
| 39 | +}: ScrollFadeProps) { |
| 40 | + const ref = useRef(null); |
| 41 | + const isInView = useInView(ref, { |
| 42 | + once: false, // Allow repeated animations |
| 43 | + margin: inViewMargin, |
| 44 | + amount: threshold, |
| 45 | + }); |
| 46 | + |
| 47 | + // Track scroll progress for parallax effect |
| 48 | + const { scrollYProgress } = useScroll({ |
| 49 | + target: ref, |
| 50 | + offset: ["start end", "end start"], // Better range for fade effects |
| 51 | + }); |
| 52 | + |
| 53 | + // Transform scroll progress to y movement |
| 54 | + const parallaxY = useTransform( |
| 55 | + scrollYProgress, |
| 56 | + [0, 0.5, 1], |
| 57 | + [offset, 0, -offset] |
| 58 | + ); |
| 59 | + |
| 60 | + // No opacity animation for better performance |
| 61 | + // const scrollOpacity = useTransform( |
| 62 | + // scrollYProgress, |
| 63 | + // fadeRange, // Use configurable fade range |
| 64 | + // [0, 1, 1, 1] // Fade in and stay visible (no fade out) |
| 65 | + // ); |
| 66 | + |
| 67 | + // Debug logging - commented out for production |
| 68 | + // useEffect(() => { |
| 69 | + // const unsubscribe = scrollYProgress.on("change", (value) => { |
| 70 | + // console.log( |
| 71 | + // "ScrollFade - scrollYProgress:", |
| 72 | + // value, |
| 73 | + // "opacity:", |
| 74 | + // scrollOpacity.get(), |
| 75 | + // "y:", |
| 76 | + // parallaxY.get() |
| 77 | + // ); |
| 78 | + // }); |
| 79 | + // return unsubscribe; |
| 80 | + // }, [scrollYProgress, scrollOpacity, parallaxY]); |
| 81 | + |
| 82 | + const getInitialValues = () => { |
| 83 | + switch (direction) { |
| 84 | + case "down": |
| 85 | + return { y: -offset, x: 0 }; |
| 86 | + case "left": |
| 87 | + return { x: offset, y: 0 }; |
| 88 | + case "right": |
| 89 | + return { x: -offset, y: 0 }; |
| 90 | + case "up": |
| 91 | + default: |
| 92 | + return { y: offset, x: 0 }; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + const initialValues = getInitialValues(); |
| 97 | + |
| 98 | + if (enableParallax) { |
| 99 | + // For parallax mode, use motion values directly (no fade for performance) |
| 100 | + return ( |
| 101 | + <motion.div |
| 102 | + ref={ref} |
| 103 | + style={{ |
| 104 | + y: parallaxY, // Only parallax movement, no opacity animation |
| 105 | + position: "relative", // Fix positioning for scroll calculation |
| 106 | + }} |
| 107 | + className={className} |
| 108 | + {...props} |
| 109 | + > |
| 110 | + {children} |
| 111 | + </motion.div> |
| 112 | + ); |
| 113 | + } else { |
| 114 | + // For non-parallax mode, use regular animations |
| 115 | + return ( |
| 116 | + <motion.div |
| 117 | + ref={ref} |
| 118 | + initial={{ |
| 119 | + opacity: 0, |
| 120 | + ...initialValues, |
| 121 | + }} |
| 122 | + animate={{ |
| 123 | + opacity: isInView ? 1 : 0, |
| 124 | + x: isInView ? 0 : initialValues.x, |
| 125 | + y: isInView ? 0 : initialValues.y, |
| 126 | + }} |
| 127 | + transition={{ |
| 128 | + duration, |
| 129 | + delay, |
| 130 | + ease: "easeOut", |
| 131 | + opacity: { duration: duration * 0.8 }, |
| 132 | + }} |
| 133 | + className={className} |
| 134 | + {...props} |
| 135 | + > |
| 136 | + {children} |
| 137 | + </motion.div> |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
0 commit comments