fix(slider): add pointer capture, touch-action none, link isolation, and zero-reflow RAF tracking for ultra-smooth mobile responsiveness

This commit is contained in:
2026-11-05 13:03:19 +05:30
parent f579b263b4
commit 3d147dd3de
5 changed files with 375 additions and 159 deletions
@@ -185,31 +185,63 @@ 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 */}
<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"
>
{/* 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"
/>
{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 touch-action: none) */}
<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 === '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-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 hover:bg-blue-600 transition-colors shadow-sm"
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"
>
{/* 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
@@ -234,6 +266,9 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
// 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);
const pointerRafRef = useRef<number | null>(null);
const cachedRectRef = useRef<{ left: number; width: number } | null>(null);
@@ -241,12 +276,16 @@ 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));
currentPercentRef.current = clamped;
if (dividerRef.current) {
dividerRef.current.style.left = `${clamped}%`;
}
if (afterImgRef.current) {
afterImgRef.current.style.clipPath = `inset(0 ${100 - clamped}% 0 0)`;
}
if (containerRef.current) {
containerRef.current.setAttribute('aria-valuenow', `${Math.round(clamped)}`);
}
}, []);
// Video Autoplay Muted Loop only on Hover (No zoom, no scale)
@@ -267,18 +306,22 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
if (item.mediaType !== 'comparison') return;
if (!isHovered) {
isManualRef.current = false;
if (animFrameRef.current) {
cancelAnimationFrame(animFrameRef.current);
animFrameRef.current = null;
if (!isInteractedRef.current) {
if (animFrameRef.current) {
cancelAnimationFrame(animFrameRef.current);
animFrameRef.current = null;
}
setSliderDOM(50);
}
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) {
if (!isManualRef.current && !isInteractedRef.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;
@@ -298,17 +341,49 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
}, [isHovered, item.mediaType, setSliderDOM]);
// Pointer position tracking:
// Caches container bounding rect on enter to avoid layout thrashing in pointermove.
// Updates slider DOM in requestAnimationFrame with zero React state calls.
// 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 handlePointerDown = (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) {
cancelAnimationFrame(animFrameRef.current);
animFrameRef.current = null;
}
try {
e.currentTarget.setPointerCapture(e.pointerId);
} catch {
// 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 handlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
if (!isPointerDownRef.current) return;
isManualRef.current = true;
isInteractedRef.current = true;
const clientX = e.clientX;
if (pointerRafRef.current) {
@@ -330,13 +405,44 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
});
};
const handlePointerLeave = () => {
isManualRef.current = false;
const handlePointerUp = (e: React.PointerEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
isPointerDownRef.current = false;
cachedRectRef.current = null;
if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = null;
}
try {
e.currentTarget.releasePointerCapture(e.pointerId);
} catch {
// Fallback
}
};
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);
}
};
// 1. VIDEO (Strictly locked to fixed 680px centered width - ZERO ZOOM)
@@ -364,9 +470,20 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
<div
ref={containerRef}
onPointerEnter={handlePointerEnter}
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerUp}
onPointerCancel={handlePointerUp}
onPointerLeave={handlePointerLeave}
className="relative w-full h-full select-none cursor-ew-resize overflow-hidden bg-slate-950 transform-gpu [backface-visibility:hidden]"
onKeyDown={handleKeyDown}
role="slider"
aria-label="Before and after comparison slider"
aria-valuemin={8}
aria-valuemax={92}
aria-valuenow={50}
tabIndex={0}
style={{ touchAction: 'none' }}
className="relative w-full h-full select-none cursor-ew-resize overflow-hidden bg-slate-950 transform-gpu [backface-visibility:hidden] touch-none"
>
{/* BEFORE Image (Base layer, fixed scale & position, 0% zoom) */}
<img
@@ -391,25 +508,23 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
{/* Glowing Slider Divider Line (Direct GPU transform & position) */}
<div
ref={dividerRef}
className="absolute top-0 bottom-0 w-[2px] 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"
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 */}
<div className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 left-1/2 w-7 h-7 rounded-full bg-white shadow-lg border border-slate-200/90 flex items-center justify-center text-[10px] font-bold text-slate-900 pointer-events-none">
{/* Circular Grab Handle with Before/After Arrow Cue & Touch Padding */}
<div 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-none before:absolute before:-inset-3 before:content-['']">
↔
</div>
</div>
{/* Badges with Smooth GPU Opacity Transition (No DOM mounting/unmounting) */}
{/* Badges with Smooth Contrast */}
<div
className={`absolute bottom-3 inset-x-3 flex justify-between pointer-events-none z-20 text-[9px] font-mono font-bold uppercase tracking-wider text-white transition-opacity duration-300 ease-[cubic-bezier(0.16,1,0.3,1)] ${
isHovered ? 'opacity-100' : 'opacity-0'
}`}
className="absolute bottom-3 inset-x-3 flex justify-between pointer-events-none z-20 text-[9px] font-mono font-bold uppercase tracking-wider text-white"
>
<span className="bg-slate-950/80 px-2 py-0.5 rounded backdrop-blur border border-white/10">
<span className="bg-slate-950/80 px-2 py-0.5 rounded backdrop-blur border border-white/10 shadow-xs">
AFTER
</span>
<span className="bg-slate-950/80 px-2 py-0.5 rounded backdrop-blur border border-white/10">
<span className="bg-slate-950/80 px-2 py-0.5 rounded backdrop-blur border border-white/10 shadow-xs">
BEFORE
</span>
</div>