Skip to content

Commit

Permalink
fix(cdk/overlay): support SVG element as overlay origin (#18595)
Browse files Browse the repository at this point in the history
* Support SVG elements as overlay origin.

* Review.

* Cleanup.

* Missing semicolon.

* Review.

* Remove describe.

* Rename.

Co-authored-by: Jeroen Zwartepoorte <jeroen.zwartepoorte@iddinkgroup.com>
(cherry picked from commit 09f5476)
  • Loading branch information
jpzwarte authored and andrewseguin committed Mar 12, 2020
1 parent 1e8424c commit 1303d8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Expand Up @@ -767,6 +767,29 @@ describe('FlexibleConnectedPositionStrategy', () => {

});

it('should position the panel correctly when the origin is an SVG element', () => {
document.body.removeChild(originElement);
originElement = createBlockElement('svg', 'http://www.w3.org/2000/svg');
document.body.appendChild(originElement);

const originRect = originElement.getBoundingClientRect();

positionStrategy
.setOrigin(originElement)
.withPositions([{
originX: 'start',
originY: 'bottom',
overlayX: 'start',
overlayY: 'top'
}]);

attachOverlay({positionStrategy});

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

it('should account for the `offsetX` pushing the overlay out of the screen', () => {
// Position the element so it would have enough space to fit.
originElement.style.top = '200px';
Expand Down Expand Up @@ -2592,8 +2615,15 @@ function createPositionedBlockElement() {
}

/** Creates a block element with a default size. */
function createBlockElement() {
const element = document.createElement('div');
function createBlockElement(tagName = 'div', namespace?: string) {
let element;

if (namespace) {
element = document.createElementNS(namespace, tagName) as HTMLElement;
} else {
element = document.createElement(tagName);
}

element.style.width = `${DEFAULT_WIDTH}px`;
element.style.height = `${DEFAULT_HEIGHT}px`;
element.style.backgroundColor = 'rebeccapurple';
Expand Down
Expand Up @@ -1110,7 +1110,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
return origin.nativeElement.getBoundingClientRect();
}

if (origin instanceof HTMLElement) {
// Check for Element so SVG elements are also supported.
if (origin instanceof Element) {
return origin.getBoundingClientRect();
}

Expand Down

0 comments on commit 1303d8f

Please sign in to comment.