Skip to content

Commit

Permalink
Check for parent overflow only once.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Oct 18, 2024
1 parent 9cecad1 commit 4403bb9
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions packages/block-editor/src/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,37 @@ export function getVisibleElementBounds( element ) {
return new window.DOMRectReadOnly();
}

let bounds = element.getBoundingClientRect();
const parentRect = element.getBoundingClientRect();
let bounds = parentRect;

const stack = [ element ];
const xValues = [];
const yValues = [];
let currentElement;

while ( ( currentElement = stack.pop() ) ) {
for ( const child of currentElement.children ) {
if ( isElementVisible( child ) ) {
const childBounds = child.getBoundingClientRect();
// If the child is larger than the parent, adjust the x and y values to account for any scrolling.
if ( childBounds.width > bounds.width ) {
childBounds.x = bounds.x;
yValues.push( childBounds.y );
xValues.push( childBounds.x );
/*
* If the child is larger than the parent, adjust the x and y values to the greatest known
* to account for any scrolling.
*/
if ( childBounds.width > parentRect.width ) {
childBounds.x = Math.max(
parentRect.x,
xValues.sort().at( -1 ) || 0
);
}
if ( childBounds.height > bounds.height ) {
childBounds.y = bounds.y;
childBounds.height = bounds.height;
if ( childBounds.height > parentRect.height ) {
childBounds.y = Math.max(
parentRect.y,
yValues.sort().at( -1 ) || 0
);
}

bounds = rectUnion( bounds, childBounds );
stack.push( child );
}
Expand All @@ -173,12 +187,12 @@ export function getVisibleElementBounds( element ) {
*/
const left = Math.max( bounds.left, 0 );
const right = Math.min( bounds.right, viewport.innerWidth );

bounds = new window.DOMRectReadOnly(
left,
bounds.top,
right - left,
bounds.height
);

return bounds;
}

0 comments on commit 4403bb9

Please sign in to comment.