Skip to content

Commit 281bfa1

Browse files
authored
fix(connected-position): fix position emitter (#1863)
1 parent 72ac7a0 commit 281bfa1

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/lib/core/overlay/position/connected-position-strategy.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,23 @@ describe('ConnectedPositionStrategy', () => {
278278
{overlayX: 'end', overlayY: 'top'});
279279

280280
const positionChangeHandler = jasmine.createSpy('positionChangeHandler');
281-
strategy.onPositionChange.first().subscribe(positionChangeHandler);
281+
const subscription = strategy.onPositionChange.subscribe(positionChangeHandler);
282282

283283
strategy.apply(overlayElement);
284284
expect(positionChangeHandler).toHaveBeenCalled();
285285
expect(positionChangeHandler.calls.mostRecent().args[0])
286286
.toEqual(jasmine.any(ConnectedOverlayPositionChange),
287287
`Expected strategy to emit an instance of ConnectedOverlayPositionChange.`);
288+
289+
originElement.style.top = '0';
290+
originElement.style.left = '0';
291+
292+
// If the strategy is re-applied and the initial position would now fit,
293+
// the position change event should be emitted again.
294+
strategy.apply(overlayElement);
295+
expect(positionChangeHandler).toHaveBeenCalledTimes(2);
296+
297+
subscription.unsubscribe();
288298
});
289299

290300

src/lib/core/overlay/position/connected-position-strategy.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export class ConnectedPositionStrategy implements PositionStrategy {
7373
// We use the viewport rect to determine whether a position would go off-screen.
7474
const viewportRect = this._viewportRuler.getViewportRect();
7575
let firstOverlayPoint: Point = null;
76-
let isFirstPosition = true;
7776

7877
// We want to place the overlay in the first of the preferred positions such that the
7978
// overlay fits on-screen.
@@ -87,12 +86,9 @@ export class ConnectedPositionStrategy implements PositionStrategy {
8786
// If the overlay in the calculated position fits on-screen, put it there and we're done.
8887
if (this._willOverlayFitWithinViewport(overlayPoint, overlayRect, viewportRect)) {
8988
this._setElementPosition(element, overlayPoint);
90-
if (!isFirstPosition) {
91-
this._onPositionChange.next(new ConnectedOverlayPositionChange(pos));
92-
}
89+
this._onPositionChange.next(new ConnectedOverlayPositionChange(pos));
9390
return Promise.resolve(null);
9491
}
95-
isFirstPosition = false;
9692
}
9793

9894
// TODO(jelbourn): fallback behavior for when none of the preferred positions fit on-screen.

0 commit comments

Comments
 (0)