fix(home-slider): restore desktop mouse hover tracking, isolate slider from link navigation, and fix touch drag math

This commit is contained in:
2026-11-05 13:20:36 +05:30
parent ee518ff451
commit 7760722c76
3 changed files with 147 additions and 89 deletions
@@ -184,32 +184,64 @@ function ShiftableRow({ items }: { items: ToolMediaItem[] }) {
}}
className="w-full transition-[flex-grow] duration-1000 ease-[cubic-bezier(0.16,1,0.3,1)] will-change-[flex-grow] flex flex-col min-w-0"
>
{/* Window Container with strictly locked pixel height & landscape aspect (entire card is clickable) */}
<Link
href={`/tools/${item.slug}`}
className="group block relative w-full h-[220px] sm:h-[240px] lg:h-[260px] rounded-2xl sm:rounded-3xl overflow-hidden bg-slate-950 border border-slate-900/[0.08] shadow-cream-sm cursor-pointer"
>
{/* GPU-Composited Shadow Elevation Layer */}
<div
className="absolute -inset-px rounded-2xl sm:rounded-3xl shadow-cream-lg opacity-0 group-hover:opacity-100 transition-opacity duration-700 ease-[cubic-bezier(0.16,1,0.3,1)] pointer-events-none z-30"
aria-hidden="true"
/>
{/* Window Container with strictly locked pixel height & landscape aspect */}
{item.mediaType === 'comparison' ? (
<div className="group block relative w-full h-[220px] sm:h-[240px] lg:h-[260px] rounded-2xl sm:rounded-3xl overflow-hidden bg-slate-950 border border-slate-900/[0.08] shadow-cream-sm">
{/* GPU-Composited Shadow Elevation Layer */}
<div
className="absolute -inset-px rounded-2xl sm:rounded-3xl shadow-cream-lg opacity-0 group-hover:opacity-100 transition-opacity duration-700 ease-[cubic-bezier(0.16,1,0.3,1)] pointer-events-none z-30"
aria-hidden="true"
/>
{/* Media Renderer */}
<ToolWindowMedia item={item} isHovered={isHovered} />
{/* Media Renderer (Interactive comparison slider with desktop hover + mobile touch handle) */}
<ToolWindowMedia item={item} isHovered={isHovered} />
{/* Hover Media Type Pill */}
<div
className="absolute top-3 left-3 z-20 pointer-events-none opacity-0 group-hover:opacity-100 transition-opacity duration-500 ease-[cubic-bezier(0.16,1,0.3,1)]"
>
<span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-[10px] font-mono font-bold tracking-wider uppercase bg-slate-950/85 backdrop-blur-md text-white border border-white/20">
{item.mediaType === 'video' && <Video className="w-3 h-3 text-cyan-400" />}
{item.mediaType === 'comparison' && <Sliders className="w-3 h-3 text-amber-400" />}
{item.mediaType === 'photo' && <ImageIcon className="w-3 h-3 text-emerald-400" />}
<span>{item.mediaType}</span>
</span>
{/* Dedicated Tool Link Pill on top right so sliding never accidentally navigates */}
<Link
href={`/tools/${item.slug}`}
className="absolute top-3 right-3 z-30 inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-[10px] font-mono font-bold tracking-wider uppercase bg-slate-950/85 backdrop-blur-md text-white border border-white/20 hover:bg-blue-600 transition-colors shadow-sm cursor-pointer"
aria-label={`Open ${item.title}`}
>
<span>TRY TOOL</span>
<ArrowRight className="w-2.5 h-2.5" />
</Link>
{/* Hover Media Type Pill */}
<div
className="absolute top-3 left-3 z-20 pointer-events-none opacity-0 group-hover:opacity-100 transition-opacity duration-500 ease-[cubic-bezier(0.16,1,0.3,1)]"
>
<span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-[10px] font-mono font-bold tracking-wider uppercase bg-slate-950/85 backdrop-blur-md text-white border border-white/20">
<Sliders className="w-3 h-3 text-amber-400" />
<span>comparison</span>
</span>
</div>
</div>
</Link>
) : (
<Link
href={`/tools/${item.slug}`}
className="group block relative w-full h-[220px] sm:h-[240px] lg:h-[260px] rounded-2xl sm:rounded-3xl overflow-hidden bg-slate-950 border border-slate-900/[0.08] shadow-cream-sm cursor-pointer"
>
{/* GPU-Composited Shadow Elevation Layer (Opacity-only transition, zero paint thrash) */}
<div
className="absolute -inset-px rounded-2xl sm:rounded-3xl shadow-cream-lg opacity-0 group-hover:opacity-100 transition-opacity duration-700 ease-[cubic-bezier(0.16,1,0.3,1)] pointer-events-none z-30"
aria-hidden="true"
/>
{/* Media Renderer */}
<ToolWindowMedia item={item} isHovered={isHovered} />
{/* Hover Media Type Pill (Smooth GPU Opacity Transition) */}
<div
className="absolute top-3 left-3 z-20 pointer-events-none opacity-0 group-hover:opacity-100 transition-opacity duration-500 ease-[cubic-bezier(0.16,1,0.3,1)]"
>
<span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-[10px] font-mono font-bold tracking-wider uppercase bg-slate-950/85 backdrop-blur-md text-white border border-white/20">
{item.mediaType === 'video' && <Video className="w-3 h-3 text-cyan-400" />}
{item.mediaType === 'photo' && <ImageIcon className="w-3 h-3 text-emerald-400" />}
<span>{item.mediaType}</span>
</span>
</div>
</Link>
)}
{/* Little Title Below Window (Fixed height, no vertical jumping) */}
<Link
@@ -231,10 +263,10 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
const containerRef = useRef<HTMLDivElement>(null);
const dividerRef = useRef<HTMLDivElement>(null);
const afterImgRef = useRef<HTMLImageElement>(null);
const handleRef = useRef<HTMLDivElement>(null);
// High performance refs - completely eliminates per-frame React re-renders!
const isManualRef = useRef(false);
const isInteractedRef = useRef(false);
const isPointerDownRef = useRef(false);
const currentPercentRef = useRef(50);
const animFrameRef = useRef<number | null>(null);
@@ -251,8 +283,8 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
if (afterImgRef.current) {
afterImgRef.current.style.clipPath = `inset(0 ${100 - clamped}% 0 0)`;
}
if (containerRef.current) {
containerRef.current.setAttribute('aria-valuenow', `${Math.round(clamped)}`);
if (handleRef.current) {
handleRef.current.setAttribute('aria-valuenow', `${Math.round(clamped)}`);
}
}, []);
@@ -274,22 +306,18 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
if (item.mediaType !== 'comparison') return;
if (!isHovered) {
if (!isInteractedRef.current) {
if (animFrameRef.current) {
cancelAnimationFrame(animFrameRef.current);
animFrameRef.current = null;
}
setSliderDOM(50);
isManualRef.current = false;
if (animFrameRef.current) {
cancelAnimationFrame(animFrameRef.current);
animFrameRef.current = null;
}
setSliderDOM(50);
return;
}
// If user has already interacted, do not overwrite their position
if (isInteractedRef.current) return;
const startTime = performance.now();
const sweep = (now: number) => {
if (!isManualRef.current && !isInteractedRef.current) {
if (!isManualRef.current) {
const elapsed = (now - startTime) / 1000;
// Calm, breathing oscillation: 1.4 rad/s sine wave with 22% sweep amplitude
const pos = 50 + Math.sin(elapsed * 1.4) * 22;
@@ -308,24 +336,53 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
};
}, [isHovered, item.mediaType, setSliderDOM]);
// Pointer position tracking:
// Caches container bounding rect on pointer down to eliminate layout thrashing in pointermove.
// Updates slider DOM directly in requestAnimationFrame with zero React state calls.
const handlePointerEnter = (e: React.PointerEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
const rect = e.currentTarget.getBoundingClientRect();
cachedRectRef.current = { left: rect.left, width: rect.width };
const getContainerRect = () => {
if (!containerRef.current) return null;
const r = containerRef.current.getBoundingClientRect();
return { left: r.left, width: r.width };
};
const handlePointerDown = (e: React.PointerEvent<HTMLDivElement>) => {
// Desktop Mouse Movement: Tracks cursor across the entire card effortlessly without clicking
const handleContainerPointerEnter = (e: React.PointerEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
if (e.pointerType === 'mouse') {
cachedRectRef.current = getContainerRect();
}
};
const handleContainerPointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
if (e.pointerType === 'mouse') {
isManualRef.current = true;
const clientX = e.clientX;
if (pointerRafRef.current) cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = requestAnimationFrame(() => {
let rect = cachedRectRef.current || getContainerRect();
cachedRectRef.current = rect;
if (!rect || rect.width <= 0) return;
const x = Math.max(0, Math.min(clientX - rect.left, rect.width));
setSliderDOM((x / rect.width) * 100);
});
}
};
const handleContainerPointerLeave = () => {
if (isPointerDownRef.current) return;
isManualRef.current = false;
cachedRectRef.current = null;
if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = null;
}
};
// Dedicated Drag Handlers for Center Arrow Handle (Works on mobile touch & desktop click-drag)
const handleHandlePointerDown = (e: React.PointerEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
if (e.button !== 0 && e.pointerType === 'mouse') return;
// Prevent container click bubbling and vertical scrolling
e.stopPropagation();
isManualRef.current = true;
isInteractedRef.current = true;
isPointerDownRef.current = true;
if (animFrameRef.current) {
@@ -339,19 +396,18 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
// Fallback
}
const rect = e.currentTarget.getBoundingClientRect();
cachedRectRef.current = { left: rect.left, width: rect.width };
const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100;
setSliderDOM(percent);
const rect = getContainerRect();
cachedRectRef.current = rect;
if (rect && rect.width > 0) {
const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
setSliderDOM((x / rect.width) * 100);
}
};
const handlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
const handleHandlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
if (!isPointerDownRef.current) return;
e.stopPropagation();
isManualRef.current = true;
isInteractedRef.current = true;
const clientX = e.clientX;
if (pointerRafRef.current) {
@@ -359,22 +415,18 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
}
pointerRafRef.current = requestAnimationFrame(() => {
let rect = cachedRectRef.current;
if (!rect && containerRef.current) {
const r = containerRef.current.getBoundingClientRect();
rect = { left: r.left, width: r.width };
cachedRectRef.current = rect;
}
let rect = cachedRectRef.current || getContainerRect();
cachedRectRef.current = rect;
if (!rect || rect.width <= 0) return;
const x = Math.max(0, Math.min(clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100;
setSliderDOM(percent);
setSliderDOM((x / rect.width) * 100);
});
};
const handlePointerUp = (e: React.PointerEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
const handleHandlePointerUp = (e: React.PointerEvent<HTMLDivElement>) => {
if (!isPointerDownRef.current) return;
e.stopPropagation();
isPointerDownRef.current = false;
cachedRectRef.current = null;
if (pointerRafRef.current) {
@@ -388,26 +440,15 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
}
};
const handlePointerLeave = () => {
if (isPointerDownRef.current) return; // Keep tracking if pointer capture is active
cachedRectRef.current = null;
if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = null;
}
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
const step = e.shiftKey ? 10 : 2;
if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') {
e.preventDefault();
isInteractedRef.current = true;
isManualRef.current = true;
setSliderDOM(currentPercentRef.current - step);
} else if (e.key === 'ArrowRight' || e.key === 'ArrowUp') {
e.preventDefault();
isInteractedRef.current = true;
isManualRef.current = true;
setSliderDOM(currentPercentRef.current + step);
}
@@ -437,8 +478,11 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
return (
<div
ref={containerRef}
onPointerEnter={handleContainerPointerEnter}
onPointerMove={handleContainerPointerMove}
onPointerLeave={handleContainerPointerLeave}
style={{ touchAction: 'pan-y' }}
className="relative w-full h-full select-none overflow-hidden bg-slate-950 transform-gpu [backface-visibility:hidden]"
className="relative w-full h-full select-none overflow-hidden bg-slate-950 transform-gpu [backface-visibility:hidden] cursor-ew-resize"
>
{/* BEFORE Image (Base layer, fixed scale & position, 0% zoom) */}
<img
@@ -466,16 +510,13 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
className="absolute top-0 bottom-0 w-[2.5px] bg-white shadow-[0_0_12px_rgba(255,255,255,1)] pointer-events-none z-10 will-change-transform transform-gpu -translate-x-1/2"
style={{ left: '50%' }}
>
{/* Circular Grab Handle with Before/After Arrow Cue & Touch Padding - Dedicated Drag Trigger with Pointer Capture */}
{/* Circular Grab Handle with Before/After Arrow Cue & Touch Hit Area */}
<div
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerUp}
onPointerCancel={handlePointerUp}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
}}
ref={handleRef}
onPointerDown={handleHandlePointerDown}
onPointerMove={handleHandlePointerMove}
onPointerUp={handleHandlePointerUp}
onPointerCancel={handleHandlePointerUp}
onKeyDown={handleKeyDown}
role="slider"
aria-label="Before and after comparison slider"
@@ -484,7 +525,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
aria-valuenow={50}
tabIndex={0}
style={{ touchAction: 'none' }}
className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 left-1/2 w-8 h-8 rounded-full bg-white shadow-xl border border-slate-200/90 flex items-center justify-center text-xs font-bold text-blue-600 pointer-events-auto cursor-ew-resize touch-none before:absolute before:-inset-3 before:content-['']"
className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 left-1/2 w-9 h-9 sm:w-10 sm:h-10 rounded-full bg-white shadow-xl border border-slate-200/90 flex items-center justify-center text-xs font-bold text-blue-600 pointer-events-auto cursor-ew-resize touch-none before:absolute before:-inset-4 before:content-[''] hover:scale-110 active:scale-95 transition-transform"
>
↔
</div>
@@ -145,11 +145,21 @@ export function CardBeforeAfterSlider({
}
}, []);
const handleContainerPointerDown = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
// On desktop with mouse, clicking anywhere on the card moves the split
if (e.pointerType !== 'mouse' || e.button !== 0) return;
handlePointerDown(e);
},
[handlePointerDown]
);
return (
<div
ref={containerRef}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onPointerDown={handleContainerPointerDown}
style={{
'--split': '50%',
touchAction: 'pan-y', // Allows mobile vertical scrolling across the card and line
+8 -1
View File
@@ -82,7 +82,7 @@ export function Cinematic3DComparison({
};
}, [autoSlide, isDragging, updatePosition]);
// Unified Pointer Handlers on the Slider Arrows Handle
// Unified Pointer Handlers
const handlePointerDown = (e: React.PointerEvent<HTMLDivElement>) => {
if (e.button !== 0 && e.pointerType === 'mouse') return;
e.stopPropagation();
@@ -105,6 +105,12 @@ export function Cinematic3DComparison({
}
};
const handleContainerPointerDown = (e: React.PointerEvent<HTMLDivElement>) => {
// On desktop with mouse, clicking anywhere on the stage moves the slider
if (e.pointerType !== 'mouse' || e.button !== 0) return;
handlePointerDown(e);
};
const handlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
if (!isPointerDownRef.current) return;
e.stopPropagation();
@@ -167,6 +173,7 @@ export function Cinematic3DComparison({
{/* 3D Kinetic Tilt Stage - Allows smooth page scrolling when touching anywhere except the arrows handle */}
<div
ref={containerRef}
onPointerDown={handleContainerPointerDown}
className={`relative w-full ${aspectRatio} rounded-2xl sm:rounded-3xl overflow-hidden preserve-3d transition-transform duration-150 ease-out shadow-cinematic-3d hover:shadow-cinematic-hover border border-slate-900/[0.08] bg-slate-950`}
style={{
'--split': '50%',