fix(home-slider): ensure comparison bar aligns immediately with cursor on hover

This commit is contained in:
2026-11-05 13:31:58 +05:30
parent 7760722c76
commit 2c8286b837
@@ -275,7 +275,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
// Directly mutate DOM styles on the GPU layer without triggering React reconciliation // Directly mutate DOM styles on the GPU layer without triggering React reconciliation
const setSliderDOM = useCallback((percent: number) => { const setSliderDOM = useCallback((percent: number) => {
const clamped = Math.max(8, Math.min(92, percent)); const clamped = Math.max(0, Math.min(100, percent));
currentPercentRef.current = clamped; currentPercentRef.current = clamped;
if (dividerRef.current) { if (dividerRef.current) {
dividerRef.current.style.left = `${clamped}%`; dividerRef.current.style.left = `${clamped}%`;
@@ -300,8 +300,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
} }
}, [isHovered, item.mediaType]); }, [isHovered, item.mediaType]);
// Smooth Automatic Oscillating Scan when hovered: // When unhovered, reset slider smoothly to 50%
// Runs at 60-120fps directly mutating DOM refs (0 React re-renders!)
useEffect(() => { useEffect(() => {
if (item.mediaType !== 'comparison') return; if (item.mediaType !== 'comparison') return;
@@ -312,67 +311,49 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
animFrameRef.current = null; animFrameRef.current = null;
} }
setSliderDOM(50); setSliderDOM(50);
return;
} }
const startTime = performance.now();
const sweep = (now: number) => {
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;
setSliderDOM(pos);
}
animFrameRef.current = requestAnimationFrame(sweep);
};
animFrameRef.current = requestAnimationFrame(sweep);
return () => {
if (animFrameRef.current) {
cancelAnimationFrame(animFrameRef.current);
animFrameRef.current = null;
}
};
}, [isHovered, item.mediaType, setSliderDOM]); }, [isHovered, item.mediaType, setSliderDOM]);
const getContainerRect = () => { // Instant pixel-precise cursor alignment calculation
if (!containerRef.current) return null; const updateToClientX = useCallback((clientX: number) => {
const r = containerRef.current.getBoundingClientRect(); if (!containerRef.current) return;
return { left: r.left, width: r.width }; const rect = containerRef.current.getBoundingClientRect();
}; if (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]);
// Desktop Mouse Movement: Tracks cursor across the entire card effortlessly without clicking // Desktop Mouse Movement: Immediately aligns comparison bar with cursor on hover and stays locked
const handleContainerPointerEnter = (e: React.PointerEvent<HTMLDivElement>) => { const handleContainerPointerEnter = (e: React.PointerEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return; if (item.mediaType !== 'comparison') return;
if (e.pointerType === 'mouse') { if (!('pointerType' in e) || e.pointerType === 'mouse') {
cachedRectRef.current = getContainerRect(); isManualRef.current = true;
updateToClientX(e.clientX);
} }
}; };
const handleContainerPointerMove = (e: React.PointerEvent<HTMLDivElement>) => { const handleContainerPointerMove = (e: React.PointerEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return; if (item.mediaType !== 'comparison') return;
if (e.pointerType === 'mouse') { if (!('pointerType' in e) || e.pointerType === 'mouse') {
isManualRef.current = true; isManualRef.current = true;
const clientX = e.clientX; const clientX = e.clientX;
if (pointerRafRef.current) cancelAnimationFrame(pointerRafRef.current); if (pointerRafRef.current) cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = requestAnimationFrame(() => { pointerRafRef.current = requestAnimationFrame(() => {
let rect = cachedRectRef.current || getContainerRect(); updateToClientX(clientX);
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 = () => { const handleContainerPointerLeave = (e: React.PointerEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement>) => {
if (isPointerDownRef.current) return; if (isPointerDownRef.current) return;
isManualRef.current = false; if (!('pointerType' in e) || e.pointerType === 'mouse') {
cachedRectRef.current = null; isManualRef.current = false;
if (pointerRafRef.current) { if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current); cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = null; pointerRafRef.current = null;
}
setSliderDOM(50);
} }
}; };
@@ -396,12 +377,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
// Fallback // Fallback
} }
const rect = getContainerRect(); updateToClientX(e.clientX);
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 handleHandlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => { const handleHandlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
@@ -415,12 +391,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
} }
pointerRafRef.current = requestAnimationFrame(() => { pointerRafRef.current = requestAnimationFrame(() => {
let rect = cachedRectRef.current || getContainerRect(); updateToClientX(clientX);
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);
}); });
}; };
@@ -428,7 +399,6 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
if (!isPointerDownRef.current) return; if (!isPointerDownRef.current) return;
e.stopPropagation(); e.stopPropagation();
isPointerDownRef.current = false; isPointerDownRef.current = false;
cachedRectRef.current = null;
if (pointerRafRef.current) { if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current); cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = null; pointerRafRef.current = null;
@@ -479,8 +449,11 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
<div <div
ref={containerRef} ref={containerRef}
onPointerEnter={handleContainerPointerEnter} onPointerEnter={handleContainerPointerEnter}
onMouseEnter={handleContainerPointerEnter}
onPointerMove={handleContainerPointerMove} onPointerMove={handleContainerPointerMove}
onMouseMove={handleContainerPointerMove}
onPointerLeave={handleContainerPointerLeave} onPointerLeave={handleContainerPointerLeave}
onMouseLeave={handleContainerPointerLeave}
style={{ touchAction: 'pan-y' }} style={{ touchAction: 'pan-y' }}
className="relative w-full h-full select-none overflow-hidden bg-slate-950 transform-gpu [backface-visibility:hidden] cursor-ew-resize" className="relative w-full h-full select-none overflow-hidden bg-slate-950 transform-gpu [backface-visibility:hidden] cursor-ew-resize"
> >