fix(slider): lock comparison clip layer to container bounds and align before/after orientation

This commit is contained in:
2026-11-05 13:42:18 +05:30
parent 2c8286b837
commit d2e43a6da0
@@ -262,7 +262,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
const videoRef = useRef<HTMLVideoElement>(null); const videoRef = useRef<HTMLVideoElement>(null);
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
const dividerRef = useRef<HTMLDivElement>(null); const dividerRef = useRef<HTMLDivElement>(null);
const afterImgRef = useRef<HTMLImageElement>(null); const clipLayerRef = useRef<HTMLDivElement>(null);
const handleRef = useRef<HTMLDivElement>(null); const handleRef = useRef<HTMLDivElement>(null);
// High performance refs - completely eliminates per-frame React re-renders! // High performance refs - completely eliminates per-frame React re-renders!
@@ -280,8 +280,8 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
if (dividerRef.current) { if (dividerRef.current) {
dividerRef.current.style.left = `${clamped}%`; dividerRef.current.style.left = `${clamped}%`;
} }
if (afterImgRef.current) { if (clipLayerRef.current) {
afterImgRef.current.style.clipPath = `inset(0 ${100 - clamped}% 0 0)`; clipLayerRef.current.style.clipPath = `inset(0 ${100 - clamped}% 0 0)`;
} }
if (handleRef.current) { if (handleRef.current) {
handleRef.current.setAttribute('aria-valuenow', `${Math.round(clamped)}`); handleRef.current.setAttribute('aria-valuenow', `${Math.round(clamped)}`);
@@ -317,7 +317,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
// Instant pixel-precise cursor alignment calculation // Instant pixel-precise cursor alignment calculation
const updateToClientX = useCallback((clientX: number) => { const updateToClientX = useCallback((clientX: number) => {
if (!containerRef.current) return; if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect(); const rect = cachedRectRef.current || containerRef.current.getBoundingClientRect();
if (rect.width <= 0) return; if (rect.width <= 0) return;
const x = Math.max(0, Math.min(clientX - rect.left, rect.width)); const x = Math.max(0, Math.min(clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100; const percent = (x / rect.width) * 100;
@@ -362,6 +362,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
if (item.mediaType !== 'comparison') return; if (item.mediaType !== 'comparison') return;
if (e.button !== 0 && e.pointerType === 'mouse') return; if (e.button !== 0 && e.pointerType === 'mouse') return;
e.preventDefault();
e.stopPropagation(); e.stopPropagation();
isManualRef.current = true; isManualRef.current = true;
isPointerDownRef.current = true; isPointerDownRef.current = true;
@@ -377,6 +378,11 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
// Fallback // Fallback
} }
if (containerRef.current) {
const r = containerRef.current.getBoundingClientRect();
cachedRectRef.current = { left: r.left, width: r.width };
}
updateToClientX(e.clientX); updateToClientX(e.clientX);
}; };
@@ -399,6 +405,7 @@ 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;
@@ -457,25 +464,29 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
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"
> >
{/* BEFORE Image (Base layer, fixed scale & position, 0% zoom) */} {/* Layer 1: AFTER Image (Base layer, fixed scale & position, 0% zoom) */}
<img <img
src={item.beforeUrl} src={item.afterUrl}
alt="Before" alt="After"
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none transform-gpu will-change-transform [backface-visibility:hidden]" className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none transform-gpu will-change-transform [backface-visibility:hidden]"
loading="lazy" loading="lazy"
/> />
{/* AFTER Image (Top layer, exact same position & scale, clipped via clip-path) */} {/* Layer 2: BEFORE Image (Clipped container-sized layer - 100% exact alignment with slider divider on phone & desktop) */}
<img <div
ref={afterImgRef} ref={clipLayerRef}
src={item.afterUrl} className="absolute inset-0 overflow-hidden pointer-events-none transform-gpu will-change-transform [backface-visibility:hidden]"
alt="After"
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none transform-gpu will-change-transform [backface-visibility:hidden]"
style={{ style={{
clipPath: 'inset(0 50% 0 0)', clipPath: 'inset(0 50% 0 0)',
}} }}
loading="lazy" >
/> <img
src={item.beforeUrl}
alt="Before"
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none transform-gpu will-change-transform [backface-visibility:hidden]"
loading="lazy"
/>
</div>
{/* Glowing Slider Divider Line (pointer-events-none so touching the line allows page scroll) */} {/* Glowing Slider Divider Line (pointer-events-none so touching the line allows page scroll) */}
<div <div
@@ -493,8 +504,8 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
role="slider" role="slider"
aria-label="Before and after comparison slider" aria-label="Before and after comparison slider"
aria-valuemin={8} aria-valuemin={0}
aria-valuemax={92} aria-valuemax={100}
aria-valuenow={50} aria-valuenow={50}
tabIndex={0} tabIndex={0}
style={{ touchAction: 'none' }} style={{ touchAction: 'none' }}
@@ -504,16 +515,16 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
</div> </div>
</div> </div>
{/* Badges with Smooth Contrast */} {/* Badges with Smooth Contrast: BEFORE on left, AFTER on right */}
<div <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" 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 shadow-xs">
AFTER
</span>
<span className="bg-slate-950/80 px-2 py-0.5 rounded backdrop-blur border border-white/10 shadow-xs"> <span className="bg-slate-950/80 px-2 py-0.5 rounded backdrop-blur border border-white/10 shadow-xs">
BEFORE BEFORE
</span> </span>
<span className="bg-blue-600/90 px-2 py-0.5 rounded backdrop-blur border border-white/20 shadow-xs">
AFTER
</span>
</div> </div>
</div> </div>
); );