Skip to content

Commit f99aaac

Browse files
committed
fix(core): iframe support in floating dom controller
1 parent 45303f5 commit f99aaac

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

.changeset/stale-moments-drive.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@patternfly/pfe-core": patch
3+
---
4+
5+
`FloatingDOMController`: support usage within iframes
6+

core/pfe-core/controllers/floating-dom-controller.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ function getOverflowAncestors(
839839
): OverflowAncestors {
840840
const scrollableAncestor = getNearestOverflowAncestor(node);
841841
const isBody = scrollableAncestor === node.ownerDocument?.body;
842-
const win = window;
842+
const win = scrollableAncestor.ownerDocument?.defaultView ?? window;
843843

844844
if (isBody) {
845845
const frameElement = getFrameElement(win);
@@ -1269,8 +1269,8 @@ function getScale(element: Element): Coords {
12691269
* @param element - The element to get visual offsets for
12701270
* @returns Coordinates object with x and y offsets
12711271
*/
1272-
function getVisualOffsets(): Coords {
1273-
const win = window;
1272+
function getVisualOffsets(element: Element): Coords {
1273+
const win = element.ownerDocument?.defaultView ?? window;
12741274
if (!isWebKit() || !win.visualViewport) {
12751275
return noOffsets;
12761276
}
@@ -1323,16 +1323,17 @@ function getBoundingClientRect(
13231323
}
13241324
}
13251325
const visualOffsets = shouldAddVisualOffsets(isFixedStrategy, offsetParent) ?
1326-
getVisualOffsets()
1326+
getVisualOffsets(element)
13271327
: createCoords(0);
13281328
let x = (clientRect.left + visualOffsets.x) / scale.x;
13291329
let y = (clientRect.top + visualOffsets.y) / scale.y;
13301330
let width = clientRect.width / scale.x;
13311331
let height = clientRect.height / scale.y;
13321332
if (element) {
1333-
const win = window;
1334-
const offsetWin = offsetParent
1335-
&& isElement(offsetParent) ? window : offsetParent;
1333+
const win = element.ownerDocument?.defaultView ?? window;
1334+
const offsetWin = offsetParent && isElement(offsetParent) ?
1335+
offsetParent.ownerDocument?.defaultView ?? window
1336+
: offsetParent;
13361337
let currentWin = win;
13371338
let currentIFrame = getFrameElement(currentWin);
13381339
while (currentIFrame && offsetParent && offsetWin !== currentWin) {
@@ -1349,7 +1350,7 @@ function getBoundingClientRect(
13491350
height *= iframeScale.y;
13501351
x += left;
13511352
y += top;
1352-
currentWin = window;
1353+
currentWin = currentIFrame.ownerDocument?.defaultView ?? window;
13531354
currentIFrame = getFrameElement(currentWin);
13541355
}
13551356
}
@@ -1487,7 +1488,7 @@ function getDocumentRect(element: Element): Rect {
14871488
* @returns Rect object with viewport dimensions and position
14881489
*/
14891490
function getViewportRect(element: Element, strategy: Strategy): Rect {
1490-
const win = window;
1491+
const win = element.ownerDocument?.defaultView ?? window;
14911492
const html = getDocumentElement(element)!;
14921493
const { visualViewport } = win;
14931494
const width = visualViewport ? visualViewport.width : html.clientWidth;
@@ -1552,7 +1553,7 @@ function getClientRectFromClippingAncestor(
15521553
} else if (isElement(clippingAncestor)) {
15531554
rect = getInnerBoundingClientRect(clippingAncestor, strategy);
15541555
} else {
1555-
const visualOffsets = getVisualOffsets();
1556+
const visualOffsets = getVisualOffsets(element);
15561557
rect = {
15571558
x: clippingAncestor.x - visualOffsets.x,
15581559
y: clippingAncestor.y - visualOffsets.y,
@@ -1768,7 +1769,7 @@ function getTrueOffsetParent(element: Element, polyfill?: (element: Element) =>
17681769
*/
17691770
function getOffsetParent(element: Element, polyfill?: (element: Element) =>
17701771
Element | null): Element | Window {
1771-
const win = window;
1772+
const win = element.ownerDocument?.defaultView ?? window;
17721773
if (isTopLayer(element)) {
17731774
return win;
17741775
}

0 commit comments

Comments
 (0)