diff --git a/components/converter/VisualComparison.tsx b/components/converter/VisualComparison.tsx index 0feb0bf..e1d6c9f 100644 --- a/components/converter/VisualComparison.tsx +++ b/components/converter/VisualComparison.tsx @@ -18,7 +18,7 @@ export default function VisualComparison({ const [draggingUnit, setDraggingUnit] = useState(null); const dragStartX = useRef(0); const dragStartWidth = useRef(0); - const barRef = useRef(null); + const activeBarRef = useRef(null); // Calculate percentages for visual bars using logarithmic scale const withPercentages = useMemo(() => { if (conversions.length === 0) return []; @@ -80,19 +80,20 @@ export default function VisualComparison({ }, []); // Mouse drag handlers - const handleMouseDown = useCallback((e: React.MouseEvent, unit: string, currentPercentage: number) => { + const handleMouseDown = useCallback((e: React.MouseEvent, unit: string, currentPercentage: number, barElement: HTMLDivElement) => { if (!onValueChange) return; e.preventDefault(); setDraggingUnit(unit); dragStartX.current = e.clientX; dragStartWidth.current = currentPercentage; + activeBarRef.current = barElement; }, [onValueChange]); const handleMouseMove = useCallback((e: MouseEvent) => { - if (!draggingUnit || !barRef.current || !onValueChange) return; + if (!draggingUnit || !activeBarRef.current || !onValueChange) return; - const barWidth = barRef.current.offsetWidth; + const barWidth = activeBarRef.current.offsetWidth; const deltaX = e.clientX - dragStartX.current; const deltaPercentage = (deltaX / barWidth) * 100; @@ -116,24 +117,26 @@ export default function VisualComparison({ const handleMouseUp = useCallback(() => { setDraggingUnit(null); + activeBarRef.current = null; }, []); // Touch drag handlers - const handleTouchStart = useCallback((e: React.TouchEvent, unit: string, currentPercentage: number) => { + const handleTouchStart = useCallback((e: React.TouchEvent, unit: string, currentPercentage: number, barElement: HTMLDivElement) => { if (!onValueChange) return; const touch = e.touches[0]; setDraggingUnit(unit); dragStartX.current = touch.clientX; dragStartWidth.current = currentPercentage; + activeBarRef.current = barElement; }, [onValueChange]); const handleTouchMove = useCallback((e: TouchEvent) => { - if (!draggingUnit || !barRef.current || !onValueChange) return; + if (!draggingUnit || !activeBarRef.current || !onValueChange) return; e.preventDefault(); // Prevent scrolling while dragging const touch = e.touches[0]; - const barWidth = barRef.current.offsetWidth; + const barWidth = activeBarRef.current.offsetWidth; const deltaX = touch.clientX - dragStartX.current; const deltaPercentage = (deltaX / barWidth) * 100; @@ -154,6 +157,7 @@ export default function VisualComparison({ const handleTouchEnd = useCallback(() => { setDraggingUnit(null); + activeBarRef.current = null; }, []); // Add/remove global event listeners for drag @@ -202,15 +206,22 @@ export default function VisualComparison({ {/* Progress bar */}
isDraggable && handleMouseDown(e, item.unit, item.percentage)} - onTouchStart={(e) => isDraggable && handleTouchStart(e, item.unit, item.percentage)} + onMouseDown={(e) => { + if (isDraggable && e.currentTarget instanceof HTMLDivElement) { + handleMouseDown(e, item.unit, item.percentage, e.currentTarget); + } + }} + onTouchStart={(e) => { + if (isDraggable && e.currentTarget instanceof HTMLDivElement) { + handleTouchStart(e, item.unit, item.percentage, e.currentTarget); + } + }} > {/* Colored fill */}