Skip to content

Commit b7071ba

Browse files
crisbetoandrewseguin
authored andcommitted
fix(connected-position-strategy): allow positions to be updated after init (#8800)
Backports the ability to update a connected overlay's positioning from the new flexible connected position strategy. This helps facilitate cases like #8653 without having to overwrite the entire position strategy.
1 parent 97ebd76 commit b7071ba

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/cdk/overlay/position/connected-position-strategy.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
OverlayContainer,
1212
ConnectedPositionStrategy,
1313
ConnectedOverlayPositionChange,
14+
ConnectionPositionPair,
1415
} from '../index';
1516

1617

@@ -544,6 +545,32 @@ describe('ConnectedPositionStrategy', () => {
544545
expect(Math.floor(overlayRect.top)).toBe(Math.floor(originRect!.top));
545546
expect(Math.floor(overlayRect.left)).toBe(Math.floor(originRect!.left));
546547
});
548+
549+
it('should allow for the positions to be updated after init', () => {
550+
strategy = positionBuilder.connectedTo(
551+
fakeElementRef,
552+
{originX: 'start', originY: 'bottom'},
553+
{overlayX: 'start', overlayY: 'top'});
554+
555+
strategy.attach(fakeOverlayRef(overlayElement));
556+
strategy.apply();
557+
558+
let overlayRect = overlayElement.getBoundingClientRect();
559+
expect(Math.floor(overlayRect.top)).toBe(Math.floor(originRect!.bottom));
560+
expect(Math.floor(overlayRect.left)).toBe(Math.floor(originRect!.left));
561+
562+
strategy.withPositions([new ConnectionPositionPair(
563+
{originX: 'start', originY: 'bottom'},
564+
{overlayX: 'end', overlayY: 'top'}
565+
)]);
566+
567+
strategy.apply();
568+
569+
overlayRect = overlayElement.getBoundingClientRect();
570+
expect(Math.floor(overlayRect.top)).toBe(Math.floor(originRect!.bottom));
571+
expect(Math.floor(overlayRect.right)).toBe(Math.floor(originRect!.left));
572+
});
573+
547574
}
548575
});
549576

src/cdk/overlay/position/connected-position-strategy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,15 @@ export class ConnectedPositionStrategy implements PositionStrategy {
256256
return this;
257257
}
258258

259+
/**
260+
* Overwrites the current set of positions with an array of new ones.
261+
* @param positions Position pairs to be set on the strategy.
262+
*/
263+
withPositions(positions: ConnectionPositionPair[]): this {
264+
this._preferredPositions = positions.slice();
265+
return this;
266+
}
267+
259268
/**
260269
* Gets the horizontal (x) "start" dimension based on whether the overlay is in an RTL context.
261270
* @param rect

0 commit comments

Comments
 (0)