fix: require click for card before-after slider and restore horizontal showcase scroll

This commit is contained in:
2026-11-04 12:17:22 +05:30
parent 5999b05c16
commit ad9a3ab000
3 changed files with 120 additions and 74 deletions
@@ -42,44 +42,10 @@ export function CinematicHorizontalScrollSection({
const containerRef = useRef<HTMLElement>(null); const containerRef = useRef<HTMLElement>(null);
const trackRef = useRef<HTMLDivElement>(null); const trackRef = useRef<HTMLDivElement>(null);
const targetXRef = useRef(0);
const currentXRef = useRef(0);
const animFrameRef = useRef<number>(0);
const isLoopRunningRef = useRef(false);
const [activeIndex, setActiveIndex] = useState(0); const [activeIndex, setActiveIndex] = useState(0);
const [canScrollLeft, setCanScrollLeft] = useState(false); const [canScrollLeft, setCanScrollLeft] = useState(false);
const [canScrollRight, setCanScrollRight] = useState(true); const [canScrollRight, setCanScrollRight] = useState(true);
// High-performance, responsive LERP animation loop to smooth wheel notches into liquid momentum
const startLerpLoop = useCallback(() => {
if (isLoopRunningRef.current) return;
isLoopRunningRef.current = true;
const loop = () => {
const track = trackRef.current;
if (!track) {
isLoopRunningRef.current = false;
return;
}
const diff = targetXRef.current - currentXRef.current;
if (Math.abs(diff) > 0.4) {
// Responsive 0.28 damping factor: silky smoothing with zero perceptible lag
currentXRef.current += diff * 0.28;
track.style.transform = `translate3d(-${currentXRef.current.toFixed(2)}px, 0, 0)`;
animFrameRef.current = requestAnimationFrame(loop);
} else {
currentXRef.current = targetXRef.current;
track.style.transform = `translate3d(-${currentXRef.current.toFixed(2)}px, 0, 0)`;
isLoopRunningRef.current = false;
}
};
animFrameRef.current = requestAnimationFrame(loop);
}, []);
// Compute fluid horizontal translation directly from vertical scroll progress // Compute fluid horizontal translation directly from vertical scroll progress
const updateTranslation = useCallback(() => { const updateTranslation = useCallback(() => {
const container = containerRef.current; const container = containerRef.current;
@@ -102,8 +68,8 @@ export function CinematicHorizontalScrollSection({
const lastCenter = cards[cards.length - 1].offsetLeft + cards[cards.length - 1].offsetWidth / 2; const lastCenter = cards[cards.length - 1].offsetLeft + cards[cards.length - 1].offsetWidth / 2;
const maxDistance = lastCenter - firstCenter; const maxDistance = lastCenter - firstCenter;
targetXRef.current = progress * maxDistance; const translateX = progress * maxDistance;
startLerpLoop(); track.style.transform = `translate3d(-${translateX.toFixed(2)}px, 0, 0)`;
// Sync active card index and button states // Sync active card index and button states
const active = Math.min( const active = Math.min(
@@ -114,21 +80,66 @@ export function CinematicHorizontalScrollSection({
setCanScrollLeft(progress > 0.02); setCanScrollLeft(progress > 0.02);
setCanScrollRight(progress < 0.98); setCanScrollRight(progress < 0.98);
}, [startLerpLoop]); }, []);
useEffect(() => { useEffect(() => {
const container = containerRef.current;
const handleScroll = () => { const handleScroll = () => {
updateTranslation(); updateTranslation();
}; };
// Support horizontal trackpad swipe (deltaX) on the showcase container
const handleWheel = (e: WheelEvent) => {
if (Math.abs(e.deltaX) > Math.abs(e.deltaY) && Math.abs(e.deltaX) > 6) {
e.preventDefault();
window.scrollBy({ top: e.deltaX * 1.2, behavior: 'auto' });
}
};
// Support touch horizontal swipe on mobile / tablet screens
let touchStartX = 0;
let touchStartY = 0;
let initialScrollY = 0;
const handleTouchStart = (e: TouchEvent) => {
if (!e.touches[0]) return;
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
initialScrollY = window.scrollY;
};
const handleTouchMove = (e: TouchEvent) => {
if (!e.touches[0]) return;
const deltaX = touchStartX - e.touches[0].clientX;
const deltaY = touchStartY - e.touches[0].clientY;
if (Math.abs(deltaX) > Math.abs(deltaY) && Math.abs(deltaX) > 10) {
window.scrollTo({
top: initialScrollY + deltaX * 1.3,
behavior: 'auto',
});
}
};
updateTranslation(); updateTranslation();
window.addEventListener('scroll', handleScroll, { passive: true }); window.addEventListener('scroll', handleScroll, { passive: true });
window.addEventListener('resize', handleScroll); window.addEventListener('resize', handleScroll);
if (container) {
container.addEventListener('wheel', handleWheel, { passive: false });
container.addEventListener('touchstart', handleTouchStart, { passive: true });
container.addEventListener('touchmove', handleTouchMove, { passive: true });
}
return () => { return () => {
window.removeEventListener('scroll', handleScroll); window.removeEventListener('scroll', handleScroll);
window.removeEventListener('resize', handleScroll); window.removeEventListener('resize', handleScroll);
if (animFrameRef.current) cancelAnimationFrame(animFrameRef.current); if (container) {
container.removeEventListener('wheel', handleWheel);
container.removeEventListener('touchstart', handleTouchStart);
container.removeEventListener('touchmove', handleTouchMove);
}
}; };
}, [updateTranslation, items.length]); }, [updateTranslation, items.length]);
@@ -226,7 +237,7 @@ export function CinematicHorizontalScrollSection({
style={{ style={{
paddingLeft: 'calc((100vw - min(780px, 88vw)) / 2)', paddingLeft: 'calc((100vw - min(780px, 88vw)) / 2)',
paddingRight: 'calc((100vw - min(780px, 88vw)) / 2)', paddingRight: 'calc((100vw - min(780px, 88vw)) / 2)',
transform: 'translate3d(0px, 0px, 0px)', transition: 'transform 0.08s cubic-bezier(0.25, 1, 0.5, 1)',
}} }}
> >
{items.map((item) => ( {items.map((item) => (
@@ -47,6 +47,11 @@ interface SectionRendererProps {
} }
export function SectionRenderer(props: SectionRendererProps) { export function SectionRenderer(props: SectionRendererProps) {
// Sticky scroll-linked sections must not have parent transforms or content-visibility containment
if (props.section.type === 'majorToolBanners' || props.section.type === 'cinematicHorizontal') {
return <SectionContent {...props} />;
}
return ( return (
<SectionEntrance id={props.section.id}> <SectionEntrance id={props.section.id}>
<SectionContent {...props} /> <SectionContent {...props} />
+65 -35
View File
@@ -18,7 +18,8 @@ export function CardBeforeAfterSlider({
aspectRatio = 'aspect-[16/10]', aspectRatio = 'aspect-[16/10]',
}: CardBeforeAfterSliderProps) { }: CardBeforeAfterSliderProps) {
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
const isHoveredRef = useRef(false); const isDraggingRef = useRef(false);
const isInteractedRef = useRef(false);
const animIdRef = useRef<number>(0); const animIdRef = useRef<number>(0);
const setSplit = useCallback((percent: number) => { const setSplit = useCallback((percent: number) => {
@@ -29,29 +30,38 @@ export function CardBeforeAfterSlider({
} }
}, []); }, []);
// Continuous auto-sliding animation when not actively hovered const updateSplitFromClientX = useCallback(
(clientX: number) => {
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
const x = Math.max(0, Math.min(clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100;
setSplit(percent);
},
[setSplit]
);
// Gentle auto-sliding animation until the user actively clicks
useEffect(() => { useEffect(() => {
let startTime: number | null = null; let startTime: number | null = null;
let isVisible = true; let isVisible = true;
const animate = (time: number) => { const animate = (time: number) => {
if (!isVisible) return; if (!isVisible) return;
if (!isHoveredRef.current) { if (!isInteractedRef.current) {
if (startTime === null) startTime = time; if (startTime === null) startTime = time;
const elapsed = time - startTime; const elapsed = time - startTime;
// Smooth sine wave between 28% and 72% over a 3.4-second cycle // Smooth sine wave between 28% and 72% over a 3.4-second cycle
const currentSplit = 50 + 22 * Math.sin((elapsed / 3400) * 2 * Math.PI); const currentSplit = 50 + 22 * Math.sin((elapsed / 3400) * 2 * Math.PI);
setSplit(currentSplit); setSplit(currentSplit);
} else { animIdRef.current = requestAnimationFrame(animate);
startTime = null;
} }
animIdRef.current = requestAnimationFrame(animate);
}; };
const observer = new IntersectionObserver( const observer = new IntersectionObserver(
([entry]) => { ([entry]) => {
isVisible = entry.isIntersecting; isVisible = entry.isIntersecting;
if (isVisible) { if (isVisible && !isInteractedRef.current) {
startTime = null; startTime = null;
animIdRef.current = requestAnimationFrame(animate); animIdRef.current = requestAnimationFrame(animate);
} else { } else {
@@ -65,7 +75,9 @@ export function CardBeforeAfterSlider({
observer.observe(containerRef.current); observer.observe(containerRef.current);
} }
animIdRef.current = requestAnimationFrame(animate); if (!isInteractedRef.current) {
animIdRef.current = requestAnimationFrame(animate);
}
return () => { return () => {
cancelAnimationFrame(animIdRef.current); cancelAnimationFrame(animIdRef.current);
@@ -73,45 +85,63 @@ export function CardBeforeAfterSlider({
}; };
}, [setSplit]); }, [setSplit]);
// Mouse move scrubber // Click & Drag handlers: DOES NOT WORK ON HOVER - REQUIRES CURSOR CLICK!
const handleMouseMove = useCallback( const handleMouseDown = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => { (e: React.MouseEvent<HTMLDivElement>) => {
isHoveredRef.current = true; e.preventDefault();
if (!containerRef.current) return; isDraggingRef.current = true;
const rect = containerRef.current.getBoundingClientRect(); isInteractedRef.current = true;
const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width)); cancelAnimationFrame(animIdRef.current);
const percent = (x / rect.width) * 100; updateSplitFromClientX(e.clientX);
setSplit(percent);
const handleGlobalMouseMove = (moveEvent: MouseEvent) => {
if (!isDraggingRef.current) return;
updateSplitFromClientX(moveEvent.clientX);
};
const handleGlobalMouseUp = () => {
isDraggingRef.current = false;
window.removeEventListener('mousemove', handleGlobalMouseMove);
window.removeEventListener('mouseup', handleGlobalMouseUp);
};
window.addEventListener('mousemove', handleGlobalMouseMove);
window.addEventListener('mouseup', handleGlobalMouseUp);
}, },
[setSplit] [updateSplitFromClientX]
);
// Touch handlers for mobile/tablets
const handleTouchStart = useCallback(
(e: React.TouchEvent<HTMLDivElement>) => {
if (!e.touches[0]) return;
isDraggingRef.current = true;
isInteractedRef.current = true;
cancelAnimationFrame(animIdRef.current);
updateSplitFromClientX(e.touches[0].clientX);
},
[updateSplitFromClientX]
); );
// Touch move scrubber
const handleTouchMove = useCallback( const handleTouchMove = useCallback(
(e: React.TouchEvent<HTMLDivElement>) => { (e: React.TouchEvent<HTMLDivElement>) => {
isHoveredRef.current = true; if (!isDraggingRef.current || !e.touches[0]) return;
if (!containerRef.current || !e.touches[0]) return; updateSplitFromClientX(e.touches[0].clientX);
const rect = containerRef.current.getBoundingClientRect();
const x = Math.max(0, Math.min(e.touches[0].clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100;
setSplit(percent);
}, },
[setSplit] [updateSplitFromClientX]
); );
const handlePointerLeave = useCallback(() => { const handleTouchEnd = useCallback(() => {
isHoveredRef.current = false; isDraggingRef.current = false;
}, []); }, []);
return ( return (
<div <div
ref={containerRef} ref={containerRef}
onMouseEnter={() => { isHoveredRef.current = true; }} onMouseDown={handleMouseDown}
onMouseLeave={handlePointerLeave} onTouchStart={handleTouchStart}
onMouseMove={handleMouseMove}
onTouchStart={() => { isHoveredRef.current = true; }}
onTouchMove={handleTouchMove} onTouchMove={handleTouchMove}
onTouchEnd={handlePointerLeave} onTouchEnd={handleTouchEnd}
role="slider" role="slider"
aria-label="Before and after transformation slider" aria-label="Before and after transformation slider"
aria-valuenow={50} aria-valuenow={50}
@@ -168,9 +198,9 @@ export function CardBeforeAfterSlider({
{afterLabel} {afterLabel}
</div> </div>
{/* Scrub Hint */} {/* Click / Drag Hint */}
<div className="pointer-events-none absolute top-2.5 right-2.5 z-20 rounded-full bg-white/90 px-2 py-0.5 text-[9px] font-semibold text-slate-700 shadow-sm backdrop-blur-xs opacity-0 group-hover/slider:opacity-100 transition-opacity"> <div className="pointer-events-none absolute top-2.5 right-2.5 z-20 rounded-full bg-white/90 px-2.5 py-0.5 text-[9px] font-semibold text-slate-700 shadow-sm backdrop-blur-xs opacity-0 group-hover/slider:opacity-100 transition-opacity">
Scrub to compare Click &amp; drag to compare
</div> </div>
</div> </div>
); );