|
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, |
@@ -115,6 +115,9 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy { |
115 | 115 | /** Keeps track of the CSS classes that the position strategy has applied on the overlay panel. */ |
116 | 116 | private _appliedPanelClasses: string[] = []; |
117 | 117 |
|
| 118 | + /** Amount by which the overlay was pushed in each axis during the last time it was positioned. */ |
| 119 | + private _previousPushAmount: {x: number, y: number} | null; |
| 120 | + |
118 | 121 | /** Observable sequence of position changes. */ |
119 | 122 | positionChanges: Observable<ConnectedOverlayPositionChange> = Observable.create(observer => { |
120 | 123 | const subscription = this._positionChanges.subscribe(observer); |
@@ -287,6 +290,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy { |
287 | 290 |
|
288 | 291 | detach() { |
289 | 292 | this._clearPanelClasses(); |
| 293 | + this._lastPosition = null; |
| 294 | + this._previousPushAmount = null; |
290 | 295 | this._resizeSubscription.unsubscribe(); |
291 | 296 | } |
292 | 297 |
|
@@ -546,39 +551,55 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy { |
546 | 551 | * the viewport, the top-left corner will be pushed on-screen (with overflow occuring on the |
547 | 552 | * right and bottom). |
548 | 553 | * |
549 | | - * @param start The starting point from which the overlay is pushed. |
550 | | - * @param overlay The overlay dimensions. |
| 554 | + * @param start Starting point from which the overlay is pushed. |
| 555 | + * @param overlay Dimensions of the overlay. |
| 556 | + * @param scrollPosition Current viewport scroll position. |
551 | 557 | * @returns The point at which to position the overlay after pushing. This is effectively a new |
552 | 558 | * originPoint. |
553 | 559 | */ |
554 | | - private _pushOverlayOnScreen(start: Point, overlay: ClientRect): Point { |
| 560 | + private _pushOverlayOnScreen(start: Point, |
| 561 | + overlay: ClientRect, |
| 562 | + scrollPosition: ViewportScrollPosition): Point { |
| 563 | + // If the position is locked and we've pushed the overlay already, reuse the previous push |
| 564 | + // amount, rather than pushing it again. If we were to continue pushing, the element would |
| 565 | + // remain in the viewport, which goes against the expectations when position locking is enabled. |
| 566 | + if (this._previousPushAmount && this._positionLocked) { |
| 567 | + return { |
| 568 | + x: start.x + this._previousPushAmount.x, |
| 569 | + y: start.y + this._previousPushAmount.y |
| 570 | + }; |
| 571 | + } |
| 572 | + |
555 | 573 | const viewport = this._viewportRect; |
556 | 574 |
|
557 | | - // Determine how much the overlay goes outside the viewport on each side, which we'll use to |
558 | | - // 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. |
559 | 577 | const overflowRight = Math.max(start.x + overlay.width - viewport.right, 0); |
560 | 578 | const overflowBottom = Math.max(start.y + overlay.height - viewport.bottom, 0); |
561 | | - const overflowTop = Math.max(viewport.top - start.y, 0); |
562 | | - 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); |
563 | 581 |
|
564 | | - // Amount by which to push the overlay in each direction such that it remains on-screen. |
565 | | - 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; |
566 | 585 |
|
567 | 586 | // If the overlay fits completely within the bounds of the viewport, push it from whichever |
568 | 587 | // direction is goes off-screen. Otherwise, push the top-left corner such that its in the |
569 | 588 | // viewport and allow for the trailing end of the overlay to go out of bounds. |
570 | | - if (overlay.width <= viewport.width) { |
| 589 | + if (overlay.width < viewport.width) { |
571 | 590 | pushX = overflowLeft || -overflowRight; |
572 | 591 | } else { |
573 | | - pushX = viewport.left - start.x; |
| 592 | + pushX = start.x < this._viewportMargin ? (viewport.left - scrollPosition.left) - start.x : 0; |
574 | 593 | } |
575 | 594 |
|
576 | | - if (overlay.height <= viewport.height) { |
| 595 | + if (overlay.height < viewport.height) { |
577 | 596 | pushY = overflowTop || -overflowBottom; |
578 | 597 | } else { |
579 | | - pushY = viewport.top - start.y; |
| 598 | + pushY = start.y < this._viewportMargin ? (viewport.top - scrollPosition.top) - start.y : 0; |
580 | 599 | } |
581 | 600 |
|
| 601 | + this._previousPushAmount = {x: pushX, y: pushY}; |
| 602 | + |
582 | 603 | return { |
583 | 604 | x: start.x + pushX, |
584 | 605 | y: start.y + pushY, |
@@ -801,8 +822,9 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy { |
801 | 822 | const styles = {} as CSSStyleDeclaration; |
802 | 823 |
|
803 | 824 | if (this._hasExactPosition()) { |
804 | | - extendStyles(styles, this._getExactOverlayY(position, originPoint)); |
805 | | - extendStyles(styles, this._getExactOverlayX(position, originPoint)); |
| 825 | + const scrollPosition = this._viewportRuler.getViewportScrollPosition(); |
| 826 | + extendStyles(styles, this._getExactOverlayY(position, originPoint, scrollPosition)); |
| 827 | + extendStyles(styles, this._getExactOverlayX(position, originPoint, scrollPosition)); |
806 | 828 | } else { |
807 | 829 | styles.position = 'static'; |
808 | 830 | } |
@@ -841,14 +863,16 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy { |
841 | 863 | } |
842 | 864 |
|
843 | 865 | /** Gets the exact top/bottom for the overlay when not using flexible sizing or when pushing. */ |
844 | | - private _getExactOverlayY(position: ConnectedPosition, originPoint: Point) { |
| 866 | + private _getExactOverlayY(position: ConnectedPosition, |
| 867 | + originPoint: Point, |
| 868 | + scrollPosition: ViewportScrollPosition) { |
845 | 869 | // Reset any existing styles. This is necessary in case the |
846 | 870 | // preferred position has changed since the last `apply`. |
847 | 871 | let styles = {top: null, bottom: null} as CSSStyleDeclaration; |
848 | 872 | let overlayPoint = this._getOverlayPoint(originPoint, this._overlayRect, position); |
849 | 873 |
|
850 | 874 | if (this._isPushed) { |
851 | | - overlayPoint = this._pushOverlayOnScreen(overlayPoint, this._overlayRect); |
| 875 | + overlayPoint = this._pushOverlayOnScreen(overlayPoint, this._overlayRect, scrollPosition); |
852 | 876 | } |
853 | 877 |
|
854 | 878 | // @breaking-change 7.0.0 Currently the `_overlayContainer` is optional in order to avoid a |
@@ -878,14 +902,16 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy { |
878 | 902 | } |
879 | 903 |
|
880 | 904 | /** Gets the exact left/right for the overlay when not using flexible sizing or when pushing. */ |
881 | | - private _getExactOverlayX(position: ConnectedPosition, originPoint: Point) { |
| 905 | + private _getExactOverlayX(position: ConnectedPosition, |
| 906 | + originPoint: Point, |
| 907 | + scrollPosition: ViewportScrollPosition) { |
882 | 908 | // Reset any existing styles. This is necessary in case the preferred position has |
883 | 909 | // changed since the last `apply`. |
884 | 910 | let styles = {left: null, right: null} as CSSStyleDeclaration; |
885 | 911 | let overlayPoint = this._getOverlayPoint(originPoint, this._overlayRect, position); |
886 | 912 |
|
887 | 913 | if (this._isPushed) { |
888 | | - overlayPoint = this._pushOverlayOnScreen(overlayPoint, this._overlayRect); |
| 914 | + overlayPoint = this._pushOverlayOnScreen(overlayPoint, this._overlayRect, scrollPosition); |
889 | 915 | } |
890 | 916 |
|
891 | 917 | // We want to set either `left` or `right` based on whether the overlay wants to appear "before" |
|
0 commit comments