Skip to content

Commit a9bffdb

Browse files
committed
Constrain draggable panel position within viewport boundaries
1 parent 14e02f4 commit a9bffdb

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

floating-panel-ui-creation.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,30 @@ window.MaxExtensionFloatingPanel.makeDraggable = function (element, handle) {
151151

152152
const dragElement = (e) => {
153153
e.preventDefault();
154-
element.style.left = (e.clientX - offsetX) + 'px';
155-
element.style.top = (e.clientY - offsetY) + 'px';
154+
155+
// Calculate the new position
156+
let newLeft = e.clientX - offsetX;
157+
let newTop = e.clientY - offsetY;
158+
159+
// Get viewport dimensions
160+
const viewportWidth = window.innerWidth;
161+
const viewportHeight = window.innerHeight;
162+
163+
// Get panel dimensions
164+
const panelWidth = element.offsetWidth;
165+
const panelHeight = element.offsetHeight;
166+
167+
// Constrain the horizontal position (left)
168+
newLeft = Math.max(0, newLeft);
169+
newLeft = Math.min(newLeft, viewportWidth - panelWidth);
170+
171+
// Constrain the vertical position (top)
172+
newTop = Math.max(0, newTop);
173+
newTop = Math.min(newTop, viewportHeight - panelHeight);
174+
175+
// Apply the constrained position
176+
element.style.left = newLeft + 'px';
177+
element.style.top = newTop + 'px';
156178
};
157179

158180
const stopDrag = () => {

0 commit comments

Comments
 (0)