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 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 [canScrollLeft, setCanScrollLeft] = useState(false);
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
const updateTranslation = useCallback(() => {
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 maxDistance = lastCenter - firstCenter;
targetXRef.current = progress * maxDistance;
startLerpLoop();
const translateX = progress * maxDistance;
track.style.transform = `translate3d(-${translateX.toFixed(2)}px, 0, 0)`;
// Sync active card index and button states
const active = Math.min(
@@ -114,21 +80,66 @@ export function CinematicHorizontalScrollSection({
setCanScrollLeft(progress > 0.02);
setCanScrollRight(progress < 0.98);
}, [startLerpLoop]);
}, []);
useEffect(() => {
const container = containerRef.current;
const handleScroll = () => {
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();
window.addEventListener('scroll', handleScroll, { passive: true });
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 () => {
window.removeEventListener('scroll', 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]);
@@ -226,7 +237,7 @@ export function CinematicHorizontalScrollSection({
style={{
paddingLeft: '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) => (
@@ -47,6 +47,11 @@ interface 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 (
<SectionEntrance id={props.section.id}>
<SectionContent {...props} />
+65 -35
View File
@@ -18,7 +18,8 @@ export function CardBeforeAfterSlider({
aspectRatio = 'aspect-[16/10]',
}: CardBeforeAfterSliderProps) {
const containerRef = useRef<HTMLDivElement>(null);
const isHoveredRef = useRef(false);
const isDraggingRef = useRef(false);
const isInteractedRef = useRef(false);
const animIdRef = useRef<number>(0);
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(() => {
let startTime: number | null = null;
let isVisible = true;
const animate = (time: number) => {
if (!isVisible) return;
if (!isHoveredRef.current) {
if (!isInteractedRef.current) {
if (startTime === null) startTime = time;
const elapsed = time - startTime;
// Smooth sine wave between 28% and 72% over a 3.4-second cycle
const currentSplit = 50 + 22 * Math.sin((elapsed / 3400) * 2 * Math.PI);
setSplit(currentSplit);
} else {
startTime = null;
animIdRef.current = requestAnimationFrame(animate);
}
animIdRef.current = requestAnimationFrame(animate);
};
const observer = new IntersectionObserver(
([entry]) => {
isVisible = entry.isIntersecting;
if (isVisible) {
if (isVisible && !isInteractedRef.current) {
startTime = null;
animIdRef.current = requestAnimationFrame(animate);
} else {
@@ -65,7 +75,9 @@ export function CardBeforeAfterSlider({
observer.observe(containerRef.current);
}
animIdRef.current = requestAnimationFrame(animate);
if (!isInteractedRef.current) {
animIdRef.current = requestAnimationFrame(animate);
}
return () => {
cancelAnimationFrame(animIdRef.current);
@@ -73,45 +85,63 @@ export function CardBeforeAfterSlider({
};
}, [setSplit]);
// Mouse move scrubber
const handleMouseMove = useCallback(
// Click & Drag handlers: DOES NOT WORK ON HOVER - REQUIRES CURSOR CLICK!
const handleMouseDown = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
isHoveredRef.current = true;
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100;
setSplit(percent);
e.preventDefault();
isDraggingRef.current = true;
isInteractedRef.current = true;
cancelAnimationFrame(animIdRef.current);
updateSplitFromClientX(e.clientX);
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(
(e: React.TouchEvent<HTMLDivElement>) => {
isHoveredRef.current = true;
if (!containerRef.current || !e.touches[0]) return;
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);
if (!isDraggingRef.current || !e.touches[0]) return;
updateSplitFromClientX(e.touches[0].clientX);
},
[setSplit]
[updateSplitFromClientX]
);
const handlePointerLeave = useCallback(() => {
isHoveredRef.current = false;
const handleTouchEnd = useCallback(() => {
isDraggingRef.current = false;
}, []);
return (
<div
ref={containerRef}
onMouseEnter={() => { isHoveredRef.current = true; }}
onMouseLeave={handlePointerLeave}
onMouseMove={handleMouseMove}
onTouchStart={() => { isHoveredRef.current = true; }}
onMouseDown={handleMouseDown}
onTouchStart={handleTouchStart}
onTouchMove={handleTouchMove}
onTouchEnd={handlePointerLeave}
onTouchEnd={handleTouchEnd}
role="slider"
aria-label="Before and after transformation slider"
aria-valuenow={50}
@@ -168,9 +198,9 @@ export function CardBeforeAfterSlider({
{afterLabel}
</div>
{/* Scrub 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">
Scrub to compare
{/* Click / Drag Hint */}
<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">
Click &amp; drag to compare
</div>
</div>
);