Skip to content

Commit

Permalink
No need to clip bounds to viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks authored and ramonjd committed Aug 22, 2024
1 parent 4405424 commit 0ed777f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
8 changes: 4 additions & 4 deletions packages/block-editor/src/components/block-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
*/
import { useBlockElement } from '../block-list/use-block-props/use-block-refs';
import usePopoverScroll from './use-popover-scroll';
import { rectUnion, getVisibleBoundingRect } from '../../utils/dom';
import { rectUnion, getVisibleElementBounds } from '../../utils/dom';

const MAX_POPOVER_RECOMPUTE_COUNTER = Number.MAX_SAFE_INTEGER;

Expand Down Expand Up @@ -90,10 +90,10 @@ function BlockPopover(
getBoundingClientRect() {
return lastSelectedElement
? rectUnion(
getVisibleBoundingRect( selectedElement ),
getVisibleBoundingRect( lastSelectedElement )
getVisibleElementBounds( selectedElement ),
getVisibleElementBounds( lastSelectedElement )
)
: getVisibleBoundingRect( selectedElement );
: getVisibleElementBounds( selectedElement );
},
contextElement: selectedElement,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { store as blockEditorStore } from '../../store';
import { useBlockElement } from '../block-list/use-block-props/use-block-refs';
import { hasStickyOrFixedPositionValue } from '../../hooks/position';
import { getVisibleBoundingRect } from '../../utils/dom';
import { getVisibleElementBounds } from '../../utils/dom';

const COMMON_PROPS = {
placement: 'top-start',
Expand Down Expand Up @@ -68,7 +68,7 @@ function getProps(
// Get how far the content area has been scrolled.
const scrollTop = scrollContainer?.scrollTop || 0;

const blockRect = getVisibleBoundingRect( selectedBlockElement );
const blockRect = getVisibleElementBounds( selectedBlockElement );
const contentRect = contentElement.getBoundingClientRect();

// Get the vertical position of top of the visible content area.
Expand Down
35 changes: 7 additions & 28 deletions packages/block-editor/src/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,6 @@ export function rectUnion( rect1, rect2 ) {
return new window.DOMRect( left, top, right - left, bottom - top );
}

/**
* Returns the intersection of two DOMRect objects.
*
* @param {DOMRect} rect1 First rectangle.
* @param {DOMRect} rect2 Second rectangle.
* @return {DOMRect} Intersection of the two rectangles.
*/
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 );
return new window.DOMRect( left, top, right - left, bottom - top );
}

/**
* Returns whether an element is visible.
*
Expand Down Expand Up @@ -129,22 +114,22 @@ function isElementVisible( element ) {
}

/**
* Returns the rect of the element that is visible in the viewport.
* Returns the rect of the element including all visible nested elements.
*
* Visible nested elements, including elements that overflow the parent, are
* taken into account. The returned rect is clipped to the viewport.
* taken into account.
*
* This function is useful for calculating the visible area of a block that
* contains nested elements that overflow the block, e.g. the Navigation block,
* which can contain overflowing Submenu blocks.
*
* The returned rect is suitable for passing to the Popover component to
* position the popover relative to the visible area of the block.
* The returned rect represents the full extent of the element and its visible
* children, which may extend beyond the viewport.
*
* @param {Element} element Element.
* @return {DOMRect} Bounding client rect.
* @return {DOMRect} Bounding client rect of the element and its visible children.
*/
export function getVisibleBoundingRect( element ) {
export function getVisibleElementBounds( element ) {
const viewport = element.ownerDocument.defaultView;
if ( ! viewport ) {
return new window.DOMRect();
Expand All @@ -165,11 +150,5 @@ export function getVisibleBoundingRect( element ) {
}
}

const viewportRect = new window.DOMRect(
0,
0,
viewport.innerWidth,
viewport.innerHeight
);
return rectIntersect( bounds, viewportRect );
return bounds;
}

0 comments on commit 0ed777f

Please sign in to comment.