Skip to content

refactor(overlay): allow HTMLElement to be passed in directly to connected position #11322

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

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
}

it('should throw when attempting to attach to multiple different overlays', () => {
const origin = new ElementRef(document.createElement('div'));
const origin = document.createElement('div');
const positionStrategy = overlay.position()
.flexibleConnectedTo(origin)
.withPositions([{
Expand All @@ -68,16 +68,16 @@ describe('FlexibleConnectedPositionStrategy', () => {
}]);

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

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

document.body.removeChild(origin.nativeElement);
document.body.removeChild(origin);
});

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

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

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

document.body.removeChild(origin.nativeElement);
document.body.removeChild(origin);
});

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

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

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

document.body.removeChild(origin.nativeElement);
document.body.removeChild(origin);
});

describe('without flexible dimensions and pushing', () => {
Expand All @@ -132,7 +132,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
originElement = createPositionedBlockElement();
document.body.appendChild(originElement);
positionStrategy = overlay.position()
.flexibleConnectedTo(new ElementRef(originElement))
.flexibleConnectedTo(originElement)
.withFlexibleDimensions(false)
.withPush(false);
});
Expand Down Expand Up @@ -984,7 +984,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
originElement = createPositionedBlockElement();
document.body.appendChild(originElement);
positionStrategy = overlay.position()
.flexibleConnectedTo(new ElementRef(originElement))
.flexibleConnectedTo(originElement)
.withFlexibleDimensions(false)
.withPush();
});
Expand Down Expand Up @@ -1113,7 +1113,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
beforeEach(() => {
originElement = createPositionedBlockElement();
document.body.appendChild(originElement);
positionStrategy = overlay.position().flexibleConnectedTo(new ElementRef(originElement));
positionStrategy = overlay.position().flexibleConnectedTo(originElement);
});

afterEach(() => {
Expand Down Expand Up @@ -1532,7 +1532,7 @@ describe('FlexibleConnectedPositionStrategy', () => {

// Create a strategy with knowledge of the scrollable container
const strategy = overlay.position()
.flexibleConnectedTo(new ElementRef(originElement))
.flexibleConnectedTo(originElement)
.withPush(false)
.withPositions([{
originX: 'start',
Expand Down Expand Up @@ -1611,7 +1611,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
beforeEach(() => {
originElement = createPositionedBlockElement();
document.body.appendChild(originElement);
positionStrategy = overlay.position().flexibleConnectedTo(new ElementRef(originElement));
positionStrategy = overlay.position().flexibleConnectedTo(originElement);
});

afterEach(() => {
Expand Down Expand Up @@ -1721,7 +1721,7 @@ describe('FlexibleConnectedPositionStrategy', () => {
beforeEach(() => {
originElement = createPositionedBlockElement();
document.body.appendChild(originElement);
positionStrategy = overlay.position().flexibleConnectedTo(new ElementRef(originElement));
positionStrategy = overlay.position().flexibleConnectedTo(originElement);
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {isElementScrolledOutsideView, isElementClippedByScrolling} from './scrol
import {coerceCssPixelValue} from '@angular/cdk/coercion';
import {Platform} from '@angular/cdk/platform';


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

Expand Down Expand Up @@ -119,12 +118,12 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
}

constructor(
private _connectedTo: ElementRef,
connectedTo: ElementRef | HTMLElement,
private _viewportRuler: ViewportRuler,
private _document: Document,
// @deletion-target 7.0.0 `_platform` parameter to be made required.
private _platform?: Platform) {
this._origin = this._connectedTo.nativeElement;
this.setOrigin(connectedTo);
}

/** Attaches this position strategy to an overlay. */
Expand Down Expand Up @@ -370,8 +369,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
* Sets the origin element, relative to which to position the overlay.
* @param origin Reference to the new origin element.
*/
setOrigin(origin: ElementRef): this {
this._origin = origin.nativeElement;
setOrigin(origin: ElementRef | HTMLElement): this {
this._origin = origin instanceof ElementRef ? origin.nativeElement : origin;
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/cdk/overlay/position/overlay-position-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class OverlayPositionBuilder {
* Creates a flexible position strategy.
* @param elementRef
*/
flexibleConnectedTo(elementRef: ElementRef): FlexibleConnectedPositionStrategy {
flexibleConnectedTo(elementRef: ElementRef | HTMLElement): FlexibleConnectedPositionStrategy {
return new FlexibleConnectedPositionStrategy(elementRef, this._viewportRuler, this._document,
this._platform);
}
Expand Down