Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,40 @@ describe('FlexibleConnectedPositionStrategy', () => {
expect(Math.floor(overlayRect.top)).toBe(viewportMargin);
});

it('should center flexible overlay with push on a scrolled page', () => {
const veryLargeElement = document.createElement('div');

originElement.style.left = '200px';
originElement.style.top = '200px';

veryLargeElement.style.width = '100%';
veryLargeElement.style.height = '2000px';
document.body.appendChild(veryLargeElement);
window.scroll(0, 250);

positionStrategy
.withFlexibleDimensions()
.withPush(true)
.withPositions([{
overlayY: 'top',
overlayX: 'center',
originY: 'bottom',
originX: 'center'
}]);

attachOverlay({positionStrategy});

const overlayRect = overlayRef.overlayElement.getBoundingClientRect();
const originRect = originElement.getBoundingClientRect();

expect(Math.floor(overlayRect.left - overlayRect.width / 2))
.toBe(Math.floor(originRect.left - originRect.width / 2));

window.scroll(0, 0);
document.body.removeChild(veryLargeElement);
});


});

describe('onPositionChange with scrollable view properties', () => {
Expand Down
17 changes: 11 additions & 6 deletions src/cdk/overlay/position/flexible-connected-position-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,13 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
bottom = viewport.height - origin.y + this._viewportMargin * 2;
height = viewport.height - bottom + this._viewportMargin;
} else {
// If neither top nor bottom, it means that the overlay
// is vertically centered on the origin point.
// If neither top nor bottom, it means that the overlay is vertically centered on the
// origin point. Note that we want the position relative to the viewport, rather than
// the page, which is why we don't use something like `viewport.bottom - origin.y` and
// `origin.y - viewport.top`.
const smallestDistanceToViewportEdge =
Math.min(viewport.bottom - origin.y, origin.y - viewport.left);
Math.min(viewport.bottom - origin.y + viewport.top, origin.y);

const previousHeight = this._lastBoundingBoxSize.height;

height = smallestDistanceToViewportEdge * 2;
Expand Down Expand Up @@ -720,10 +723,12 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
left = origin.x;
width = viewport.right - origin.x;
} else {
// If neither start nor end, it means that the overlay
// is horizontally centered on the origin point.
// If neither start nor end, it means that the overlay is horizontally centered on the
// origin point. Note that we want the position relative to the viewport, rather than
// the page, which is why we don't use something like `viewport.right - origin.x` and
// `origin.x - viewport.left`.
const smallestDistanceToViewportEdge =
Math.min(viewport.right - origin.x, origin.x - viewport.top);
Math.min(viewport.right - origin.x + viewport.left, origin.x);
const previousWidth = this._lastBoundingBoxSize.width;

width = smallestDistanceToViewportEdge * 2;
Expand Down