fix(ui): prevent zero-height collapse in result comparison slider and preload dimensions

This commit is contained in:
2026-11-11 18:09:14 +05:30
parent 6961846f73
commit da786b027c
2 changed files with 30 additions and 4 deletions
@@ -463,7 +463,7 @@ export function DynamicToolWorkspace({
beforeUrl={comparisonBeforeUrl || primaryPreview || primaryExample.before} beforeUrl={comparisonBeforeUrl || primaryPreview || primaryExample.before}
afterUrl={resultUrl} afterUrl={resultUrl}
title={toolName} title={toolName}
aspectRatio="aspect-auto" aspectRatio="aspect-[4/3] sm:aspect-[16/10]"
objectFit="contain" objectFit="contain"
beforeLabel="ORIGINAL" beforeLabel="ORIGINAL"
afterLabel="RESULT" afterLabel="RESULT"
+29 -3
View File
@@ -49,6 +49,29 @@ export function CardBeforeAfterSlider({
} }
}; };
// Proactively measure image natural dimensions to prevent zero-height collapse
useEffect(() => {
if (!afterUrl && !beforeUrl) return;
const targetUrl = afterUrl || beforeUrl;
let isCancelled = false;
const img = new Image();
img.onload = () => {
if (!isCancelled && img.naturalWidth && img.naturalHeight) {
setNaturalDimensions({ width: img.naturalWidth, height: img.naturalHeight });
}
};
img.src = targetUrl;
if (img.complete && img.naturalWidth && img.naturalHeight) {
setNaturalDimensions({ width: img.naturalWidth, height: img.naturalHeight });
}
return () => {
isCancelled = true;
};
}, [afterUrl, beforeUrl]);
// Smooth gentle oscillation: runs on desktop when hovered before user manually drags // Smooth gentle oscillation: runs on desktop when hovered before user manually drags
useEffect(() => { useEffect(() => {
let startTime: number | null = null; let startTime: number | null = null;
@@ -147,7 +170,7 @@ export function CardBeforeAfterSlider({
}; };
const imgFitClass = objectFit === 'contain' ? 'object-contain object-center' : 'object-cover'; const imgFitClass = objectFit === 'contain' ? 'object-contain object-center' : 'object-cover';
const containerAspectClass = naturalDimensions ? '' : aspectRatio; const containerAspectClass = naturalDimensions ? '' : (aspectRatio === 'aspect-auto' ? 'aspect-[4/3] sm:aspect-[16/10]' : aspectRatio);
return ( return (
<div <div
@@ -155,7 +178,7 @@ export function CardBeforeAfterSlider({
onPointerDown={handleContainerPointerDown} onPointerDown={handleContainerPointerDown}
onMouseEnter={handleMouseEnter} onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave} onMouseLeave={handleMouseLeave}
className={`relative ${naturalDimensions ? 'mx-auto' : 'w-full ' + containerAspectClass} select-none overflow-hidden rounded-2xl bg-paper-2 cursor-ew-resize group/slider touch-pan-y max-w-full`} className={`relative ${naturalDimensions ? 'mx-auto' : 'w-full ' + containerAspectClass} min-h-[380px] sm:min-h-[500px] select-none overflow-hidden rounded-2xl bg-paper-2 cursor-ew-resize group/slider touch-pan-y max-w-full`}
style={{ style={{
touchAction: 'pan-y', touchAction: 'pan-y',
['--split' as any]: '50%', ['--split' as any]: '50%',
@@ -163,9 +186,12 @@ export function CardBeforeAfterSlider({
? { ? {
aspectRatio: `${naturalDimensions.width} / ${naturalDimensions.height}`, aspectRatio: `${naturalDimensions.width} / ${naturalDimensions.height}`,
width: `min(100%, calc(640px * (${naturalDimensions.width} / ${naturalDimensions.height})))`, width: `min(100%, calc(640px * (${naturalDimensions.width} / ${naturalDimensions.height})))`,
minHeight: '340px',
maxHeight: '640px', maxHeight: '640px',
} }
: {}), : {
minHeight: '380px',
}),
}} }}
role="region" role="region"
aria-label={`${title} comparison slider`} aria-label={`${title} comparison slider`}