diff --git a/components/popup/src/vwc-popup-base.ts b/components/popup/src/vwc-popup-base.ts index f0f378b6d..6a98ec93f 100644 --- a/components/popup/src/vwc-popup-base.ts +++ b/components/popup/src/vwc-popup-base.ts @@ -122,14 +122,8 @@ export class VWCPopupBase extends LitElement { this.open = false; } - // TODO: Make the observer work on positioning for the first time - // new `IntersectionObserver` constructor - private observer = new IntersectionObserver((entries) => { - for (const entry of entries) { - const bounds = entry.boundingClientRect; - console.log('anchor element bounds ', bounds); - requestAnimationFrame(() => this.updatePosition()); - } + private sizeObserver = new ResizeObserver(() => { + return this.updatePosition(); }); override connectedCallback(): void { @@ -143,19 +137,21 @@ export class VWCPopupBase extends LitElement { window.removeEventListener('scroll', this.updatePosition); window.removeEventListener('resize', this.onResizeWindow); // Disconnect the observer to stop from running in the background - this.observer.disconnect(); + this.sizeObserver.disconnect(); } protected override firstUpdated(_changedProperties: PropertyValues): void { super.firstUpdated(_changedProperties); this.anchorEl = this.getAnchorById(); - if(this.anchorEl) this.observer.observe(this.anchorEl); + if(this.anchorEl) this.sizeObserver.observe(this.anchorEl); } protected override updated(changes: Map): void { super.updated(changes); if (changes.has('anchor')) { + this.sizeObserver.disconnect(); this.anchorEl = this.getAnchorById(); + if(this.anchorEl) this.sizeObserver.observe(this.anchorEl); } this.updatePosition(); } diff --git a/components/popup/stories/popup.stories.js b/components/popup/stories/popup.stories.js index 12551b8c3..4326192fc 100644 --- a/components/popup/stories/popup.stories.js +++ b/components/popup/stories/popup.stories.js @@ -34,7 +34,7 @@ const Template = args => html` }