fix(ui): prevent zero-height collapse in result comparison slider and preload dimensions
This commit is contained in:
@@ -463,7 +463,7 @@ export function DynamicToolWorkspace({
|
||||
beforeUrl={comparisonBeforeUrl || primaryPreview || primaryExample.before}
|
||||
afterUrl={resultUrl}
|
||||
title={toolName}
|
||||
aspectRatio="aspect-auto"
|
||||
aspectRatio="aspect-[4/3] sm:aspect-[16/10]"
|
||||
objectFit="contain"
|
||||
beforeLabel="ORIGINAL"
|
||||
afterLabel="RESULT"
|
||||
|
||||
@@ -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
|
||||
useEffect(() => {
|
||||
let startTime: number | null = null;
|
||||
@@ -147,7 +170,7 @@ export function CardBeforeAfterSlider({
|
||||
};
|
||||
|
||||
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 (
|
||||
<div
|
||||
@@ -155,7 +178,7 @@ export function CardBeforeAfterSlider({
|
||||
onPointerDown={handleContainerPointerDown}
|
||||
onMouseEnter={handleMouseEnter}
|
||||
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={{
|
||||
touchAction: 'pan-y',
|
||||
['--split' as any]: '50%',
|
||||
@@ -163,9 +186,12 @@ export function CardBeforeAfterSlider({
|
||||
? {
|
||||
aspectRatio: `${naturalDimensions.width} / ${naturalDimensions.height}`,
|
||||
width: `min(100%, calc(640px * (${naturalDimensions.width} / ${naturalDimensions.height})))`,
|
||||
minHeight: '340px',
|
||||
maxHeight: '640px',
|
||||
}
|
||||
: {}),
|
||||
: {
|
||||
minHeight: '380px',
|
||||
}),
|
||||
}}
|
||||
role="region"
|
||||
aria-label={`${title} comparison slider`}
|
||||
|
||||
Reference in New Issue
Block a user