Skip to content

Commit a167b86

Browse files
chore: improve calculations
1 parent ef6b0cd commit a167b86

1 file changed

Lines changed: 69 additions & 25 deletions

File tree

packages/main/src/Popover.ts

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class Popover extends Popup {
241241
_initialLeft?: number;
242242
_minWidth?: number;
243243
_cachedMinHeight?: number;
244-
_draggedOrResized = false;
244+
_resized = false;
245245

246246
_resizeHandlePlacement?: `${ResizeHandlePlacement}`;
247247

@@ -1010,6 +1010,7 @@ class Popover extends Popup {
10101010
width,
10111011
height,
10121012
minWidth,
1013+
minHeight,
10131014
} = window.getComputedStyle(this);
10141015

10151016
this._initialX = e.clientX;
@@ -1019,7 +1020,7 @@ class Popover extends Popup {
10191020
this._initialTop = top;
10201021
this._initialLeft = left;
10211022
this._minWidth = Number.parseFloat(minWidth);
1022-
// this._cachedMinHeight = this._minHeight;
1023+
this._cachedMinHeight = Number.parseFloat(minHeight);
10231024

10241025
this._resizeHandlePlacement = this._getResizeHandlePlacement();
10251026

@@ -1028,54 +1029,97 @@ class Popover extends Popup {
10281029
left: `${left}px`,
10291030
});
10301031

1031-
this._draggedOrResized = true;
1032+
this._resized = true;
10321033
this._attachMouseResizeHandlers();
10331034
}
10341035

10351036
_onResizeMouseMove(e: MouseEvent) {
10361037
const { clientX, clientY } = e;
1038+
const placement = this._resizeHandlePlacement;
1039+
const margin = Popover.VIEWPORT_MARGIN;
10371040

10381041
let newWidth,
1039-
newLeft;
1042+
newHeight,
1043+
newLeft,
1044+
newTop;
1045+
1046+
// Determine if we're resizing from left or right edge
1047+
const isResizingFromLeft = placement === ResizeHandlePlacement.TopLeft
1048+
|| placement === ResizeHandlePlacement.BottomLeft;
1049+
const isResizingFromTop = placement === ResizeHandlePlacement.TopLeft
1050+
|| placement === ResizeHandlePlacement.TopRight;
1051+
1052+
// Calculate width changes
1053+
if (isResizingFromLeft) {
1054+
// Resizing from left edge - width increases when moving left (negative delta)
1055+
const deltaX = clientX - this._initialX!;
1056+
const maxWidthFromLeft = this._initialLeft! + this._initialWidth! - margin;
10401057

1041-
if (this._isRTL) {
10421058
newWidth = clamp(
1043-
this._initialWidth! - (clientX - this._initialX!),
1059+
this._initialWidth! - deltaX,
10441060
this._minWidth!,
1045-
this._initialLeft! + this._initialWidth!,
1061+
maxWidthFromLeft,
10461062
);
10471063

1048-
// check if width is changed to avoid "left" jumping when max width is reached
1049-
Object.assign(this.style, {
1050-
width: `${newWidth}px`,
1051-
});
1052-
1053-
const deltaWidth = newWidth - this.getBoundingClientRect().width;
1054-
const rightEdge = this._initialLeft! + this._initialWidth! + deltaWidth;
1055-
1064+
// Adjust left position when resizing from left
1065+
// Ensure the left edge respects the viewport margin and the right edge position
10561066
newLeft = clamp(
1057-
rightEdge - newWidth,
1058-
0,
1059-
rightEdge - this._minWidth!,
1067+
this._initialLeft! + deltaX,
1068+
margin,
1069+
this._initialLeft! + this._initialWidth! - this._minWidth!,
10601070
);
1071+
1072+
// Recalculate width based on actual left position to stay within viewport with margin
1073+
newWidth = Math.min(newWidth, this._initialLeft! + this._initialWidth! - newLeft);
10611074
} else {
1075+
// Resizing from right edge - width increases when moving right (positive delta)
1076+
const maxWidthFromRight = window.innerWidth - this._initialLeft! - margin;
1077+
10621078
newWidth = clamp(
10631079
this._initialWidth! + (clientX - this._initialX!),
10641080
this._minWidth!,
1065-
window.innerWidth - this._initialLeft!,
1081+
maxWidthFromRight,
10661082
);
10671083
}
10681084

1069-
const newHeight = clamp(
1070-
this._initialHeight! + (clientY - this._initialY!),
1071-
this._cachedMinHeight!,
1072-
window.innerHeight - this._initialTop!,
1073-
);
1085+
// Calculate height changes
1086+
if (isResizingFromTop) {
1087+
// Resizing from top edge - height increases when moving up (negative delta)
1088+
const deltaY = clientY - this._initialY!;
1089+
const maxHeightFromTop = this._initialTop! + this._initialHeight! - margin;
1090+
1091+
newHeight = clamp(
1092+
this._initialHeight! - deltaY,
1093+
this._cachedMinHeight!,
1094+
maxHeightFromTop,
1095+
);
1096+
1097+
// Adjust top position when resizing from top
1098+
// Ensure the top edge respects the viewport margin and the bottom edge position
1099+
newTop = clamp(
1100+
this._initialTop! + deltaY,
1101+
margin,
1102+
this._initialTop! + this._initialHeight! - this._cachedMinHeight!,
1103+
);
1104+
1105+
// Recalculate height based on actual top position to stay within viewport with margin
1106+
newHeight = Math.min(newHeight, this._initialTop! + this._initialHeight! - newTop);
1107+
} else {
1108+
// Resizing from bottom edge - height increases when moving down (positive delta)
1109+
const maxHeightFromBottom = window.innerHeight - this._initialTop! - margin;
1110+
1111+
newHeight = clamp(
1112+
this._initialHeight! + (clientY - this._initialY!),
1113+
this._cachedMinHeight!,
1114+
maxHeightFromBottom,
1115+
);
1116+
}
10741117

10751118
Object.assign(this.style, {
10761119
height: `${newHeight}px`,
10771120
width: `${newWidth}px`,
1078-
left: this._isRTL ? `${newLeft}px` : undefined,
1121+
left: newLeft !== undefined ? `${newLeft}px` : undefined,
1122+
top: newTop !== undefined ? `${newTop}px` : undefined,
10791123
});
10801124
}
10811125

0 commit comments

Comments
 (0)