Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resizer on absolute drag mode when the parent is positioned ( position: relative | absolute | .... ) #6382

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/core/src/utils/Resizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,15 +672,21 @@ export default class Resizer {
const maxDim = opts.maxDim;
const deltaX = data.delta!.x;
const deltaY = data.delta!.y;
const parentEl = this.getParentEl();
const parentRect = parentEl ? this.getElementPos(parentEl) : { top: 0, left: 0, width: 0, height: 0 };
const parentW = this.parentDim!.w;
const parentH = this.parentDim!.h;
const unitWidth = this.opts.unitWidth;
const unitHeight = this.opts.unitHeight;
const startW = unitWidth === '%' ? (startDim.w / 100) * parentW : startDim.w;
const startH = unitHeight === '%' ? (startDim.h / 100) * parentH : startDim.h;

// Check if the parent or any ancestor has `position: relative`, `absolute`, `fixed`, or `sticky`
const hasPositionedParent = parentEl && this.hasPositionedParent(parentEl);

const box: RectDim = {
t: startDim.t,
l: startDim.l,
t: hasPositionedParent ? startDim.t - parentRect.top : startDim.t, // Subtract only if parent or ancestor is positioned
l: hasPositionedParent ? startDim.l - parentRect.left : startDim.l, // Subtract only if parent or ancestor is positioned
w: startW,
h: startH,
};
Expand Down Expand Up @@ -750,4 +756,11 @@ export default class Resizer {

return box;
}

hasPositionedParent(element: HTMLElement | null): boolean {
if (!element) return false;

// If the element's offsetParent is not the body or null, it has a positioned ancestor
return element.offsetParent !== document.body && element.offsetParent !== null;
}
}
Loading