image-converter-client/app/routes/DragableBox.tsx

131 lines
3.5 KiB
TypeScript
Raw Permalink Normal View History

2024-11-02 17:25:14 +00:00
import { useState, useEffect, MouseEvent, useRef, Dispatch, SetStateAction } from 'react';
type Coordinates = {
x: number;
y: number;
}
2024-11-02 15:47:17 +00:00
interface DraggableBoxProps {
2024-11-03 17:34:21 +00:00
position: Coordinates;
2024-11-02 15:47:17 +00:00
className?: string;
children?: React.ReactNode;
2024-11-03 17:34:21 +00:00
coordSetter: Dispatch<SetStateAction<Coordinates>>;
2024-11-03 21:39:34 +00:00
mode?: 'handle' | 'box';
width?: number;
height?: number;
onDrag?: (delta: Coordinates) => void;
2024-11-02 15:47:17 +00:00
}
const DraggableBox = ({
2024-11-03 17:34:21 +00:00
position,
2024-11-02 15:47:17 +00:00
className = "",
2024-11-02 17:25:14 +00:00
children = "",
2024-11-03 21:39:34 +00:00
coordSetter,
mode = 'handle',
width = 0,
height = 0,
onDrag
2024-11-02 15:47:17 +00:00
}: DraggableBoxProps) => {
const [isDragging, setIsDragging] = useState(false);
const [dragOffset, setDragOffset] = useState({ x: 0, y: 0 });
const dragRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!isDragging) return;
const handleMouseMove = (e: globalThis.MouseEvent) => {
if (!dragRef.current?.parentElement) return;
const parent = dragRef.current.parentElement;
const parentRect = parent.getBoundingClientRect();
2024-11-03 21:39:34 +00:00
if (mode === 'handle') {
const x = ((e.clientX - parentRect.left - dragOffset.x) / parentRect.width) * 100;
const y = ((e.clientY - parentRect.top - dragOffset.y) / parentRect.height) * 100;
const newX = Math.min(Math.max(0, x), 100);
const newY = Math.min(Math.max(0, y), 100);
coordSetter({
x: Number(newX.toFixed(1)),
y: Number(newY.toFixed(1))
});
} else {
const x = ((e.clientX - parentRect.left - dragOffset.x) / parentRect.width) * 100;
const y = ((e.clientY - parentRect.top - dragOffset.y) / parentRect.height) * 100;
2024-11-02 15:47:17 +00:00
2024-11-03 21:39:34 +00:00
const maxX = 100 - width;
const maxY = 100 - height;
2024-11-02 15:47:17 +00:00
2024-11-03 21:39:34 +00:00
const newX = Math.min(Math.max(0, x), maxX);
const newY = Math.min(Math.max(0, y), maxY);
2024-11-02 22:30:55 +00:00
2024-11-03 21:39:34 +00:00
coordSetter({
x: Number(newX.toFixed(1)),
y: Number(newY.toFixed(1))
});
onDrag?.({ x: newX, y: newY });
}
2024-11-02 15:47:17 +00:00
};
const handleMouseUp = () => setIsDragging(false);
window.addEventListener('mousemove', handleMouseMove);
window.addEventListener('mouseup', handleMouseUp);
return () => {
window.removeEventListener('mousemove', handleMouseMove);
window.removeEventListener('mouseup', handleMouseUp);
};
2024-11-03 21:39:34 +00:00
}, [isDragging, dragOffset, mode, width, height, onDrag]);
2024-11-02 15:47:17 +00:00
const handleMouseDown = (e: MouseEvent) => {
if (!dragRef.current?.parentElement) return;
const parentRect = dragRef.current.parentElement.getBoundingClientRect();
2024-11-03 21:39:34 +00:00
const elementRect = dragRef.current.getBoundingClientRect();
if (mode === 'handle') {
setDragOffset({
x: e.clientX - elementRect.left,
y: e.clientY - elementRect.top
});
} else {
setDragOffset({
x: e.clientX - parentRect.left - (position.x * parentRect.width / 100),
y: e.clientY - parentRect.top - (position.y * parentRect.height / 100)
});
}
2024-11-02 15:47:17 +00:00
setIsDragging(true);
};
2024-11-03 21:39:34 +00:00
const style = mode === 'handle' ? {
left: `${position.x}%`,
top: `${position.y}%`,
transform: 'translate(-50%, -50%)', // Center the handle
} : {
left: `${position.x}%`,
top: `${position.y}%`,
width: `${width}%`,
height: `${height}%`,
};
2024-11-02 15:47:17 +00:00
return (
<div
ref={dragRef}
2024-11-03 21:39:34 +00:00
className={`absolute cursor-pointer transform-gpu ${mode === 'box' ? 'cursor-move' : ''} ${className}`}
2024-11-02 15:47:17 +00:00
style={{
2024-11-03 21:39:34 +00:00
...style,
2024-11-02 15:47:17 +00:00
userSelect: 'none',
}}
onMouseDown={handleMouseDown}
>
{children}
</div>
);
};
export default DraggableBox;