Reubencf commited on
Commit
992b46f
·
1 Parent(s): a1cccea

fixing dragging issues

Browse files
Files changed (1) hide show
  1. app/components/Window.tsx +27 -17
app/components/Window.tsx CHANGED
@@ -44,11 +44,13 @@ const Window: React.FC<WindowProps> = ({
44
  }) => {
45
  const [isMaximized, setIsMaximized] = React.useState(false);
46
  const [previousSize, setPreviousSize] = React.useState({ width, height, x, y: Math.max(y, 32) });
47
- const [currentPosition, setCurrentPosition] = React.useState({ x, y: Math.max(y, 32) });
48
- const [currentSize, setCurrentSize] = React.useState({ width, height });
49
  const [isMobile, setIsMobile] = React.useState(false);
50
  const [isDraggingOrResizing, setIsDraggingOrResizing] = React.useState(false);
51
 
 
 
 
 
52
  // Detect mobile device
53
  useEffect(() => {
54
  const checkMobile = () => {
@@ -90,10 +92,10 @@ const Window: React.FC<WindowProps> = ({
90
  const handleMaximize = () => {
91
  if (!isMaximized) {
92
  setPreviousSize({
93
- width: currentSize.width,
94
- height: currentSize.height,
95
- x: currentPosition.x,
96
- y: currentPosition.y
97
  });
98
  setIsMaximized(true);
99
  } else {
@@ -118,8 +120,13 @@ const Window: React.FC<WindowProps> = ({
118
  disableDragging: true,
119
  enableResizing: false
120
  } : {
121
- position: currentPosition,
122
- size: currentSize,
 
 
 
 
 
123
  disableDragging: false,
124
  enableResizing: resizable
125
  };
@@ -138,17 +145,21 @@ const Window: React.FC<WindowProps> = ({
138
  bringToFront();
139
  }}
140
  onDrag={(e, d) => {
141
- // Prevent dragging above TopBar during drag
142
  if (d.y < 32) {
 
 
143
  return false;
144
  }
 
 
145
  }}
146
  onDragStop={(e, d) => {
147
  setIsDraggingOrResizing(false);
148
  if (!isMaximized) {
149
- // Ensure window doesn't go above TopBar
150
- const newY = Math.max(d.y, 32);
151
- setCurrentPosition({ x: d.x, y: newY });
152
  }
153
  }}
154
  onResizeStart={() => {
@@ -158,13 +169,12 @@ const Window: React.FC<WindowProps> = ({
158
  onResizeStop={(e, direction, ref, delta, position) => {
159
  setIsDraggingOrResizing(false);
160
  if (!isMaximized) {
161
- setCurrentSize({
 
162
  width: ref.offsetWidth,
163
  height: ref.offsetHeight,
164
- });
165
- // Ensure window doesn't go above TopBar after resize
166
- const newY = Math.max(position.y, 32);
167
- setCurrentPosition({ ...position, y: newY });
168
  }
169
  }}
170
  className="absolute"
 
44
  }) => {
45
  const [isMaximized, setIsMaximized] = React.useState(false);
46
  const [previousSize, setPreviousSize] = React.useState({ width, height, x, y: Math.max(y, 32) });
 
 
47
  const [isMobile, setIsMobile] = React.useState(false);
48
  const [isDraggingOrResizing, setIsDraggingOrResizing] = React.useState(false);
49
 
50
+ // Use refs to track current position/size without causing re-renders
51
+ const currentPositionRef = React.useRef({ x, y: Math.max(y, 32) });
52
+ const currentSizeRef = React.useRef({ width, height });
53
+
54
  // Detect mobile device
55
  useEffect(() => {
56
  const checkMobile = () => {
 
92
  const handleMaximize = () => {
93
  if (!isMaximized) {
94
  setPreviousSize({
95
+ width: currentSizeRef.current.width,
96
+ height: currentSizeRef.current.height,
97
+ x: currentPositionRef.current.x,
98
+ y: currentPositionRef.current.y
99
  });
100
  setIsMaximized(true);
101
  } else {
 
120
  disableDragging: true,
121
  enableResizing: false
122
  } : {
123
+ // Use default for uncontrolled mode - only sets initial position
124
+ default: {
125
+ x: currentPositionRef.current.x,
126
+ y: currentPositionRef.current.y,
127
+ width: currentSizeRef.current.width,
128
+ height: currentSizeRef.current.height
129
+ },
130
  disableDragging: false,
131
  enableResizing: resizable
132
  };
 
145
  bringToFront();
146
  }}
147
  onDrag={(e, d) => {
148
+ // Constrain dragging to not go above TopBar
149
  if (d.y < 32) {
150
+ // Update position ref to boundary
151
+ currentPositionRef.current = { x: d.x, y: 32 };
152
  return false;
153
  }
154
+ // Update position ref during drag
155
+ currentPositionRef.current = { x: d.x, y: d.y };
156
  }}
157
  onDragStop={(e, d) => {
158
  setIsDraggingOrResizing(false);
159
  if (!isMaximized) {
160
+ // Ensure window doesn't go above TopBar and update position ref
161
+ const constrainedY = Math.max(d.y, 32);
162
+ currentPositionRef.current = { x: d.x, y: constrainedY };
163
  }
164
  }}
165
  onResizeStart={() => {
 
169
  onResizeStop={(e, direction, ref, delta, position) => {
170
  setIsDraggingOrResizing(false);
171
  if (!isMaximized) {
172
+ // Update size and position refs
173
+ currentSizeRef.current = {
174
  width: ref.offsetWidth,
175
  height: ref.offsetHeight,
176
+ };
177
+ currentPositionRef.current = position;
 
 
178
  }
179
  }}
180
  className="absolute"