|
8 | 8 |
|
9 | 9 | import {PositionStrategy} from './position-strategy';
|
10 | 10 | import {ElementRef} from '@angular/core';
|
11 |
| -import {ViewportRuler, CdkScrollable} from '@angular/cdk/scrolling'; |
| 11 | +import {ViewportRuler, CdkScrollable, ViewportScrollPosition} from '@angular/cdk/scrolling'; |
12 | 12 | import {
|
13 | 13 | ConnectedOverlayPositionChange,
|
14 | 14 | ConnectionPositionPair,
|
@@ -112,6 +112,9 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
|
112 | 112 | /** Amount of subscribers to the `positionChanges` stream. */
|
113 | 113 | private _positionChangeSubscriptions = 0;
|
114 | 114 |
|
| 115 | + /** Amount by which the overlay was pushed in each axis during the last time it was positioned. */ |
| 116 | + private _previousPushAmount: {x: number, y: number} | null; |
| 117 | + |
115 | 118 | /** Observable sequence of position changes. */
|
116 | 119 | positionChanges: Observable<ConnectedOverlayPositionChange> = Observable.create(observer => {
|
117 | 120 | const subscription = this._positionChanges.subscribe(observer);
|
@@ -152,7 +155,13 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
|
152 | 155 | this._boundingBox = overlayRef.hostElement;
|
153 | 156 | this._pane = overlayRef.overlayElement;
|
154 | 157 | this._resizeSubscription.unsubscribe();
|
155 |
| - this._resizeSubscription = this._viewportRuler.change().subscribe(() => this.apply()); |
| 158 | + this._resizeSubscription = this._viewportRuler.change().subscribe(() => { |
| 159 | + // When the window is resized, we want to trigger the next reposition as if it |
| 160 | + // was an initial render, in order for the strategy to pick a new optimal position, |
| 161 | + // otherwise position locking will cause it to stay at the old one. |
| 162 | + this._isInitialRender = true; |
| 163 | + this.apply(); |
| 164 | + }); |
156 | 165 | }
|
157 | 166 |
|
158 | 167 | /**
|
@@ -282,6 +291,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
|
282 | 291 | }
|
283 | 292 |
|
284 | 293 | detach() {
|
| 294 | + this._lastPosition = null; |
| 295 | + this._previousPushAmount = null; |
285 | 296 | this._resizeSubscription.unsubscribe();
|
286 | 297 | }
|
287 | 298 |
|
@@ -541,39 +552,54 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
|
541 | 552 | * the viewport, the top-left corner will be pushed on-screen (with overflow occuring on the
|
542 | 553 | * right and bottom).
|
543 | 554 | *
|
544 |
| - * @param start The starting point from which the overlay is pushed. |
545 |
| - * @param overlay The overlay dimensions. |
| 555 | + * @param start Starting point from which the overlay is pushed. |
| 556 | + * @param overlay Dimensions of the overlay. |
| 557 | + * @param scrollPosition Current viewport scroll position. |
546 | 558 | * @returns The point at which to position the overlay after pushing. This is effectively a new
|
547 | 559 | * originPoint.
|
548 | 560 | */
|
549 |
| - private _pushOverlayOnScreen(start: Point, overlay: ClientRect): Point { |
| 561 | + private _pushOverlayOnScreen(start: Point, |
| 562 | + overlay: ClientRect, |
| 563 | + scrollPosition: ViewportScrollPosition): Point { |
| 564 | + // If the position is locked and we've pushed the overlay already, reuse the previous push |
| 565 | + // amount, rather than pushing it again. If we were to continue pushing, the element would |
| 566 | + // remain in the viewport, which goes against the expectations when position locking is enabled. |
| 567 | + if (this._previousPushAmount && this._positionLocked) { |
| 568 | + return { |
| 569 | + x: start.x + this._previousPushAmount.x, |
| 570 | + y: start.y + this._previousPushAmount.y |
| 571 | + }; |
| 572 | + } |
550 | 573 | const viewport = this._viewportRect;
|
551 | 574 |
|
552 |
| - // Determine how much the overlay goes outside the viewport on each side, which we'll use to |
553 |
| - // decide which direction to push it. |
| 575 | + // Determine how much the overlay goes outside the viewport on each |
| 576 | + // side, which we'll use to decide which direction to push it. |
554 | 577 | const overflowRight = Math.max(start.x + overlay.width - viewport.right, 0);
|
555 | 578 | const overflowBottom = Math.max(start.y + overlay.height - viewport.bottom, 0);
|
556 |
| - const overflowTop = Math.max(viewport.top - start.y, 0); |
557 |
| - const overflowLeft = Math.max(viewport.left - start.x, 0); |
| 579 | + const overflowTop = Math.max(viewport.top - scrollPosition.top - start.y, 0); |
| 580 | + const overflowLeft = Math.max(viewport.left - scrollPosition.left - start.x, 0); |
558 | 581 |
|
559 |
| - // Amount by which to push the overlay in each direction such that it remains on-screen. |
560 |
| - let pushX, pushY = 0; |
| 582 | + // Amount by which to push the overlay in each axis such that it remains on-screen. |
| 583 | + let pushX = 0; |
| 584 | + let pushY = 0; |
561 | 585 |
|
562 | 586 | // If the overlay fits completely within the bounds of the viewport, push it from whichever
|
563 | 587 | // direction is goes off-screen. Otherwise, push the top-left corner such that its in the
|
564 | 588 | // viewport and allow for the trailing end of the overlay to go out of bounds.
|
565 |
| - if (overlay.width <= viewport.width) { |
| 589 | + if (overlay.width < viewport.width) { |
566 | 590 | pushX = overflowLeft || -overflowRight;
|
567 | 591 | } else {
|
568 |
| - pushX = viewport.left - start.x; |
| 592 | + pushX = start.x < this._viewportMargin ? (viewport.left - scrollPosition.left) - start.x : 0; |
569 | 593 | }
|
570 | 594 |
|
571 |
| - if (overlay.height <= viewport.height) { |
| 595 | + if (overlay.height < viewport.height) { |
572 | 596 | pushY = overflowTop || -overflowBottom;
|
573 | 597 | } else {
|
574 |
| - pushY = viewport.top - start.y; |
| 598 | + pushY = start.y < this._viewportMargin ? (viewport.top - scrollPosition.top) - start.y : 0; |
575 | 599 | }
|
576 | 600 |
|
| 601 | + this._previousPushAmount = {x: pushX, y: pushY}; |
| 602 | + |
577 | 603 | return {
|
578 | 604 | x: start.x + pushX,
|
579 | 605 | y: start.y + pushY,
|
@@ -792,8 +818,9 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
|
792 | 818 | const styles = {} as CSSStyleDeclaration;
|
793 | 819 |
|
794 | 820 | if (this._hasExactPosition()) {
|
795 |
| - extendStyles(styles, this._getExactOverlayY(position, originPoint)); |
796 |
| - extendStyles(styles, this._getExactOverlayX(position, originPoint)); |
| 821 | + const scrollPosition = this._viewportRuler.getViewportScrollPosition(); |
| 822 | + extendStyles(styles, this._getExactOverlayY(position, originPoint, scrollPosition)); |
| 823 | + extendStyles(styles, this._getExactOverlayX(position, originPoint, scrollPosition)); |
797 | 824 | } else {
|
798 | 825 | styles.position = 'static';
|
799 | 826 | }
|
@@ -832,14 +859,16 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
|
832 | 859 | }
|
833 | 860 |
|
834 | 861 | /** Gets the exact top/bottom for the overlay when not using flexible sizing or when pushing. */
|
835 |
| - private _getExactOverlayY(position: ConnectedPosition, originPoint: Point) { |
| 862 | + private _getExactOverlayY(position: ConnectedPosition, |
| 863 | + originPoint: Point, |
| 864 | + scrollPosition: ViewportScrollPosition) { |
836 | 865 | // Reset any existing styles. This is necessary in case the
|
837 | 866 | // preferred position has changed since the last `apply`.
|
838 | 867 | let styles = {top: null, bottom: null} as CSSStyleDeclaration;
|
839 | 868 | let overlayPoint = this._getOverlayPoint(originPoint, this._overlayRect, position);
|
840 | 869 |
|
841 | 870 | if (this._isPushed) {
|
842 |
| - overlayPoint = this._pushOverlayOnScreen(overlayPoint, this._overlayRect); |
| 871 | + overlayPoint = this._pushOverlayOnScreen(overlayPoint, this._overlayRect, scrollPosition); |
843 | 872 | }
|
844 | 873 |
|
845 | 874 | // @breaking-change 7.0.0 Currently the `_overlayContainer` is optional in order to avoid a
|
@@ -869,14 +898,16 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
|
869 | 898 | }
|
870 | 899 |
|
871 | 900 | /** Gets the exact left/right for the overlay when not using flexible sizing or when pushing. */
|
872 |
| - private _getExactOverlayX(position: ConnectedPosition, originPoint: Point) { |
| 901 | + private _getExactOverlayX(position: ConnectedPosition, |
| 902 | + originPoint: Point, |
| 903 | + scrollPosition: ViewportScrollPosition) { |
873 | 904 | // Reset any existing styles. This is necessary in case the preferred position has
|
874 | 905 | // changed since the last `apply`.
|
875 | 906 | let styles = {left: null, right: null} as CSSStyleDeclaration;
|
876 | 907 | let overlayPoint = this._getOverlayPoint(originPoint, this._overlayRect, position);
|
877 | 908 |
|
878 | 909 | if (this._isPushed) {
|
879 |
| - overlayPoint = this._pushOverlayOnScreen(overlayPoint, this._overlayRect); |
| 910 | + overlayPoint = this._pushOverlayOnScreen(overlayPoint, this._overlayRect, scrollPosition); |
880 | 911 | }
|
881 | 912 |
|
882 | 913 | // We want to set either `left` or `right` based on whether the overlay wants to appear "before"
|
|
0 commit comments