Skip to content

fix(reposition-scroll-strategy): use last calculated position to re-align the overlay #5471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
7 changes: 7 additions & 0 deletions src/lib/core/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ export class OverlayRef implements PortalHost {
}
}

/** Re-positions the overlay based on its last calculated position. */
recalculateLastPosition() {
if (this._state.positionStrategy) {
this._state.positionStrategy.recalculateLastPosition();
}
}

/** Updates the text direction of the overlay panel. */
private updateDirection() {
this._pane.setAttribute('dir', this._state.direction!);
Expand Down
1 change: 1 addition & 0 deletions src/lib/core/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ class FakePositionStrategy implements PositionStrategy {
return Promise.resolve(null);
}

recalculateLastPosition() { }
dispose() {}
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/core/overlay/position/global-position-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,8 @@ export class GlobalPositionStrategy implements PositionStrategy {
this._wrapper = null;
}
}

recalculateLastPosition() {
// noop since the automatic positioning is handled by CSS.
}
}
3 changes: 3 additions & 0 deletions src/lib/core/overlay/position/position-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export interface PositionStrategy {
/** Updates the position of the overlay element. */
apply(element: Element): void;

/** Re-positions the overlay element based on its last calculated position. */
recalculateLastPosition(): void;

/** Cleans up any DOM modifications made by the position strategy, if necessary. */
dispose(): void;
}
14 changes: 7 additions & 7 deletions src/lib/core/overlay/scroll/reposition-scroll-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,33 @@ describe('RepositionScrollStrategy', () => {

it('should update the overlay position when the page is scrolled', () => {
overlayRef.attach(componentPortal);
spyOn(overlayRef, 'updatePosition');
spyOn(overlayRef, 'recalculateLastPosition');

scrolledSubject.next();
expect(overlayRef.updatePosition).toHaveBeenCalledTimes(1);
expect(overlayRef.recalculateLastPosition).toHaveBeenCalledTimes(1);

scrolledSubject.next();
expect(overlayRef.updatePosition).toHaveBeenCalledTimes(2);
expect(overlayRef.recalculateLastPosition).toHaveBeenCalledTimes(2);
});

it('should not be updating the position after the overlay is detached', () => {
overlayRef.attach(componentPortal);
spyOn(overlayRef, 'updatePosition');
spyOn(overlayRef, 'recalculateLastPosition');

overlayRef.detach();
scrolledSubject.next();

expect(overlayRef.updatePosition).not.toHaveBeenCalled();
expect(overlayRef.recalculateLastPosition).not.toHaveBeenCalled();
});

it('should not be updating the position after the overlay is destroyed', () => {
overlayRef.attach(componentPortal);
spyOn(overlayRef, 'updatePosition');
spyOn(overlayRef, 'recalculateLastPosition');

overlayRef.dispose();
scrolledSubject.next();

expect(overlayRef.updatePosition).not.toHaveBeenCalled();
expect(overlayRef.recalculateLastPosition).not.toHaveBeenCalled();
});

});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/overlay/scroll/reposition-scroll-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class RepositionScrollStrategy implements ScrollStrategy {
let throttle = this._config ? this._config.scrollThrottle : 0;

this._scrollSubscription = this._scrollDispatcher.scrolled(throttle, () => {
this._overlayRef.updatePosition();
this._overlayRef.recalculateLastPosition();
});
}
}
Expand Down