Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions src/dom/__test__/get-positioned-parent-offset.web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,24 @@ describe('getPositionedParentOffset', () => {

expect(getPositionedParentOffset(target)).toEqual({ x: 100, y: 100 });
});

test('should handle out parameter', () => {
const parent = document.createElement('div');
const target = document.createElement('div');

window.scrollTo(10, 20);

parent.style.position = 'relative';
target.style.position = 'absolute';
mock.method(parent, 'getBoundingClientRect', () => new DOMRectReadOnlyMock(10, 20, 30, 40));

document.body.append(parent);
parent.append(target);

const point = { x: 12, y: 34 };
const result = getPositionedParentOffset(target, undefined, point);
expect(result).toBe(point);
expect(result.x).toEqual(10);
expect(result.y).toEqual(20);
});
});
30 changes: 15 additions & 15 deletions src/dom/get-positioned-parent-offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ export interface PositioningOptions {
* Returns position of top left corner of the parent positioned element relative to viewport origin.
* @param element Target element.
* @param options Options.
* @param out Out parameter.
* @returns Offset.
*/
export function getPositionedParentOffset(
element: HTMLElement,
{ strategy = 'absolute' }: PositioningOptions = {},
out: Point2d = { x: 0, y: 0 },
): Point2d {
out.x = 0;
out.y = 0;

if (!element.isConnected) {
return { x: 0, y: 0 };
return out;
}

const offsetParent = element.parentElement
Expand All @@ -28,26 +33,21 @@ export function getPositionedParentOffset(
)
: null;

const offset: Point2d = {
x: 0,
y: 0,
};

if (strategy === 'absolute') {
offset.x = -window.scrollX;
offset.y = -window.scrollY;
out.x = -window.scrollX;
out.y = -window.scrollY;
}

if (offsetParent) {
const parentRect = offsetParent.getBoundingClientRect();
const parentStyle = getComputedStyle(offsetParent);

offset.x = parentRect.left;
offset.y = parentRect.top;
out.x = parentRect.left;
out.y = parentRect.top;

// IMPORTANT: border-top/border-left affects parent positioning origin
offset.x += cssValueToNumber(parentStyle.borderLeftWidth);
offset.y += cssValueToNumber(parentStyle.borderTopWidth);
out.x += cssValueToNumber(parentStyle.borderLeftWidth);
out.y += cssValueToNumber(parentStyle.borderTopWidth);
}

const scrollParent =
Expand All @@ -56,11 +56,11 @@ export function getPositionedParentOffset(

// IMPORTANT: check offsetParent's scrollTop/scrollLeft
if (offsetParent && offsetParent === scrollParent) {
offset.x += scrollParent.scrollLeft;
offset.y += scrollParent.scrollTop;
out.x += scrollParent.scrollLeft;
out.y += scrollParent.scrollTop;
}

return offset;
return out;
}

/**
Expand Down
Loading