Skip to content

Commit

Permalink
fix(connected-position-strategy): allow positions to be updated after…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
crisbeto authored and andrewseguin committed Dec 19, 2017
1 parent c98321c commit c207219
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/cdk/overlay/position/connected-position-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
OverlayContainer,
ConnectedPositionStrategy,
ConnectedOverlayPositionChange,
ConnectionPositionPair,
} from '../index';


Expand Down Expand Up @@ -544,6 +545,32 @@ describe('ConnectedPositionStrategy', () => {
expect(Math.floor(overlayRect.top)).toBe(Math.floor(originRect!.top));
expect(Math.floor(overlayRect.left)).toBe(Math.floor(originRect!.left));
});

it('should allow for the positions to be updated after init', () => {
strategy = positionBuilder.connectedTo(
fakeElementRef,
{originX: 'start', originY: 'bottom'},
{overlayX: 'start', overlayY: 'top'});

strategy.attach(fakeOverlayRef(overlayElement));
strategy.apply();

let overlayRect = overlayElement.getBoundingClientRect();
expect(Math.floor(overlayRect.top)).toBe(Math.floor(originRect!.bottom));
expect(Math.floor(overlayRect.left)).toBe(Math.floor(originRect!.left));

strategy.withPositions([new ConnectionPositionPair(
{originX: 'start', originY: 'bottom'},
{overlayX: 'end', overlayY: 'top'}
)]);

strategy.apply();

overlayRect = overlayElement.getBoundingClientRect();
expect(Math.floor(overlayRect.top)).toBe(Math.floor(originRect!.bottom));
expect(Math.floor(overlayRect.right)).toBe(Math.floor(originRect!.left));
});

}
});

Expand Down
9 changes: 9 additions & 0 deletions src/cdk/overlay/position/connected-position-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ export class ConnectedPositionStrategy implements PositionStrategy {
return this;
}

/**
* Overwrites the current set of positions with an array of new ones.
* @param positions Position pairs to be set on the strategy.
*/
withPositions(positions: ConnectionPositionPair[]): this {
this._preferredPositions = positions.slice();
return this;
}

/**
* Gets the horizontal (x) "start" dimension based on whether the overlay is in an RTL context.
* @param rect
Expand Down

0 comments on commit c207219

Please sign in to comment.