Spaces:
Sleeping
Sleeping
File size: 10,048 Bytes
a7b8df9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
import { useLayoutEffect, useRef, useCallback } from 'react';
import Lenis from 'lenis';
import './ScrollStack.css';
export const ScrollStackItem = ({ children, itemClassName = '' }) => (
<div className={`scroll-stack-card ${itemClassName}`.trim()}>{children}</div>
);
const ScrollStack = ({
children,
className = '',
itemDistance = 100,
itemScale = 0.03,
itemStackDistance = 30,
stackPosition = '20%',
scaleEndPosition = '10%',
baseScale = 0.85,
scaleDuration = 0.5,
rotationAmount = 0,
blurAmount = 0,
useWindowScroll = false,
onStackComplete
}) => {
const scrollerRef = useRef(null);
const stackCompletedRef = useRef(false);
const animationFrameRef = useRef(null);
const lenisRef = useRef(null);
const cardsRef = useRef([]);
const lastTransformsRef = useRef(new Map());
const isUpdatingRef = useRef(false);
const calculateProgress = useCallback((scrollTop, start, end) => {
if (scrollTop < start) return 0;
if (scrollTop > end) return 1;
return (scrollTop - start) / (end - start);
}, []);
const parsePercentage = useCallback((value, containerHeight) => {
if (typeof value === 'string' && value.includes('%')) {
return (parseFloat(value) / 100) * containerHeight;
}
return parseFloat(value);
}, []);
const getScrollData = useCallback(() => {
if (useWindowScroll) {
return {
scrollTop: window.scrollY,
containerHeight: window.innerHeight,
scrollContainer: document.documentElement
};
} else {
const scroller = scrollerRef.current;
return {
scrollTop: scroller.scrollTop,
containerHeight: scroller.clientHeight,
scrollContainer: scroller
};
}
}, [useWindowScroll]);
const getElementOffset = useCallback(
element => {
if (useWindowScroll) {
const rect = element.getBoundingClientRect();
return rect.top + window.scrollY;
} else {
return element.offsetTop;
}
},
[useWindowScroll]
);
const updateCardTransforms = useCallback(() => {
if (!cardsRef.current.length || isUpdatingRef.current) return;
isUpdatingRef.current = true;
const { scrollTop, containerHeight, scrollContainer } = getScrollData();
const stackPositionPx = parsePercentage(stackPosition, containerHeight);
const scaleEndPositionPx = parsePercentage(scaleEndPosition, containerHeight);
const endElement = useWindowScroll
? document.querySelector('.scroll-stack-end')
: scrollerRef.current?.querySelector('.scroll-stack-end');
const endElementTop = endElement ? getElementOffset(endElement) : 0;
cardsRef.current.forEach((card, i) => {
if (!card) return;
const cardTop = getElementOffset(card);
const triggerStart = cardTop - stackPositionPx - itemStackDistance * i;
const triggerEnd = cardTop - scaleEndPositionPx;
const pinStart = cardTop - stackPositionPx - itemStackDistance * i;
const pinEnd = endElementTop - containerHeight / 2;
const scaleProgress = calculateProgress(scrollTop, triggerStart, triggerEnd);
const targetScale = baseScale + i * itemScale;
const scale = 1 - scaleProgress * (1 - targetScale);
const rotation = rotationAmount ? i * rotationAmount * scaleProgress : 0;
let blur = 0;
if (blurAmount) {
let topCardIndex = 0;
for (let j = 0; j < cardsRef.current.length; j++) {
const jCardTop = getElementOffset(cardsRef.current[j]);
const jTriggerStart = jCardTop - stackPositionPx - itemStackDistance * j;
if (scrollTop >= jTriggerStart) {
topCardIndex = j;
}
}
if (i < topCardIndex) {
const depthInStack = topCardIndex - i;
blur = Math.max(0, depthInStack * blurAmount);
}
}
let translateY = 0;
const isPinned = scrollTop >= pinStart && scrollTop <= pinEnd;
if (isPinned) {
translateY = scrollTop - cardTop + stackPositionPx + itemStackDistance * i;
} else if (scrollTop > pinEnd) {
translateY = pinEnd - cardTop + stackPositionPx + itemStackDistance * i;
}
const newTransform = {
translateY: Math.round(translateY * 100) / 100,
scale: Math.round(scale * 1000) / 1000,
rotation: Math.round(rotation * 100) / 100,
blur: Math.round(blur * 100) / 100
};
const lastTransform = lastTransformsRef.current.get(i);
const hasChanged =
!lastTransform ||
Math.abs(lastTransform.translateY - newTransform.translateY) > 0.1 ||
Math.abs(lastTransform.scale - newTransform.scale) > 0.001 ||
Math.abs(lastTransform.rotation - newTransform.rotation) > 0.1 ||
Math.abs(lastTransform.blur - newTransform.blur) > 0.1;
if (hasChanged) {
const transform = `translate3d(0, ${newTransform.translateY}px, 0) scale(${newTransform.scale}) rotate(${newTransform.rotation}deg)`;
const filter = newTransform.blur > 0 ? `blur(${newTransform.blur}px)` : '';
// Apply transforms directly for better performance
card.style.transform = transform;
card.style.filter = filter;
card.style.willChange = 'transform';
lastTransformsRef.current.set(i, newTransform);
}
if (i === cardsRef.current.length - 1) {
const isInView = scrollTop >= pinStart && scrollTop <= pinEnd;
if (isInView && !stackCompletedRef.current) {
stackCompletedRef.current = true;
onStackComplete?.();
} else if (!isInView && stackCompletedRef.current) {
stackCompletedRef.current = false;
}
}
});
isUpdatingRef.current = false;
}, [
itemScale,
itemStackDistance,
stackPosition,
scaleEndPosition,
baseScale,
rotationAmount,
blurAmount,
useWindowScroll,
onStackComplete,
calculateProgress,
parsePercentage,
getScrollData,
getElementOffset
]);
const handleScroll = useCallback(() => {
updateCardTransforms();
}, [updateCardTransforms]);
const setupLenis = useCallback(() => {
if (useWindowScroll) {
const lenis = new Lenis({
duration: 0.8,
easing: t => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
smoothWheel: true,
touchMultiplier: 1.5,
infinite: false,
wheelMultiplier: 0.8,
lerp: 0.05,
syncTouch: true,
syncTouchLerp: 0.05
});
lenis.on('scroll', handleScroll);
const raf = time => {
lenis.raf(time);
animationFrameRef.current = requestAnimationFrame(raf);
};
animationFrameRef.current = requestAnimationFrame(raf);
lenisRef.current = lenis;
return lenis;
} else {
const scroller = scrollerRef.current;
if (!scroller) return;
const lenis = new Lenis({
wrapper: scroller,
content: scroller.querySelector('.scroll-stack-inner'),
duration: 0.8,
easing: t => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
smoothWheel: true,
touchMultiplier: 1.5,
infinite: false,
gestureOrientationHandler: true,
normalizeWheel: true,
wheelMultiplier: 0.8,
touchInertiaMultiplier: 25,
lerp: 0.05,
syncTouch: true,
syncTouchLerp: 0.05,
touchInertia: 0.4
});
lenis.on('scroll', handleScroll);
const raf = time => {
lenis.raf(time);
animationFrameRef.current = requestAnimationFrame(raf);
};
animationFrameRef.current = requestAnimationFrame(raf);
lenisRef.current = lenis;
return lenis;
}
}, [handleScroll, useWindowScroll]);
useLayoutEffect(() => {
const scroller = scrollerRef.current;
if (!scroller) return;
const cards = Array.from(
useWindowScroll
? document.querySelectorAll('.scroll-stack-card')
: scroller.querySelectorAll('.scroll-stack-card')
);
cardsRef.current = cards;
const transformsCache = lastTransformsRef.current;
cards.forEach((card, i) => {
if (i < cards.length - 1) {
card.style.marginBottom = `${itemDistance}px`;
}
card.style.willChange = 'transform, filter, opacity';
card.style.transformOrigin = 'top center';
card.style.backfaceVisibility = 'hidden';
card.style.transform = 'translateZ(0)';
card.style.webkitTransform = 'translateZ(0)';
card.style.perspective = '1000px';
card.style.webkitPerspective = '1000px';
card.style.contain = 'layout style paint';
card.style.isolation = 'isolate';
card.style.webkitFontSmoothing = 'antialiased';
card.style.mozOsxFontSmoothing = 'grayscale';
});
setupLenis();
updateCardTransforms();
return () => {
if (animationFrameRef.current) {
cancelAnimationFrame(animationFrameRef.current);
}
if (lenisRef.current) {
lenisRef.current.destroy();
}
stackCompletedRef.current = false;
cardsRef.current = [];
transformsCache.clear();
isUpdatingRef.current = false;
};
}, [
itemDistance,
itemScale,
itemStackDistance,
stackPosition,
scaleEndPosition,
baseScale,
scaleDuration,
rotationAmount,
blurAmount,
useWindowScroll,
onStackComplete,
setupLenis,
updateCardTransforms
]);
return (
<div className={`scroll-stack-scroller ${className}`.trim()} ref={scrollerRef}>
<div className="scroll-stack-inner">
{children}
{/* Spacer so the last pin can release cleanly */}
<div className="scroll-stack-end" />
</div>
</div>
);
};
export default ScrollStack;
|