Skip to content

Commit

Permalink
Popover: allow scrolling over
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jul 6, 2020
1 parent 71c07ea commit 96c0434
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ export default function InsertionPoint( {
focusOnMount={ false }
className="block-editor-block-list__insertion-point-popover"
__unstableSlotName="block-toolbar"
__unstableFixedPosition={ false }
>
<div
className="block-editor-block-list__insertion-point"
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/block-list/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@

.block-editor-block-list__insertion-point-popover.is-without-arrow {
z-index: z-index(".block-editor-block-list__insertion-point-popover");
position: absolute;

.components-popover__content.components-popover__content { // Needs specificity.
background: none;
Expand All @@ -681,6 +682,7 @@

.components-popover.block-editor-block-list__block-popover {
z-index: z-index(".block-editor-block-list__block-popover");
position: absolute;

.components-popover__content {
margin: 0 !important;
Expand Down
22 changes: 10 additions & 12 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ function computeAnchorRect(
}

if ( anchorRef !== false ) {
if ( ! anchorRef ) {
if (
! anchorRef ||
! window.Range ||
! window.Element ||
! window.DOMRect
) {
return;
}

Expand Down Expand Up @@ -256,7 +261,6 @@ const Popover = ( {
__unstableSticky,
__unstableSlotName = SLOT_NAME,
__unstableObserveElement,
__unstableFixedPosition = true,
__unstableBoundaryParent,
/* eslint-enable no-unused-vars */
...contentProps
Expand All @@ -281,7 +285,6 @@ const Popover = ( {
setStyle( containerRef.current, 'left' );
setStyle( contentRef.current, 'maxHeight' );
setStyle( contentRef.current, 'maxWidth' );
setStyle( containerRef.current, 'position' );
return;
}

Expand All @@ -302,17 +305,16 @@ const Popover = ( {
return;
}

const { offsetParent, ownerDocument } = containerRef.current;

let relativeOffsetTop = 0;

// If there is a positioned ancestor element that is not the body,
// subtract the position from the anchor rect. If the position of
// the popover is fixed, the offset parent is null or the body
// element, in which case the position is relative to the viewport.
// See https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent
if ( ! __unstableFixedPosition ) {
setStyle( containerRef.current, 'position', 'absolute' );

const { offsetParent } = containerRef.current;
if ( offsetParent && offsetParent !== ownerDocument.body ) {
const offsetParentRect = offsetParent.getBoundingClientRect();

relativeOffsetTop = offsetParentRect.top;
Expand All @@ -322,8 +324,6 @@ const Popover = ( {
anchor.width,
anchor.height
);
} else {
setStyle( containerRef.current, 'position' );
}

let boundaryElement;
Expand Down Expand Up @@ -396,8 +396,7 @@ const Popover = ( {
setAnimateOrigin( animateXAxis + ' ' + animateYAxis );
};

// Height may still adjust between now and the next tick.
const timeoutId = window.setTimeout( refresh );
refresh();

/*
* There are sometimes we need to reposition or resize the popover that
Expand Down Expand Up @@ -430,7 +429,6 @@ const Popover = ( {
}

return () => {
window.clearTimeout( timeoutId );
window.clearInterval( intervalHandle );
window.removeEventListener( 'resize', refresh );
window.removeEventListener( 'scroll', refresh, true );
Expand Down
10 changes: 8 additions & 2 deletions packages/components/src/popover/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ exports[`Popover should pass additional props to portaled element 1`] = `
<div>
<div>
<div
class="components-popover is-without-arrow"
class="components-popover components-animate__appear is-from-top is-from-left is-without-arrow"
data-x-axis="right"
data-y-axis="bottom"
role="tooltip"
style="top: 0px; left: 0px;"
>
<div
class="components-popover__content"
Expand All @@ -36,7 +39,10 @@ exports[`Popover should render content 1`] = `
<div>
<div>
<div
class="components-popover is-without-arrow"
class="components-popover components-animate__appear is-from-top is-from-left is-without-arrow"
data-x-axis="right"
data-y-axis="bottom"
style="top: 0px; left: 0px;"
>
<div
class="components-popover__content"
Expand Down
8 changes: 3 additions & 5 deletions packages/components/src/popover/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,12 @@ export function computePopoverYAxisPosition(
const scrollContainerEl =
getScrollContainer( anchorRef ) || document.body;
const scrollRect = scrollContainerEl.getBoundingClientRect();
const stickyPosition = scrollRect.top + height - relativeOffsetTop;

if ( anchorRect.top - height <= scrollRect.top ) {
if ( anchorRect.top <= stickyPosition ) {
return {
yAxis,
popoverTop: Math.min(
anchorRect.bottom - relativeOffsetTop,
scrollRect.top + height - relativeOffsetTop
),
popoverTop: Math.min( anchorRect.bottom, stickyPosition ),
};
}
}
Expand Down

0 comments on commit 96c0434

Please sign in to comment.