Skip to content

Commit

Permalink
Popover: improve iframe offset computation (#42417)
Browse files Browse the repository at this point in the history
* Fix popover offset in site editor

* Fix offset for left/right placements

* Add explanatory comment

* Update comment

* Remove accidentally committed code

* Update changelog

* Remove empty line

* Update packages/components/CHANGELOG.md

* Reduce to using only a single offset middleware and document offset prop

Co-authored-by: Marco Ciampini <marco.ciampo@gmail.com>
  • Loading branch information
talldan and ciampo authored Jul 29, 2022
1 parent e3136a4 commit 8f0c091
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
5 changes: 2 additions & 3 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `ColorPalette`: Fix background image in RTL mode ([#42510](https://github.com/WordPress/gutenberg/pull/42510)).
- `RangeControl`: clamp initialPosition between min and max values ([#42571](https://github.com/WordPress/gutenberg/pull/42571)).
- `Tooltip`: avoid unnecessary re-renders of select child elements ([#42483](https://github.com/WordPress/gutenberg/pull/42483)).
- `Popover`: Fix offset when the reference element is within an iframe. ([#42417](https://github.com/WordPress/gutenberg/pull/42417)).

### Enhancements

Expand Down Expand Up @@ -57,12 +58,10 @@
- `Popover`: call `getAnchorRect` callback prop even if `anchorRefFallback` has no value. ([#42329](https://github.com/WordPress/gutenberg/pull/42329)).
- Fix `ToolTip` position to ensure it is always positioned relative to the first child of the ToolTip. ([#41268](https://github.com/WordPress/gutenberg/pull/41268))

### Enhancement

- `ToggleGroupControl`: Add large size variant ([#42008](https://github.com/WordPress/gutenberg/pull/42008/)).

### Enhancements

- `ToggleGroupControl`: Add large size variant ([#42008](https://github.com/WordPress/gutenberg/pull/42008/)).
- `InputControl`: Ensure that the padding between a `prefix`/`suffix` and the text input stays at a reasonable 8px, even in larger size variants ([#42166](https://github.com/WordPress/gutenberg/pull/42166)).

### Internal
Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/popover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ Each of these base placements has an alignment in the form -start and -end. For
- Required: No
- Default: `"bottom-start"`

### offset

The distance (in pixels) between the anchor and popover.

- Type: `Number`
- Required: No

### children

The content to be displayed within the popover.
Expand Down
44 changes: 33 additions & 11 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,42 @@ const Popover = (
}

const iframeRect = frameElement.getBoundingClientRect();
return {
name: 'iframeOffset',
fn( { x, y } ) {
return {
x: x + iframeRect.left,
y: y + iframeRect.top,
};
},
};
return { x: iframeRect.left, y: iframeRect.top };
}, [ ownerDocument ] );

const middlewares = [
frameOffset,
offset ? offsetMiddleware( offset ) : undefined,
frameOffset || offset
? offsetMiddleware( ( { placement: currentPlacement } ) => {
if ( ! frameOffset ) {
return offset;
}

const isTopBottomPlacement =
currentPlacement.includes( 'top' ) ||
currentPlacement.includes( 'bottom' );

// The main axis should represent the gap between the
// floating element and the reference element. The cross
// axis is always perpendicular to the main axis.
const mainAxis = isTopBottomPlacement ? 'y' : 'x';
const crossAxis = mainAxis === 'x' ? 'y' : 'x';

// When the popover is before the reference, subtract the offset,
// of the main axis else add it.
const hasBeforePlacement =
currentPlacement.includes( 'top' ) ||
currentPlacement.includes( 'left' );
const mainAxisModifier = hasBeforePlacement ? -1 : 1;
const normalizedOffset = offset ? offset : 0;

return {
mainAxis:
normalizedOffset +
frameOffset[ mainAxis ] * mainAxisModifier,
crossAxis: frameOffset[ crossAxis ],
};
} )
: undefined,
__unstableForcePosition ? undefined : flip(),
__unstableForcePosition
? undefined
Expand Down

0 comments on commit 8f0c091

Please sign in to comment.