fix(slider): lock comparison clip layer to container bounds and align before/after orientation
This commit is contained in:
@@ -262,7 +262,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
const videoRef = useRef<HTMLVideoElement>(null);
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const dividerRef = useRef<HTMLDivElement>(null);
|
||||
const afterImgRef = useRef<HTMLImageElement>(null);
|
||||
const clipLayerRef = useRef<HTMLDivElement>(null);
|
||||
const handleRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// High performance refs - completely eliminates per-frame React re-renders!
|
||||
@@ -280,8 +280,8 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
if (dividerRef.current) {
|
||||
dividerRef.current.style.left = `${clamped}%`;
|
||||
}
|
||||
if (afterImgRef.current) {
|
||||
afterImgRef.current.style.clipPath = `inset(0 ${100 - clamped}% 0 0)`;
|
||||
if (clipLayerRef.current) {
|
||||
clipLayerRef.current.style.clipPath = `inset(0 ${100 - clamped}% 0 0)`;
|
||||
}
|
||||
if (handleRef.current) {
|
||||
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
|
||||
const updateToClientX = useCallback((clientX: number) => {
|
||||
if (!containerRef.current) return;
|
||||
const rect = containerRef.current.getBoundingClientRect();
|
||||
const rect = cachedRectRef.current || 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;
|
||||
@@ -362,6 +362,7 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
if (item.mediaType !== 'comparison') return;
|
||||
if (e.button !== 0 && e.pointerType === 'mouse') return;
|
||||
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
isManualRef.current = true;
|
||||
isPointerDownRef.current = true;
|
||||
@@ -377,6 +378,11 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
// Fallback
|
||||
}
|
||||
|
||||
if (containerRef.current) {
|
||||
const r = containerRef.current.getBoundingClientRect();
|
||||
cachedRectRef.current = { left: r.left, width: r.width };
|
||||
}
|
||||
|
||||
updateToClientX(e.clientX);
|
||||
};
|
||||
|
||||
@@ -399,6 +405,7 @@ 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;
|
||||
@@ -457,25 +464,29 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
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"
|
||||
>
|
||||
{/* BEFORE Image (Base layer, fixed scale & position, 0% zoom) */}
|
||||
{/* Layer 1: AFTER Image (Base layer, fixed scale & position, 0% zoom) */}
|
||||
<img
|
||||
src={item.beforeUrl}
|
||||
alt="Before"
|
||||
src={item.afterUrl}
|
||||
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]"
|
||||
loading="lazy"
|
||||
/>
|
||||
|
||||
{/* AFTER Image (Top layer, exact same position & scale, clipped via clip-path) */}
|
||||
<img
|
||||
ref={afterImgRef}
|
||||
src={item.afterUrl}
|
||||
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]"
|
||||
{/* Layer 2: BEFORE Image (Clipped container-sized layer - 100% exact alignment with slider divider on phone & desktop) */}
|
||||
<div
|
||||
ref={clipLayerRef}
|
||||
className="absolute inset-0 overflow-hidden pointer-events-none transform-gpu will-change-transform [backface-visibility:hidden]"
|
||||
style={{
|
||||
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) */}
|
||||
<div
|
||||
@@ -493,8 +504,8 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
onKeyDown={handleKeyDown}
|
||||
role="slider"
|
||||
aria-label="Before and after comparison slider"
|
||||
aria-valuemin={8}
|
||||
aria-valuemax={92}
|
||||
aria-valuemin={0}
|
||||
aria-valuemax={100}
|
||||
aria-valuenow={50}
|
||||
tabIndex={0}
|
||||
style={{ touchAction: 'none' }}
|
||||
@@ -504,16 +515,16 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Badges with Smooth Contrast */}
|
||||
{/* Badges with Smooth Contrast: BEFORE on left, AFTER on right */}
|
||||
<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"
|
||||
>
|
||||
<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">
|
||||
BEFORE
|
||||
</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>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user