Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Adjust left and right manually at end of getVisibleElementBounds
  • Loading branch information
noisysocks committed Sep 3, 2024
commit d0172b6e93bc872ffb0dac3b00bcb9dda87efd53
34 changes: 9 additions & 25 deletions packages/block-editor/src/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,7 @@ export function rectUnion( rect1, rect2 ) {
const bottom = Math.max( rect1.bottom, rect2.bottom );
const top = Math.min( rect1.top, rect2.top );

return new window.DOMRect( left, top, right - left, bottom - top );
}

/**
* Calculates the intersection of two rectangles. If there is no intersection,
* it returns a DOMRect with zero width and height.
*
* @param {DOMRect} rect1 First rectangle.
* @param {DOMRect} rect2 Second rectangle.
* @return {DOMRect} Intersection of the two rectangles.
*/
export function rectIntersect( rect1, rect2 ) {
const left = Math.max( rect1.left, rect2.left );
const top = Math.max( rect1.top, rect2.top );
const right = Math.min( rect1.right, rect2.right );
const bottom = Math.min( rect1.bottom, rect2.bottom );

const width = Math.max( 0, right - left );
const height = Math.max( 0, bottom - top );

return new window.DOMRect( left, top, width, height );
return new window.DOMRectReadOnly( left, top, right - left, bottom - top );
}

/**
Expand Down Expand Up @@ -142,7 +122,7 @@ function isElementVisible( element ) {
export function getVisibleElementBounds( element ) {
const viewport = element.ownerDocument.defaultView;
if ( ! viewport ) {
return new window.DOMRect();
return new window.DOMRectReadOnly();
}

let bounds = element.getBoundingClientRect();
Expand All @@ -168,9 +148,13 @@ export function getVisibleElementBounds( element ) {
* not to be counted in the visibility calculations. Top and bottom values
* are not accounted for to accommodate vertical scroll.
*/
bounds = rectIntersect(
bounds,
new window.DOMRect( 0, -Infinity, viewport.innerWidth, Infinity )
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;
Expand Down