From 65e6363e82372a0edee32b000a2db882a8127b0d Mon Sep 17 00:00:00 2001
From: Mitchell Austin <mr.fye@oneandthesame.net>
Date: Fri, 1 Nov 2024 10:22:42 -0700
Subject: [PATCH] Optimize `getVisibleElementBounds` in scrollable cases
 (#66546)

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
---
 packages/block-editor/src/utils/dom.js | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/packages/block-editor/src/utils/dom.js b/packages/block-editor/src/utils/dom.js
index 0603e9bbb1db9..e30f809e38779 100644
--- a/packages/block-editor/src/utils/dom.js
+++ b/packages/block-editor/src/utils/dom.js
@@ -162,15 +162,14 @@ export function getVisibleElementBounds( element ) {
 	let currentElement;
 
 	while ( ( currentElement = stack.pop() ) ) {
-		for ( const child of currentElement.children ) {
-			if ( isElementVisible( child ) ) {
-				let childBounds = child.getBoundingClientRect();
-				// If the parent is scrollable, use parent's scrollable bounds.
-				if ( isScrollable( currentElement ) ) {
-					childBounds = currentElement.getBoundingClientRect();
+		// Children won’t affect bounds unless the element is not scrollable.
+		if ( ! isScrollable( currentElement ) ) {
+			for ( const child of currentElement.children ) {
+				if ( isElementVisible( child ) ) {
+					const childBounds = child.getBoundingClientRect();
+					bounds = rectUnion( bounds, childBounds );
+					stack.push( child );
 				}
-				bounds = rectUnion( bounds, childBounds );
-				stack.push( child );
 			}
 		}
 	}