| 9 | }; |
| 10 | |
| 11 | export default function Resizable({ |
| 12 | children, |
| 13 | isHorizontal = true, |
| 14 | initialSize, |
| 15 | minimumSize, |
| 16 | maximumSize, |
| 17 | }: ResizableProps) { |
| 18 | const [dimension, setDimension] = useState(initialSize); |
| 19 | const previousDragPosition = useRef<{ x: number; y: number } | null>(null); |
| 20 | |
| 21 | const handleDragStart = (e: React.MouseEvent<HTMLDivElement>): void => { |
| 22 | previousDragPosition.current = { |
| 23 | x: e.clientX, |
| 24 | y: e.clientY, |
| 25 | }; |
| 26 | }; |
| 27 | |
| 28 | const handleDrag = (e: MouseEvent) => { |
| 29 | if (previousDragPosition.current == null) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | e.preventDefault(); |
| 34 | |
| 35 | let offset = 0; |
| 36 | if (isHorizontal) { |
| 37 | offset = e.clientX - previousDragPosition.current.x; |
| 38 | } else { |
| 39 | offset = e.clientY - previousDragPosition.current.y; |
| 40 | } |
| 41 | let newValue = dimension - offset; |
| 42 | if (minimumSize != null) { |
| 43 | newValue = Math.max(minimumSize, newValue); |
| 44 | } |
| 45 | if (maximumSize != null) { |
| 46 | newValue = Math.min(maximumSize, newValue); |
| 47 | } |
| 48 | setDimension(newValue); |
| 49 | previousDragPosition.current = { |
| 50 | x: e.clientX, |
| 51 | y: e.clientY, |
| 52 | }; |
| 53 | }; |
| 54 | |
| 55 | const handleDragEnd = () => { |
| 56 | previousDragPosition.current = null; |
| 57 | }; |
| 58 | |
| 59 | useEffect(() => { |
| 60 | window.addEventListener("mousemove", handleDrag); |
| 61 | window.addEventListener("mouseup", handleDragEnd); |
| 62 | return () => { |
| 63 | window.removeEventListener("mousemove", handleDrag); |
| 64 | window.removeEventListener("mouseup", handleDragEnd); |
| 65 | }; |
| 66 | }, [handleDrag, handleDragEnd]); |
| 67 | |
| 68 | const style = () => { |