Skip to content

Commit 177c357

Browse files
committed
refactor(overlay): allow HTMLElement to be passed in directly to connected position
Allows for an `HTMLElement` to be passed in directly to the `FlexibleConnectedPositionStrategy`, in addition to one wrapped in an `ElementRef`. Fixes #11320.
1 parent e6f3062 commit 177c357

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

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

Lines changed: 15 additions & 15 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([{
@@ -68,16 +68,16 @@ describe('FlexibleConnectedPositionStrategy', () => {
6868
}]);
6969

7070
// Needs to be in the DOM for IE not to throw an "Unspecified error".
71-
document.body.appendChild(origin.nativeElement);
71+
document.body.appendChild(origin);
7272
attachOverlay({positionStrategy});
7373

7474
expect(() => attachOverlay({positionStrategy})).toThrow();
7575

76-
document.body.removeChild(origin.nativeElement);
76+
document.body.removeChild(origin);
7777
});
7878

7979
it('should not throw when trying to apply after being disposed', () => {
80-
const origin = new ElementRef(document.createElement('div'));
80+
const origin = document.createElement('div');
8181
const positionStrategy = overlay.position()
8282
.flexibleConnectedTo(origin)
8383
.withPositions([{
@@ -88,17 +88,17 @@ describe('FlexibleConnectedPositionStrategy', () => {
8888
}]);
8989

9090
// Needs to be in the DOM for IE not to throw an "Unspecified error".
91-
document.body.appendChild(origin.nativeElement);
91+
document.body.appendChild(origin);
9292
attachOverlay({positionStrategy});
9393
overlayRef.dispose();
9494

9595
expect(() => positionStrategy.apply()).not.toThrow();
9696

97-
document.body.removeChild(origin.nativeElement);
97+
document.body.removeChild(origin);
9898
});
9999

100100
it('should not throw when trying to re-apply the last position after being disposed', () => {
101-
const origin = new ElementRef(document.createElement('div'));
101+
const origin = document.createElement('div');
102102
const positionStrategy = overlay.position()
103103
.flexibleConnectedTo(origin)
104104
.withPositions([{
@@ -109,13 +109,13 @@ describe('FlexibleConnectedPositionStrategy', () => {
109109
}]);
110110

111111
// Needs to be in the DOM for IE not to throw an "Unspecified error".
112-
document.body.appendChild(origin.nativeElement);
112+
document.body.appendChild(origin);
113113
attachOverlay({positionStrategy});
114114
overlayRef.dispose();
115115

116116
expect(() => positionStrategy.reapplyLastPosition()).not.toThrow();
117117

118-
document.body.removeChild(origin.nativeElement);
118+
document.body.removeChild(origin);
119119
});
120120

121121
describe('without flexible dimensions and pushing', () => {
@@ -132,7 +132,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
132132
originElement = createPositionedBlockElement();
133133
document.body.appendChild(originElement);
134134
positionStrategy = overlay.position()
135-
.flexibleConnectedTo(new ElementRef(originElement))
135+
.flexibleConnectedTo(originElement)
136136
.withFlexibleDimensions(false)
137137
.withPush(false);
138138
});
@@ -984,7 +984,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
984984
originElement = createPositionedBlockElement();
985985
document.body.appendChild(originElement);
986986
positionStrategy = overlay.position()
987-
.flexibleConnectedTo(new ElementRef(originElement))
987+
.flexibleConnectedTo(originElement)
988988
.withFlexibleDimensions(false)
989989
.withPush();
990990
});
@@ -1113,7 +1113,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
11131113
beforeEach(() => {
11141114
originElement = createPositionedBlockElement();
11151115
document.body.appendChild(originElement);
1116-
positionStrategy = overlay.position().flexibleConnectedTo(new ElementRef(originElement));
1116+
positionStrategy = overlay.position().flexibleConnectedTo(originElement);
11171117
});
11181118

11191119
afterEach(() => {
@@ -1532,7 +1532,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
15321532

15331533
// Create a strategy with knowledge of the scrollable container
15341534
const strategy = overlay.position()
1535-
.flexibleConnectedTo(new ElementRef(originElement))
1535+
.flexibleConnectedTo(originElement)
15361536
.withPush(false)
15371537
.withPositions([{
15381538
originX: 'start',
@@ -1611,7 +1611,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
16111611
beforeEach(() => {
16121612
originElement = createPositionedBlockElement();
16131613
document.body.appendChild(originElement);
1614-
positionStrategy = overlay.position().flexibleConnectedTo(new ElementRef(originElement));
1614+
positionStrategy = overlay.position().flexibleConnectedTo(originElement);
16151615
});
16161616

16171617
afterEach(() => {
@@ -1721,7 +1721,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
17211721
beforeEach(() => {
17221722
originElement = createPositionedBlockElement();
17231723
document.body.appendChild(originElement);
1724-
positionStrategy = overlay.position().flexibleConnectedTo(new ElementRef(originElement));
1724+
positionStrategy = overlay.position().flexibleConnectedTo(originElement);
17251725
});
17261726

17271727
afterEach(() => {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {isElementScrolledOutsideView, isElementClippedByScrolling} from './scrol
2222
import {coerceCssPixelValue} from '@angular/cdk/coercion';
2323
import {Platform} from '@angular/cdk/platform';
2424

25-
2625
// TODO: refactor clipping detection into a separate thing (part of scrolling module)
2726
// TODO: doesn't handle both flexible width and height when it has to scroll along both axis.
2827

@@ -119,12 +118,12 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
119118
}
120119

121120
constructor(
122-
private _connectedTo: ElementRef,
121+
connectedTo: ElementRef | HTMLElement,
123122
private _viewportRuler: ViewportRuler,
124123
private _document: Document,
125124
// @deletion-target 7.0.0 `_platform` parameter to be made required.
126125
private _platform?: Platform) {
127-
this._origin = this._connectedTo.nativeElement;
126+
this.setOrigin(connectedTo);
128127
}
129128

130129
/** Attaches this position strategy to an overlay. */
@@ -370,8 +369,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
370369
* Sets the origin element, relative to which to position the overlay.
371370
* @param origin Reference to the new origin element.
372371
*/
373-
setOrigin(origin: ElementRef): this {
374-
this._origin = origin.nativeElement;
372+
setOrigin(origin: ElementRef | HTMLElement): this {
373+
this._origin = origin instanceof ElementRef ? origin.nativeElement : origin;
375374
return this;
376375
}
377376

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)