Skip to content

Commit

Permalink
Don't use window global
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jun 21, 2024
1 parent 627a0cd commit 285ac09
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/block-editor/src/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ function rectIntersect( rect1, rect2 ) {
* @return {boolean} Whether the element is visible.
*/
function isElementVisible( element ) {
const style = window.getComputedStyle( element );
const viewport = element.ownerDocument.defaultView;
if ( ! viewport ) {
return false;
}

const style = viewport.getComputedStyle( element );
if (
style.display === 'none' ||
style.visibility === 'hidden' ||
Expand Down Expand Up @@ -140,6 +145,11 @@ function isElementVisible( element ) {
* @return {DOMRect} Bounding client rect.
*/
export function getVisibleBoundingRect( element ) {
const viewport = element.ownerDocument.defaultView;
if ( ! viewport ) {
return new window.DOMRect();
}

let bounds = element.getBoundingClientRect();

const stack = [ element ];
Expand All @@ -158,8 +168,8 @@ export function getVisibleBoundingRect( element ) {
const viewportRect = new window.DOMRect(
0,
0,
window.innerWidth,
window.innerHeight
viewport.innerWidth,
viewport.innerHeight
);
return rectIntersect( bounds, viewportRect );
}

0 comments on commit 285ac09

Please sign in to comment.