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
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;
if (dividerRef.current) {
dividerRef.current.style.left = `${clamped}%`;
@@ -300,8 +300,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
}
}, [isHovered, item.mediaType]);
// Smooth Automatic Oscillating Scan when hovered:
// Runs at 60-120fps directly mutating DOM refs (0 React re-renders!)
// When unhovered, reset slider smoothly to 50%
useEffect(() => {
if (item.mediaType !== 'comparison') return;
@@ -312,67 +311,49 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
animFrameRef.current = null;
}
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]);
const getContainerRect = () => {
if (!containerRef.current) return null;
const r = containerRef.current.getBoundingClientRect();
return { left: r.left, width: r.width };
};
// Instant pixel-precise cursor alignment calculation
const updateToClientX = useCallback((clientX: number) => {
if (!containerRef.current) return;
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
const handleContainerPointerEnter = (e: React.PointerEvent<HTMLDivElement>) => {
// Desktop Mouse Movement: Immediately aligns comparison bar with cursor on hover and stays locked
const handleContainerPointerEnter = (e: React.PointerEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
if (e.pointerType === 'mouse') {
cachedRectRef.current = getContainerRect();
if (!('pointerType' in e) || e.pointerType === 'mouse') {
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 (e.pointerType === 'mouse') {
if (!('pointerType' in e) || 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);
updateToClientX(clientX);
});
}
};
const handleContainerPointerLeave = () => {
const handleContainerPointerLeave = (e: React.PointerEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement>) => {
if (isPointerDownRef.current) return;
isManualRef.current = false;
cachedRectRef.current = null;
if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = null;
if (!('pointerType' in e) || e.pointerType === 'mouse') {
isManualRef.current = false;
if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = null;
}
setSliderDOM(50);
}
};
@@ -396,12 +377,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
// Fallback
}
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);
}
updateToClientX(e.clientX);
};
const handleHandlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
@@ -415,12 +391,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
}
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);
updateToClientX(clientX);
});
};
@@ -428,7 +399,6 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
if (!isPointerDownRef.current) return;
e.stopPropagation();
isPointerDownRef.current = false;
cachedRectRef.current = null;
if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = null;
@@ -479,8 +449,11 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
<div
ref={containerRef}
onPointerEnter={handleContainerPointerEnter}
onMouseEnter={handleContainerPointerEnter}
onPointerMove={handleContainerPointerMove}
onMouseMove={handleContainerPointerMove}
onPointerLeave={handleContainerPointerLeave}
onMouseLeave={handleContainerPointerLeave}
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"
>