Skip to content

Commit 09f5476

Browse files
authored
fix(cdk/overlay): support SVG element as overlay origin (#18595)
* Support SVG elements as overlay origin. * Review. * Cleanup. * Missing semicolon. * Review. * Remove describe. * Rename. Co-authored-by: Jeroen Zwartepoorte <[email protected]>
1 parent fa96c8a commit 09f5476

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,29 @@ describe('FlexibleConnectedPositionStrategy', () => {
767767

768768
});
769769

770+
it('should position the panel correctly when the origin is an SVG element', () => {
771+
document.body.removeChild(originElement);
772+
originElement = createBlockElement('svg', 'http://www.w3.org/2000/svg');
773+
document.body.appendChild(originElement);
774+
775+
const originRect = originElement.getBoundingClientRect();
776+
777+
positionStrategy
778+
.setOrigin(originElement)
779+
.withPositions([{
780+
originX: 'start',
781+
originY: 'bottom',
782+
overlayX: 'start',
783+
overlayY: 'top'
784+
}]);
785+
786+
attachOverlay({positionStrategy});
787+
788+
const overlayRect = overlayRef.overlayElement.getBoundingClientRect();
789+
expect(Math.floor(overlayRect.top)).toBe(Math.floor(originRect.bottom));
790+
expect(Math.floor(overlayRect.left)).toBe(Math.floor(originRect.left));
791+
});
792+
770793
it('should account for the `offsetX` pushing the overlay out of the screen', () => {
771794
// Position the element so it would have enough space to fit.
772795
originElement.style.top = '200px';
@@ -2592,8 +2615,15 @@ function createPositionedBlockElement() {
25922615
}
25932616

25942617
/** Creates a block element with a default size. */
2595-
function createBlockElement() {
2596-
const element = document.createElement('div');
2618+
function createBlockElement(tagName = 'div', namespace?: string) {
2619+
let element;
2620+
2621+
if (namespace) {
2622+
element = document.createElementNS(namespace, tagName) as HTMLElement;
2623+
} else {
2624+
element = document.createElement(tagName);
2625+
}
2626+
25972627
element.style.width = `${DEFAULT_WIDTH}px`;
25982628
element.style.height = `${DEFAULT_HEIGHT}px`;
25992629
element.style.backgroundColor = 'rebeccapurple';

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
11101110
return origin.nativeElement.getBoundingClientRect();
11111111
}
11121112

1113-
if (origin instanceof HTMLElement) {
1113+
// Check for Element so SVG elements are also supported.
1114+
if (origin instanceof Element) {
11141115
return origin.getBoundingClientRect();
11151116
}
11161117

0 commit comments

Comments
 (0)