Skip to content

Commit 3ae4494

Browse files
crisbetotinayuangao
authored andcommitted
refactor(overlay): allow HTMLElement to be passed in directly to connected position (#11322)
Allows for an `HTMLElement` to be passed in directly to the `FlexibleConnectedPositionStrategy`, in addition to one wrapped in an `ElementRef`. Fixes #11320.
1 parent 81dbcd2 commit 3ae4494

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
5757
}
5858

5959
it('should throw when attempting to attach to multiple different overlays', () => {
60-
const origin = new ElementRef(document.createElement('div'));
60+
const origin = document.createElement('div');
6161
const positionStrategy = overlay.position()
6262
.flexibleConnectedTo(origin)
6363
.withPositions([{
@@ -67,12 +67,16 @@ describe('FlexibleConnectedPositionStrategy', () => {
6767
originY: 'bottom'
6868
}]);
6969

70+
// Needs to be in the DOM for IE not to throw an "Unspecified error".
71+
document.body.appendChild(origin);
7072
attachOverlay({positionStrategy});
7173
expect(() => attachOverlay({positionStrategy})).toThrow();
74+
75+
document.body.removeChild(origin);
7276
});
7377

7478
it('should not throw when trying to apply after being disposed', () => {
75-
const origin = new ElementRef(document.createElement('div'));
79+
const origin = document.createElement('div');
7680
const positionStrategy = overlay.position()
7781
.flexibleConnectedTo(origin)
7882
.withPositions([{
@@ -82,14 +86,18 @@ describe('FlexibleConnectedPositionStrategy', () => {
8286
originY: 'bottom'
8387
}]);
8488

89+
// Needs to be in the DOM for IE not to throw an "Unspecified error".
90+
document.body.appendChild(origin);
8591
attachOverlay({positionStrategy});
8692
overlayRef.dispose();
8793

8894
expect(() => positionStrategy.apply()).not.toThrow();
95+
96+
document.body.removeChild(origin);
8997
});
9098

9199
it('should not throw when trying to re-apply the last position after being disposed', () => {
92-
const origin = new ElementRef(document.createElement('div'));
100+
const origin = document.createElement('div');
93101
const positionStrategy = overlay.position()
94102
.flexibleConnectedTo(origin)
95103
.withPositions([{
@@ -99,10 +107,14 @@ describe('FlexibleConnectedPositionStrategy', () => {
99107
originY: 'bottom'
100108
}]);
101109

110+
// Needs to be in the DOM for IE not to throw an "Unspecified error".
111+
document.body.appendChild(origin);
102112
attachOverlay({positionStrategy});
103113
overlayRef.dispose();
104114

105115
expect(() => positionStrategy.reapplyLastPosition()).not.toThrow();
116+
117+
document.body.removeChild(origin);
106118
});
107119

108120
describe('without flexible dimensions and pushing', () => {
@@ -119,7 +131,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
119131
originElement = createPositionedBlockElement();
120132
document.body.appendChild(originElement);
121133
positionStrategy = overlay.position()
122-
.flexibleConnectedTo(new ElementRef(originElement))
134+
.flexibleConnectedTo(originElement)
123135
.withFlexibleDimensions(false)
124136
.withPush(false);
125137
});
@@ -863,7 +875,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
863875
originElement = createPositionedBlockElement();
864876
document.body.appendChild(originElement);
865877
positionStrategy = overlay.position()
866-
.flexibleConnectedTo(new ElementRef(originElement))
878+
.flexibleConnectedTo(originElement)
867879
.withFlexibleDimensions(false)
868880
.withPush();
869881
});
@@ -992,7 +1004,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
9921004
beforeEach(() => {
9931005
originElement = createPositionedBlockElement();
9941006
document.body.appendChild(originElement);
995-
positionStrategy = overlay.position().flexibleConnectedTo(new ElementRef(originElement));
1007+
positionStrategy = overlay.position().flexibleConnectedTo(originElement);
9961008
});
9971009

9981010
afterEach(() => {
@@ -1411,7 +1423,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
14111423

14121424
// Create a strategy with knowledge of the scrollable container
14131425
const strategy = overlay.position()
1414-
.flexibleConnectedTo(new ElementRef(originElement))
1426+
.flexibleConnectedTo(originElement)
14151427
.withPush(false)
14161428
.withPositions([{
14171429
originX: 'start',
@@ -1490,7 +1502,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
14901502
beforeEach(() => {
14911503
originElement = createPositionedBlockElement();
14921504
document.body.appendChild(originElement);
1493-
positionStrategy = overlay.position().flexibleConnectedTo(new ElementRef(originElement));
1505+
positionStrategy = overlay.position().flexibleConnectedTo(originElement);
14941506
});
14951507

14961508
afterEach(() => {
@@ -1600,7 +1612,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
16001612
beforeEach(() => {
16011613
originElement = createPositionedBlockElement();
16021614
document.body.appendChild(originElement);
1603-
positionStrategy = overlay.position().flexibleConnectedTo(new ElementRef(originElement));
1615+
positionStrategy = overlay.position().flexibleConnectedTo(originElement);
16041616
});
16051617

16061618
afterEach(() => {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
117117
}
118118

119119
constructor(
120-
private _connectedTo: ElementRef,
120+
connectedTo: ElementRef | HTMLElement,
121121
private _viewportRuler: ViewportRuler,
122122
private _document: Document,
123123
// @deletion-target 7.0.0 `_platform` parameter to be made required.
124124
private _platform?: Platform) {
125-
this._origin = this._connectedTo.nativeElement;
125+
this.setOrigin(connectedTo);
126126
}
127127

128128
/** Attaches this position strategy to an overlay. */
@@ -368,8 +368,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
368368
* Sets the origin element, relative to which to position the overlay.
369369
* @param origin Reference to the new origin element.
370370
*/
371-
setOrigin(origin: ElementRef): this {
372-
this._origin = origin.nativeElement;
371+
setOrigin(origin: ElementRef | HTMLElement): this {
372+
this._origin = origin instanceof ElementRef ? origin.nativeElement : origin;
373373
return this;
374374
}
375375

src/cdk/overlay/position/overlay-position-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class OverlayPositionBuilder {
5353
* Creates a flexible position strategy.
5454
* @param elementRef
5555
*/
56-
flexibleConnectedTo(elementRef: ElementRef): FlexibleConnectedPositionStrategy {
56+
flexibleConnectedTo(elementRef: ElementRef | HTMLElement): FlexibleConnectedPositionStrategy {
5757
return new FlexibleConnectedPositionStrategy(elementRef, this._viewportRuler, this._document,
5858
this._platform);
5959
}

0 commit comments

Comments
 (0)